2009/3/11 Alan Gauld <alan.ga...@btinternet.com> > "Emad Nawfal (عماد نوفل)" <emadnaw...@gmail.com> wrote > >> How can I extract a specific sublist (??) from a nested list, >> > > Just extract eaach item using indexing. So first extract the sublist, > then from the sublist extract the item. > > if I want to extract the sublist ["ADJ", "good"], or the bigger sublist >> ["NP",["DET", "The"],["ADJ", "good"],["NOUN", "man"]] from the following >> nested list? >> > > Look at the structure of your list: > > nested_list = [ "S", <--- 0 > [ "NP", <--------- 1,0 > [ "DET", "The" ], <-------1,1,0 / 1,1,1 > [ "ADJ", "good" ], <-------1,2,0 / 1,2,1 > [ "NOUN", "man" ] <-------1,3,0 / 1,3,1 > ], > [ "VP", <-------2,0 > [ "V", "came" ] <-------2,1,0 / 2,1,1 > ] > ] > So to extract ['ADJ','good'] we use nested_llist[1][2] > and to extract the lionger list nested_list[1] > and to extract the word 'good' nested_list[1][2][1] > > HTH, > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor >
Now I know that I did not ask the right question. What I meant was: how to extract a sublist from a list whose length is unknown. Let's say I have a hundred of these lists and each of these has an NP somewhere, it could be nested in nested list, which is turn nested in another one and so on. The bottom line is that I do not know the index. To make things more concrete, this is a representative list. How can I extract all the sublists beginning with "NP" from it? nested_list2 = [['SBAR-TMP', ['WHADVP-4', ['SUB_CONJ', 'EndmA']], ['S', ['VP', ['PV+PVSUFF_SUBJ:3FS', 'Evrt'], ['PP-CLR', ['PREP', 'Ely'], ['NP', ['PRON_3MS', 'h']]], ['NP-SBJ', ['NOUN+NSUFF_FEM_SG+CASE_DEF_NOM', '$qyqt'], ['POSS_PRON_3MS', 'h']], ['PP-TMP', ['PREP', 'bEd'], ['NP', ['NOUN+CASE_INDEF_GEN', "EnA'"], ['ADJ+CASE_INDEF_GEN', 'Twyl']]], ['SBAR-PRP', ['SUB_CONJ', 'l'], ['S', ['VP', ['IV3FS+IV+IVSUFF_MOOD:S', 'tblg'], ['NP-SBJ', ['-NONE-', '*']], ['NP-OBJ', ['IVSUFF_DO:3MS', 'h']], ['SBAR', ['SBAR', ['PREP', 'b'], ['SUB_CONJ', '>n'], ['S', ['NP-TPC-2', ['PRON_3MS', 'h']], ['VP', ['PV+PVSUFF_SUBJ:3MS', 'wrv'], ['NP-SBJ-2', ['-NONE-', '*T*']], ['NP-OBJ', ['QP', ['NUM', '300'], ['NUM+CASE_DEF_ACC', 'Alf']], ['NOUN+CASE_INDEF_GEN', 'dwlAr']]]]], ['CONJ', 'w'], ['SBAR', ['PREP', 'b'], ['SUB_CONJ', '>n'], ['S', ['NP-TPC-3', ['PRON_3MS', 'h']], ['VP', ['PV+PVSUFF_SUBJ:3MS', 'bAt'], ['NP-SBJ-3', ['-NONE-', '*T*']], ['ADJP-PRD', ['ADJ+CASE_INDEF_ACC', 'qAdrA'], ['PP', ['PREP', 'ElY'], ['NP', ['NOUN+CASE_DEF_GEN', 'wDE'], ['NP', ['NP', ['NOUN+CASE_INDEF_GEN', 'Hd']], ['PP', ['PREP', 'l'], ['NP', ['NP', ['NUM+NSUFF_MASC_PL_GEN', 'E$ryn'], ['NOUN+NSUFF_FEM_SG+CASE_INDEF_ACC', 'snp']], ['PP', ['PREP', 'mn'], ['NP', ['NOUN+NSUFF_FEM_SG+CASE_DEF_GEN', 'HyAp'], ['NP', ['NP', ['DET+NOUN+CASE_DEF_GEN', 'Alt$rd']], ['PP-LOC', ['PREP', 'fy'], ['NP', ['NOUN+CASE_DEF_GEN', '$wArE'], ['NP', ['NP', ['NOUN+NSUFF_FEM_SG+CASE_DEF_GEN', 'mdynp'], ['NP', ['NOUN_PROP', 'lwng'], ['NOUN_PROP', 'byt$']]], ['PP-LOC', ['PREP', 'fy'], ['NP', ['NOUN+NSUFF_FEM_SG+CASE_DEF_GEN', 'wlAyp'], ['NP', ['NOUN_PROP', 'kAlyfwrnyA']]]]]]]]]]]]]]]]]]]]]]], ['ADVP-TMP-4', ['-NONE-', '*T*']]]]]] -- لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.....محمد الغزالي "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington http://emnawfal.googlepages.com --------------------------------------------------------
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor