python - split list of tuples in lists of list of tuples -
i have list of tuples like:
[(1,a),(2,b), (1, e), (3, b), (2,c), (4,d), (1, b), (0,b), (6, a), (8, e)]
i want split list of lists @ every "b"
[[(1,a),(2,b)], [(1, e), (3, b)], [(2,c), (4,d), (1, b)], [(0,b)], [(6, a), (8, e)]]
is there pythonic way this?
my_list = [(1, "a"),(2, "b"), (1, "e"), (3, "b"), (2, "c"), (1, "b"), (0, "b")] result, temp = [], [] item in my_list: temp.append(item) if item[1] == 'b': result.append(temp) temp = [] if len(temp) > 0: result.append(temp) print result # [[(1, 'a'), (2, 'b')], [(1, 'e'), (3, 'b')], [(2, 'c'), (1, 'b')], [(0, 'b')]]
Comments
Post a Comment