这是我用python编写的第一个月的编写结束,而且我正在努力学习一段看起来更简单的代码。
我试图根据np.argwhere
生成的另一个数组给出的位置编辑数组值。例如:
a = np.arange(6).reshape(2,3)
b = np.argwhere(a>3)
c = ([7,8,9],[10,11,12])
现在,我想更改c
中与数组a
中大于3的值位于同一位置的值。
由于我正在处理的真实数据的大小,我试图避免使用for循环。
提前致谢!
答案 0 :(得分:0)
您可以使用numpy
索引:
In [6]: c[np.where(a>3)] = a[a>3]
In [7]: c
Out[7]:
array([[ 7, 8, 9],
[10, 4, 5]])
答案 1 :(得分:0)
你不能这样做
c[a>3] = a[a>3]
示例:
import numpy as np
c = np.arange(7,13).reshape(2,3)
a = np.arange(6).reshape(2,3)
c[a>3] = a[a>3]
输出
>>> c
[[ 7 8 9]
[10 4 5]]