我有一个从直线渐变派生的整数数组。该数组称为 sign_slope,如下所示:
sign_slope = array([-1, 1, -1, ..., -1, -1, -1])
我正在寻找数组中的连续项为:1、-1 例如,您可以从上面的 sign_slope 输出中看到:sign_slope[1] = 1 和 sign_slope[2] = -1 这将是第一个许多我想检测项目/索引号的情况。在上述情况下,我希望代码输出与第 (n-1) 个索引对应的索引号数组或列表,即 sign_slope[1]。我已经写了以下似乎有效的打印声明。但是,我不知道如何输出索引号而不是当前情况下的值并将它们附加到列表或将它们输入到数组中。
for n in range(0, len(sign_slope)):
if sign_slope[n] < 0 and sign_slope[n - 1] > 0:
print sign_slope[n - 1]
else:
print 0
谢谢提前,
凯恩