当列表停止增加Python时,返回指数



我一直在尝试返回我的列表(数组)停止增加的索引。到目前为止,我只能第一次获得列表停止增加的索引,并反复返回。使用array_1 = np.Array([1,2,2,2,1,1,2,1])输出应为[2,5],因为这些索引是停止增加的索引。

def monotonic_check(ori_array):
    indices_array = []
    for i in ori_array:
        try:
            if ori_array[i] > ori_array[i+1]:
                indices_array.append(i)
            else:
                continue
        except:
            pass
    return indices_array

此代码反而返回[2,2,2]

您正在使用该值而不是索引

def monotonic_check(ori_array):
    indices_array = []
    for i in range(len(ori_array)):
        try:
            if ori_array[i] > ori_array[i+1]:
                indices_array.append(i)
            else:
                continue
        except:
            pass
    return indices_array

最新更新