使用具有应更改位置的另一个数组编辑数组值

时间:2018-04-25 18:16:23

标签: python

这是我用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循环。

提前致谢!

2 个答案:

答案 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]]