【发布时间】:2012-11-14 09:04:53
【问题描述】:
假设你有一个 numpy 数组和一个列表:
>>> a = np.array([1,2,2,1]).reshape(2,2)
>>> a
array([[1, 2],
[2, 1]])
>>> b = [0, 10]
我想替换数组中的值,所以 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
给我一个错误:
ValueError: assignment destination is read-only
我猜那是因为我无法真正写入 numpy 数组。
附: numpy 数组的实际大小为 514 x 504,列表为 8。
【问题讨论】: