In python
, I'd like to group elements together based on a key (in example below, key is second element, or element[1]
).
initial_array = [[10, 0], [30, 0], [40, 2], [20, 2], [90, 0], [80, 0]]
Only elements which keys are the same and that are adjacent should be grouped together.
splited_array = [ [[10, 0], [30, 0]],
[[40, 2], [20, 2]],
[[90, 0], [80, 0]] ]
Additionally, i'd like the element that caused the split to be also at the end of the previous array.
splited_array = [ [[10, 0], [30, 0], [40, 2]],
[[40, 2], [20, 2], [90, 0]],
[[90, 0], [80, 0]] ]
What is the easiest way to do that in python ? (re-using Built-in Functions if possible)