Assign value to a slice of a numpy array
<p>I want to get slices from a numpy array and assign them to a larger array.
The slices should be 64 long and should be taken out evenly from the source array.
I tried the following:</p>

<pre><code>r = np.arange(0,magnitude.shape[0],step) 
magnitudes[counter:counter+len(r),ch] = magnitude[r:r+64]
</code></pre>

<p>I get the following error when I tried the above code:</p>

<pre><code>TypeError: only integer arrays with one element can be converted to an index
</code></pre>

<p>What is the most pythonic way to achieve the slicing?</p>

<p><code>magnitude[r:r+64]</code> where <code>r</code> is an array is wrong.  The variables in the slice must be scalars, <code>magnitude[3:67]</code>, not <code>magnitude[[1,2,3]:[5,6,7]]</code>.</p>

<p>If you want to collect multiple slices you have to do something like</p>

<pre><code>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]])
</code></pre>

<p>Other SO questions have explored variations on this, trying to find the fastest, but it's hard to get around some sort loop or list comprehension.</p>

<p>I'd suggest working with this answer, and come back with a new question, and a small working example, if you think you need more speed.</p>

python arrays numpy indexing