1

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.
2
  • Possible duplicate of How do I create a variable number of variables? Commented May 14, 2019 at 8:47
  • Hi @Chunpin, you can either use a dictionary, or a 2D list to simplify this problem. Check my answer below :) Commented May 14, 2019 at 11:02

3 Answers 3

6

The best way to do this is to create a dictionary :

d={}

for p in range(0,a.shape[0],2):
    for q in range(0,a.shape[1],2):
        d['slice_{0}_{1}'.format(p,q)] = a[p:p+2,q:q+2]

In [7] : d['slice_0_0']
Out[7] : array([[ 0,  1], [10, 11]])
Sign up to request clarification or add additional context in comments.

Comments

0

Why do you want to create so many variables, when you can simply create a two-dimensional list of 9x9 and use it's indexes in order to represent the variable names

import numpy as np

a = np.arange(100).reshape(10, 10)

#2D list of 9x9
slice = [ [0 for _ in range(9)] for _ in range(9)]
for p in range(0,a.shape[0],2):
    for q in range(0,a.shape[1],2):
        #Assign elements to the 2D list
        slice[p][q] = a[p:p+2,q:q+2]

print(slice)

You can then access any variable using the same indexes you were using in your variable names

#slice_0_0
print(slice[0][0])
#slice_0_2
print(slice[0][2])
#slice_8_8
print(slice[8][8])

The output will be

[[ 0  1]
 [10 11]]
[[ 2  3]
 [12 13]]
[[88 89]
 [98 99]]

Or another way to solve this problem is to use a dictionary with key as slice variable names

import numpy as np

a = np.arange(100).reshape(10, 10)

#Dictionary for slice variables
slice = {}

for p in range(0,a.shape[0],2):
    for q in range(0,a.shape[1],2):
        #Assign values to appropriate keys
        key = 'slice_{0}_{1}'.format(p,q)
        slice[key] = a[p:p+2,q:q+2]

print(slice)

The output will be

{'slice_0_0': array([[ 0,  1],
       [10, 11]]), 'slice_0_2': array([[ 2,  3],
       [12, 13]]), 'slice_0_4': array([[ 4,  5],
       [14, 15]]), 'slice_0_6': array([[ 6,  7],
       [16, 17]]), 'slice_0_8': array([[ 8,  9],
       [18, 19]]), 'slice_2_0': array([[20, 21],
       [30, 31]]), 'slice_2_2': array([[22, 23],
       [32, 33]]), 'slice_2_4': array([[24, 25],
       [34, 35]]), 'slice_2_6': array([[26, 27],
       [36, 37]]), 'slice_2_8': array([[28, 29],
       [38, 39]]), 'slice_4_0': array([[40, 41],
       [50, 51]]), 'slice_4_2': array([[42, 43],
       [52, 53]]), 'slice_4_4': array([[44, 45],
       [54, 55]]), 'slice_4_6': array([[46, 47],
       [56, 57]]), 'slice_4_8': array([[48, 49],
       [58, 59]]), 'slice_6_0': array([[60, 61],
       [70, 71]]), 'slice_6_2': array([[62, 63],
       [72, 73]]), 'slice_6_4': array([[64, 65],
       [74, 75]]), 'slice_6_6': array([[66, 67],
       [76, 77]]), 'slice_6_8': array([[68, 69],
       [78, 79]]), 'slice_8_0': array([[80, 81],
       [90, 91]]), 'slice_8_2': array([[82, 83],
       [92, 93]]), 'slice_8_4': array([[84, 85],
       [94, 95]]), 'slice_8_6': array([[86, 87],
       [96, 97]]), 'slice_8_8': array([[88, 89],
       [98, 99]])}

Comments

0

Take a look at the many possible answers here, but I think that is generally a bad idea to do. You might be better of just doing slice[p][q].

1 Comment

So it's better to create a dictionary, 2D list, or just keep the original array and get small array I want with command like a[p:p+2,q:q+2] ? I am new to coding and can't actually figure out the advantages of these methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.