I have a VERY long numpy array of 3d-tuples:
array([('Session A', 'mov1', 1932), ('Session A', 'mov1', 1934),
('Session A', 'mov1', 1936), ..., ('Session B', 'mov99', 5306),
('Session B', 'mov99', 5308), ('Session B', 'mov99', 5310)], dtype=object)
Each tuple's first & second values are from a small set:
first_values = set('Session A', 'Session B')
second_values = set('mov1', 'mov2', 'mov3', ... , 'mov100')
But the third value can be any positive integer.
I'm looking for a nice Pythonic way to split the original array to separate arrays of tuples where:
- All tuples have the same value for the 1st & 2nd argument.
- The difference between the 3rd argument of every consecutive tuple is no greater than a given value delta
So for example:
delta = 5
data = [('Session A', 'mov1', 1000), ('Session A', 'mov1', 1001), ('Session A', 'mov1', 1003), ('Session A', 'mov1', 1007), ('Session A', 'mov1', 1010), ('Session A', 'mov1', 1050), ('Session A', 'mov1', 1052), ('Session A', 'mov2', 1002), ('Session A', 'mov2', 1004)]
*magical python function*
result = [
[('Session A', 'mov1', 1000), ('Session A', 'mov1', 1001), ('Session A', 'mov1', 1003), ('Session A',
'mov1', 1007), ('Session A', 'mov1', 1010)],
[('Session A', 'mov1', 1050), ('Session A', 'mov1', 1052)],
[('Session A', 'mov2', 1002), ('Session A', 'mov2', 1004)]
]
I found this answer but it's not exactly what I need. Any suggestions?