I need a function that accepts a list of two element tuples and returns these tuples grouped in lists where the second elements of the tuples are equal, and the original sequence order is kept. For instance,
>>> seq = [(0, 1), (1, 2), (2, 2), (3, 2), (4, 1), (5, 3), (6, 3), (7, 2)]
>>> split_repeated(seq)
[[(0, 1)], [(1, 2), (2, 2), (3, 2)], [(4, 1)], [(5, 3), (6, 3)], [(7, 2)]]
If I flat the result I must get original sequence:
>>> itertools.chain.from_iterable(split_repeated(seq)) == seq
True
1 Answer