Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.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/arrays/14.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 将值赋给numpy数组的切片_Python_Arrays_Numpy_Indexing - Fatal编程技术网

Python 将值赋给numpy数组的切片

Python 将值赋给numpy数组的切片,python,arrays,numpy,indexing,Python,Arrays,Numpy,Indexing,我想从一个numpy数组中获取切片,并将它们分配给一个更大的数组。 切片的长度应为64,并应均匀地从源阵列中取出。 我尝试了以下方法: r = np.arange(0,magnitude.shape[0],step) magnitudes[counter:counter+len(r),ch] = magnitude[r:r+64] 我在尝试上述代码时出现以下错误: TypeError: only integer arrays with one element can be converted

我想从一个numpy数组中获取切片,并将它们分配给一个更大的数组。 切片的长度应为64,并应均匀地从源阵列中取出。 我尝试了以下方法:

r = np.arange(0,magnitude.shape[0],step) 
magnitudes[counter:counter+len(r),ch] = magnitude[r:r+64]
我在尝试上述代码时出现以下错误:

TypeError: only integer arrays with one element can be converted to an index

实现切片的最简单的方法是什么?

magnity[r:r+64]
其中
r
是一个数组,这是错误的。切片中的变量必须是标量,
幅值[3:67]
,而不是
幅值[[1,2,3]:[5,6,7]

如果你想收集多个切片,你必须做如下的事情

In [345]: x=np.arange(10)
In [346]: [x[i:i+3] for i in range(4)]
Out[346]: [array([0, 1, 2]), array([1, 2, 3]), array([2, 3, 4]), array([3, 4, 5])]
In [347]: np.array([x[i:i+3] for i in range(4)])
Out[347]: 
array([[0, 1, 2],
       [1, 2, 3],
       [2, 3, 4],
       [3, 4, 5]])
其他的问题也在探索这方面的变化,试图找到最快的,但很难绕过一些排序循环或列表理解


如果您认为您需要更快的速度,我建议您使用这个答案,然后返回一个新问题和一个小的工作示例。

您能提供一个示例吗?