Numpy: how to select items in numpy and assign its value

泄露秘密 提交于 2021-02-17 07:10:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!