Skip to content Skip to sidebar Skip to footer

How To Retrieve Every Section By 3x3?

any one know how to retrieve section from this array: a=[[1, 3, 2, 5, 7, 9, 4, 6, 8], [4, 9, 8, 2, 6, 1, 3, 7, 5], [7, 5, 6, 3, 8, 4, 2, 1, 9], [6, 4, 3, 1, 5, 8, 7, 9, 2],

Solution 1:

Here's a vectorized approach using reshaping and permuting dimensions -

a.reshape(3,3,3,3).transpose(2,0,1,3).reshape(9,3,3)

Sample run -

In [197]: a
Out[197]: 
array([[1, 3, 2, 5, 7, 9, 4, 6, 8],
       [4, 9, 8, 2, 6, 1, 3, 7, 5],
       [7, 5, 6, 3, 8, 4, 2, 1, 9],
       [6, 4, 3, 1, 5, 8, 7, 9, 2],
       [5, 2, 1, 7, 9, 3, 8, 4, 6],
       [9, 8, 7, 4, 2, 6, 5, 3, 1],
       [2, 1, 4, 9, 3, 5, 6, 8, 7],
       [3, 6, 5, 8, 1, 7, 9, 2, 4],
       [8, 7, 9, 6, 4, 2, 1, 5, 3]])

In [198]: a.reshape(3,3,3,3).transpose(2,0,1,3).reshape(9,3,3)
Out[198]: 
array([[[1, 3, 2],
        [4, 9, 8],
        [7, 5, 6]],

       [[6, 4, 3],
        [5, 2, 1],
        [9, 8, 7]],

       [[2, 1, 4],
        [3, 6, 5],
        [8, 7, 9]], ....

If you need to flatten each such section/window, just tweak the last reshaping, like so -

In [199]: a.reshape(3,3,3,3).transpose(2,0,1,3).reshape(9,9)
Out[199]: 
array([[1, 3, 2, 4, 9, 8, 7, 5, 6],
       [6, 4, 3, 5, 2, 1, 9, 8, 7],
       [2, 1, 4, 3, 6, 5, 8, 7, 9],
       [5, 7, 9, 2, 6, 1, 3, 8, 4],
       [1, 5, 8, 7, 9, 3, 4, 2, 6],
       [9, 3, 5, 8, 1, 7, 6, 4, 2],
       [4, 6, 8, 3, 7, 5, 2, 1, 9],
       [7, 9, 2, 8, 4, 6, 5, 3, 1],
       [6, 8, 7, 9, 2, 4, 1, 5, 3]])

Solution 2:

Here is the answer, using list comprehension

>>> [x[:3] for x in a[:3]]
[[1, 3, 2], [4, 9, 8], [7, 5, 6]]

Left section :

[x[0:3] for x in a[0:3]]
[x[0:3] for x in a[3:6]]
[x[0:3] for x in a[6:9]]

Middle section :

[x[3:6] for x in a[0:3]]
[x[3:6] for x in a[3:6]]
[x[3:6] for x in a[6:9]]

Right section :

[x[6:9] for x in a[0:3]]
[x[6:9] for x in a[3:6]]
[x[6:9] for x in a[6:9]]

a[i:j] takes the line from index i to j-1 x[i,j] takes the element of index i to j-1 for said lines

To create 'flattened' lists, using the input from pwnsauce's comment :

Left section :

[x for b in [x[0:3] for x in a[0:3]] for x in b]
[x for b in [x[0:3] for x in a[3:6]] for x in b]
[x for b in [x[0:3] for x in a[6:9]] for x in b]

Middle section :

[x for b in [x[3:6] for x in a[0:3]] for x in b]
[x for b in [x[3:6] for x in a[3:6]] for x in b]
[x for b in [x[3:6] for x in a[6:9]] for x in b]

Right section :

[x for b in [x[6:9] for x in a[0:3]] for x in b]
[x for b in [x[6:9] for x in a[3:6]] for x in b]
[x for b in [x[6:9] for x in a[6:9]] for x in b]

Solution 3:

You can use similar this:

for i in range(0, len(a), 3):
    left_section = []
    middle_section = []
    right_section = []
    left_section.append(a[i][:3])
    left_section.append(a[i+1][:3])
    left_section.append(a[i+2][:3])
    middle_section.append(a[i][3:6])
    middle_section.append(a[i+1][3:6])
    middle_section.append(a[i+2][3:6])
    right_section.append(a[i][3:6])
    right_section.append(a[i+1][3:6])
    right_section.append(a[i+2][3:6])
    print(left_section)
    print(middle_section)
    print(right_section)

OR

for i in range(0, len(a), 3):
    left_section = []
    middle_section = []
    right_section = []
    left_section.extend(a[i][:3])
    left_section.extend(a[i+1][:3])
    left_section.extend(a[i+2][:3])
    middle_section.extend(a[i][3:6])
    middle_section.extend(a[i+1][3:6])
    middle_section.extend(a[i+2][3:6])
    right_section.extend(a[i][3:6])
    right_section.extend(a[i+1][3:6])
    right_section.extend(a[i+2][3:6])
    print(left_section)
    print(middle_section)
    print(right_section)

Solution 4:

Pure python way without the use of numpy:

As you need the lists flattened we use this function (as already mentioned):

defflatten_list(li):
    return [el for sub_li in li for el in sub_li]

We know we can retrieve the first section like this:

flatten_list([a[row][0:3] for row in range(3)]) # retrieves the first section
>>> [1, 3, 2, 4, 9, 8, 7, 5, 6]

and all left_sections like:

[flatten_list([a[row][0:3] for row in range(y*3, y*3+3)]) for y in range(3)]
>>> [[1, 3, 2, 4, 9, 8, 7, 5, 6], [6, 4, 3, 5, 2, 1, 9, 8, 7], [2, 1, 4, 3, 6, 5, 8, 7, 9]]

all together:

[[flatten_list([a[row][x*3:x*3+3] for row in range(y*3, y*3+3)]) for y in range(3)] for x in range(3)]
>>> [[[1, 3, 2, 4, 9, 8, 7, 5, 6],
      [6, 4, 3, 5, 2, 1, 9, 8, 7],
      [2, 1, 4, 3, 6, 5, 8, 7, 9]],
     [[5, 7, 9, 2, 6, 1, 3, 8, 4],
      [1, 5, 8, 7, 9, 3, 4, 2, 6],
      [9, 3, 5, 8, 1, 7, 6, 4, 2]],
     [[4, 6, 8, 3, 7, 5, 2, 1, 9],
      [7, 9, 2, 8, 4, 6, 5, 3, 1],
      [6, 8, 7, 9, 2, 4, 1, 5, 3]]]

Post a Comment for "How To Retrieve Every Section By 3x3?"