基于其他数组Python更新数组
我有两个大小相同的数组:基于其他数组Python更新数组,python,arrays,numpy,Python,Arrays,Numpy,我有两个大小相同的数组: import numpy as np myArray = np.array([[5,3,2,1,2], [2,5,3,3,3]]) myotherArray = np.array([[0,1,1,0,0], [0,0,1,0,0]]) 我喜欢将myArray中的所有值乘以5,但前提是myotherArray中的同一索引上的值为0。我该怎么做? 我试过了,但没用 myArray[
import numpy as np
myArray = np.array([[5,3,2,1,2],
[2,5,3,3,3]])
myotherArray = np.array([[0,1,1,0,0],
[0,0,1,0,0]])
我喜欢将myArray
中的所有值乘以5,但前提是myotherArray
中的同一索引上的值为0。我该怎么做?
我试过了,但没用
myArray[myotherArray == 0]*5
我的预期输出是针对myArray
([[25,3,2,5,10],
[10,25,3,15,15]])
就地倍增:
>>> myArray[myotherArray == 0] *= 5
>>> myArray
array([[25, 3, 2, 5, 10],
[10, 25, 3, 15, 15]])
不确定这是否是最有效的方法,但:
>>> myArray * np.where(myotherArray == 0, 5, 1)
array([[25, 3, 2, 5, 10],
[10, 25, 3, 15, 15]])
另一种选择:
>>> np.where(myotherArray == 0, 5*myArray, myArray)
array([[25, 3, 2, 5, 10],
[10, 25, 3, 15, 15]])