相关疑难解决方法(0)

用列表的值替换numpy索引数组的值

假设你有一个numpy数组和一个列表:

>>> a = np.array([1,2,2,1]).reshape(2,2)
>>> a
array([[1, 2],
       [2, 1]])
>>> b = [0, 10]
Run Code Online (Sandbox Code Playgroud)

我想替换数组中的值,以便将1替换为0,将2替换为10.

我在这里发现了类似的问题 - http://mail.python.org/pipermail//tutor/2011-September/085392.html

但使用此解决方案:

for x in np.nditer(a):
    if x==1:
        x[...]=x=0
    elif x==2:
        x[...]=x=10
Run Code Online (Sandbox Code Playgroud)

给我一个错误:

ValueError: assignment destination is read-only
Run Code Online (Sandbox Code Playgroud)

我想这是因为我无法写入一个numpy数组.

PS numpy数组的实际大小是514乘504,列表是8.

python indexing numpy

19
推荐指数
3
解决办法
6万
查看次数

标签 统计

indexing ×1

numpy ×1

python ×1