python - Converting one list into individual ones -
i have following list:
list = ['t' , 'r', 'q', ']', '[' , 'd', 'e', 'n', ']' , '[', 'l', 't'] how can convert create individual lists, like:
list1 = ['t', 'r', 'q'] list2 = ['d' , 'e', 'n'] list3 = ['l', 't']
you use itertools groupby:
import itertools l = ['t' , 'r', 'q', ']', '[' , 'd', 'e', 'n', ']' , '[', 'l', 't'] new_l = [list(group) i, group in itertools.groupby(l, lambda x: x in [']', '[']) if not i] # [['t', 'r', 'q'], ['d', 'e', 'n'], ['l', 't']] you without lambda (credit goes jon clements):
new_l = [list(group) i, group in itertools.groupby(l, set('[]').intersection) if not i]
Comments
Post a Comment