I want to slice a big array into small ones and give different names to every small array.
I used 2 for loop to slice big 10 * 10 array into 25 2 * 2 arrays, I can print them out in every loop, but I failed to give every array particular name. I also tried combine '.format()' function into the loop but still failed.
Code below worked fine and print out sliced 2 * 2 matrix that I want:
import numpy as np
a = np.arange(100).reshape(10, 10)
for p in range(0,a.shape[0],2):
for q in range(0,a.shape[1],2):
print(a[p:p+2,q:q+2])
But code below failed when I tried to generate particular name for every array generated:
import numpy as np
a = np.arange(100).reshape(10, 10)
for p in range(0,a.shape[0],2):
for q in range(0,a.shape[1],2):
'slice_{0}_{1}'.format(p,q) = a[p:p+2,q:q+2]
I expect to get arrays with format below:
slice_0_0 = array([[ 0, 1],[10,11]])
slice_0_2 = array([[ 2, 3],[12,13]])
...
slice_8_8 = array([[88,89],[98,99]])
So that I can apply these arrays with names of array instead of load the original big array directly.
- I was mentioned that this question could be a duplicate of this one, however I really didn't get what "variable variables" means, though we were both recommended using dictionary.