python - How to make list with sublists based on pattern(word) from a input list? -
i have list particular block of statements separated foo follows:
a=['e3p.b99990001.pdb_138:6.923:0.241:6.116', 'e3p.b99990001.pdb_397:15.856:3.506:8.144', 'foo', 'e3p.b99990002.pdb_138:4.499:4.286:8.260', 'e3p.b99990002.pdb_397:14.897:3.238:9.338', 'foo']
here, want make mainlist sublists each blog seperated pattern "foo" follows.
mainlist=[ ['e3p.b99990001.pdb_138:6.923:0.241:6.116', 'e3p.b99990001.pdb_397:15.856:3.506:8.144']#sublist1 (read input list values until first "foo" , make first sublist) ['e3p.b99990002.pdb_138:6.923:0.241:6.116', 'e3p.b99990002.pdb_397:15.856:3.506:8.144']#sublist2 (read input list values until first "foo" second "foo" , make second sublist)
main idea make different sublist using "foo" delimiter ]
i hope understandable. if 1 knows me out of it.
thanking in advance
code based on #brien gives exact answer:
sub = [] item in a: if item == 'foo': atom_coordinate.append(a) sub = [] else: a.append(item) print sub
output:
[ ['e3p.b99990001.pdb_138:6.923:0.241:6.116', 'e3p.b99990001.pdb_397:15.856:3.506:8.144', 'e3p.b99990001.pdb_424:8.558:1.315:6.627', 'e3p.b99990001.pdb_774:14.204:-5.490:24.812', 'e3p.b99990001.pdb_865:15.545:4.258:10.007', 'e3p.b99990001.pdb_929:16.146:-6.081:24.770'], ['e3p.b99990002.pdb_138:4.499:4.286:8.260', 'e3p.b99990002.pdb_397:14.897:3.238:9.338', 'e3p.b99990002.pdb_424:5.649:5.914:8.639', 'e3p.b99990002.pdb_774:12.114:-6.864:23.897', 'e3p.b99990002.pdb_865:15.200:3.910:11.227', 'e3p.b99990002.pdb_929:13.649:-6.894:22.589'] ]
# assuming original list called biglist mainlist = [] sublist = [] item in biglist: if item == 'foo': mainlist.append(sublist) sublist = [] else: sublist.append(item)
Comments
Post a Comment