如何通过3X3检索每个部分?

时间:2016-07-21 08:53:18

标签: python list numpy

任何人都知道如何从这个数组中检索部分:

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],
  [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]]

我的意思是我想要检索第3X3节,例如,左上角是:

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

需要检索的部分是:

左栏

[[0:3,0:3]],[[3:6,0:3]],[[6:9,0:3]]

中段

[[0:3,3:6]],[[3:6,3:6]],[[6:9,3:6]]

右栏

[[0:3,6:9]],[[3:6,6:9]],[[6:9,6:9]]

如何检索所有这些部分? 是否有必要使用numpy?

4 个答案:

答案 0 :(得分:3)

这是使用reshaping和置换维度的矢量化方法 -

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

示例运行 -

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]], ....

如果您需要展平每个这样的部分/窗口,只需调整最后一次整形,就像这样 -

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]])

答案 1 :(得分:1)

以下是使用列表理解

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

左栏:

[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]]

中段:

[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]]

右图:

[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]从索引i到j-1 对于所述行<{p>,x[i,j]将索引i的元素取为j-1

制作“扁平化”&#39;列表,使用pwnsauce评论的输入:

左栏:

[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]

中段:

[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]

右图:

[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]

答案 2 :(得分:0)

你可以使用类似的:

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)

答案 3 :(得分:0)

纯粹的python方式,不使用numpy:

由于你需要扁平化的列表,我们使用这个功能(如前所述):

def flatten_list(li):
    return [el for sub_li in li for el in sub_li]

我们知道我们可以像这样检索第一部分:

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

和所有left_sections如:

[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]]

所有在一起:

[[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]]]