I have python list that contains several monotonically decreasing elements. However, all these sequences are not adjacent to each other
A = [[100, 83, 82, 51, 45, 29, 100, 100, 88, 88, 76, 76, 76, 59, 10, 12, 36, 100, 100, 86, 81, 79, 65, 65, 9, 10, 8]
I want to extract a1 = [100, 83, 82, 51, 45, 29], a2=[100, 100, 88, 88, 76, 76, 76, 59, 10], a3=[100, 100, 86, 81, 79, 65, 65, 9] from A . As you must have noted that I discarded 12,36,10,8 from A as these do not follow any pattern. The First element of each subarray should be greater than 80. Hence, I have discarded monotonic sub-array with 10 as initial element
So far I have this .
def chop_array(array):
itr = 0
prev_element = 1e6
window = list()
mainWindow = list ()
for i, element in enumerate(array):
if element <= prev_element:
window.append(element)
prev_element = element
else:
mainWindow.append(window)
prev_element = element
window = list()
window.append(element)
filter_array = [True if item[0] > 80 else False for item in mainWindow]
return list(itertools.compress(mainWindow,filter_array))
Is there more efficient way to do it in python ?