是否可以在不遍历列表的情况下将一系列数组元素的值递增一个常量?

Is it possible to increment the value of a range of array elements by a constant without looping through the list?

python 中是否有任何模块或库可以帮助我解决这个问题?我的意思是用 O(1) 的复杂度来做到这一点。

您可以使用 numpy 来做到这一点

>>> import numpy as np
>>> data = np.arange(10)
>>> data
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> data[2:6] += 5
>>> data
array([ 0,  1,  7,  8,  9, 10,  6,  7,  8,  9])

您可以通过常量递增一系列数组元素的值,而无需在 numpy 中使用 Broadcasting 遍历列表。

https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html

但我不认为你可以在 O(1) 中做到这一点,这可能是一个 NP-hard 问题。