I am trying to segment a list of list into blocks based on the type of data in the sublist's second index (namely NoneType or int).
Example Data:
arr = [
[81, None, None],
[82, None, None],
[83, None, None],
[84, None, None],
[85, 161, 360],
[86, 161, 360],
[87, 161, 360],
[88, 160, 360],
[89, 160, 360],
[90, 160, 360],
[91, 160, 360],
[92, 160, 360],
[93, None, None],
[94, None, None],
[95, None, None],
[96, 153, 359],
[97, 153, 359],
[98, 153, 359],
[99, 153, 359]]
This can be treated as a list of lists, as I said, or as a numpy array (i.e., numpy.array(arr)). Whichever is easier.
I am trying for something like this (doesn't need to be identical):
[(81, 84, None), # or [[None, None], [None, None]...] ... either is fine.
(85, 93, [[161, 360], [161, 360]]...),
(93, 95, None),
(96, 99, [[153, 359], [153, 359]]...)
]
Sloppy attempt:
none_end = 0
none_start = False
blocks_loc = list()
for i in arr:
if None in i:
if not none_start:
none_start = i[0]
none_end = i[0]
elif None not in i and none_start is not False:
blocks_loc.append((none_start, none_end))
none_start = False
none_end = 0
Then I could simply pull out the data for based on blocks_loc (which now contains [(81, 84, (93, 95)]).
However, it is hard to put into words just how terrible and ugly that code is. Something better would be great.