Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于其他数组Python更新数组_Python_Arrays_Numpy - Fatal编程技术网

基于其他数组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]])