Fastest way to create strictly increasing lists in Python

后端 未结 5 2110
南方客
南方客 2021-02-07 09:03

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

5条回答
  •  我在风中等你
    2021-02-07 09:18

    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])
    

提交回复
热议问题