I have a numpy array over which I want to iterate. Now, I know of the nditer() function which loops over the array taking one point at a time. However, I want to iterate over the array taking 'n' points at a time. Here n can range between 100-200. Any reference as to how that is possible?
Asked
Active
Viewed 208 times
-1
-
https://stackoverflow.com/questions/434287/what-is-the-most-pythonic-way-to-iterate-over-a-list-in-chunks – RedKnite May 05 '20 at 06:38
1 Answers
0
A simple solution will be reshaping.
for chunk in arr.reshape(-1,n):
...

Murtaza Nazir
- 91
- 5
-
`arr.reshape` can lead to the array being copied. This is because not all multidimensional array layouts can be represented by a lower-dimensional array layout. For example `arr = np.zeros([10,10,10])[1:,1:,1:]`. In your loop, a copy of arr will be made, and modifications to chunk will not be reflected in arr itself. Chunking is often done to save memory, so this is an important limitation. – amaurea Mar 21 '21 at 17:49