更新时间:2023-02-19 11:04:55
您可以使用:
a[0] -= 5 # use -=
这会将a
变成:
>>> a = np.array([35,2,160,56,120,80,1,1,0,0,1])
>>> a[0] -= 5
>>> a
array([ 30, 2, 160, 56, 120, 80, 1, 1, 0, 0, 1])
对于大多数操作(+
,-
,*
,/
等),有一个就地"等效项(+=
,-=
,*=
,/=
等等),该操作将与正确的操作数一起应用,然后将其存储回去.
For most operations (+
, -
, *
, /
, etc.), there is an "inplace" equivalent (+=
, -=
, *=
, /=
, etc.) that will apply that operation with the right operand and store it back.
请注意,如果要减去 all 个元素,则不应该使用Python for
循环,这样做有更有效的方法.
Note that if you want to subtract all elements, you should not use a Python for
loop, there are more efficient ways for that.