I am trying to optimize this code using numpy built-in function:
result = [test[0]]
for x in range(1, len(test)):
if test[x] <= result[-1]:
result.append(result[-1]+1)
else:
result.append(test[x])
print(result)
The above code loops through the array, checks if the previous value is equal or superior to the current value, and if it is, adds +1. Otherwise, it does nothing.
It is recursive (the current value depends on the previously calculated value). My input array is, by design, always sorted (in ascending order).
∗∗∗∗∗ 我预期会收到<条码>[0、1、2、3、4、15、16、17、18、19]代码>。
是否有更好的办法这样做? 我需要多次使用非常大的阵列(>=10M长)。