我有一个列表:
lst = [ 1,2,3,4,5,6,7,8]
我想递增索引 4 以上的所有数字。
for i in range(4,len(lst)):
lst[i]+=2
由于这个操作需要多次完成,所以我想以最有效的方式进行。 我怎样才能快速做到这一点。
最佳答案
使用Numpy对于快速数组操作,请查看以下示例:
import numpy as np
lst = np.array([1,2,3,4,5,6,7,8])
# add 2 at all indices from 4 till the end of the array
lst[4:] += 2
print(lst)
# array([ 1, 2, 3, 4, 7, 8, 9, 10])
关于python - 递增列表切片的最快方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59448954/