Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.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_Numpy - Fatal编程技术网

Python 使用另一个数组中的一些元素从另一个数组中创建一个数组,并填充零个其他元素

Python 使用另一个数组中的一些元素从另一个数组中创建一个数组,并填充零个其他元素,python,numpy,Python,Numpy,我有一个数组 np.random.seed(42) x = np.random.rand(3, 3) 所以 我想从这个数组中创建另一个元素,只使用x中的一些元素,并填充零个其他元素,例如,如果我想要元素x[1::2,::2]我将得到一个数组 y = np.array([[0, 0, 0], [0.59865848, 0, 0.15599452], [0, 0, 0]]) out=np.类零(x);out[1::2,::2]=x[1::2,:

我有一个数组

np.random.seed(42)
x = np.random.rand(3, 3)
所以

我想从这个数组中创建另一个元素,只使用x中的一些元素,并填充零个其他元素,例如,如果我想要元素
x[1::2,::2]
我将得到一个数组

y = np.array([[0, 0, 0],
             [0.59865848, 0, 0.15599452],
             [0, 0, 0]])

out=np.类零(x);out[1::2,::2]=x[1::2,::2]
@user3483203谢谢!它起作用了!
y = np.array([[0, 0, 0],
             [0.59865848, 0, 0.15599452],
             [0, 0, 0]])