I would like to find out what is the most efficient way to achieve the following in Python:
Suppose we have two lists a
and b
which are of equa
You can calculate the cumulative max of a
and then drop duplicates with np.unique
with which you can also record the unique index so as to subset b
correspondingly:
a = np.array([2,1,2,3,4,5,4,6,5,7,8,9,8,10,11])
b = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15])
a_cummax = np.maximum.accumulate(a)
a_new, idx = np.unique(a_cummax, return_index=True)
a_new
# array([ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
b[idx]
# array([ 1, 4, 5, 6, 8, 10, 11, 12, 14, 15])