问题
I have got a problem in assigning new value by list.
I want to change 12 items values in s numpy by numpy array's index ,and i hope every index i choose is different.
so i made a list random.sample(range(0,len(s),12) to select 12 different index.And through this index change some of values in numpy array s()
However, I'm getting the error: SyntaxError: can't assign to function call
import numpy as np
import random
N = 20
s = np.zeros([N])
alist = random.sample(range(0,20),12)
alist
for i in alist:
s(i)=10
回答1:
I'm not totally sure what you're trying to achieve here, but s(i)
is your problem: round brackets imply a function call, but s
is a numpy array, so this won't work . I think you're trying to index the list, in which case you'd use s[i]
.
来源:https://stackoverflow.com/questions/61926149/numpy-how-to-select-items-in-numpy-and-assign-its-value