从数组列表制作10x10网格

时间:2018-12-06 10:53:46

标签: python arrays numpy grid

我正在努力将数组列出为10x10网格,我一直得到的输出不是我想要的。我希望有人能帮助我。

import numpy as np

x = 1
y = 1

scale = 10

nn = []

for x in range(1,scale+1):
    mm = []
    for y in range(1,scale+1):
        xy = [x,y]
        mm.append(xy)
        #print(xy)
        y=+1
    nn.append(mm)
    x=+1

nn


grid_array = np.array(nn)

grid=np.meshgrid(grid_array)

但是我得到的输出没有显示10x10

[array([ 1,  1,  1,  2,  1,  3,  1,  4,  1,  5,  1,  6,  1,  7,  1,  8,  1,
         9,  1, 10,  2,  1,  2,  2,  2,  3,  2,  4,  2,  5,  2,  6,  2,  7,
         2,  8,  2,  9,  2, 10,  3,  1,  3,  2,  3,  3,  3,  4,  3,  5,  3,
         6,  3,  7,  3,  8,  3,  9,  3, 10,  4,  1,  4,  2,  4,  3,  4,  4,
         4,  5,  4,  6,  4,  7,  4,  8,  4,  9,  4, 10,  5,  1,  5,  2,  5,
         3,  5,  4,  5,  5,  5,  6,  5,  7,  5,  8,  5,  9,  5, 10,  6,  1,
         6,  2,  6,  3,  6,  4,  6,  5,  6,  6,  6,  7,  6,  8,  6,  9,  6,
        10,  7,  1,  7,  2,  7,  3,  7,  4,  7,  5,  7,  6,  7,  7,  7,  8,
         7,  9,  7, 10,  8,  1,  8,  2,  8,  3,  8,  4,  8,  5,  8,  6,  8,
         7,  8,  8,  8,  9,  8, 10,  9,  1,  9,  2,  9,  3,  9,  4,  9,  5,
         9,  6,  9,  7,  9,  8,  9,  9,  9, 10, 10,  1, 10,  2, 10,  3, 10,
         4, 10,  5, 10,  6, 10,  7, 10,  8, 10,  9, 10, 10])]

已编辑。

这是我到目前为止所拥有的,感谢帮助人员。

import numpy as np

scale = 10
array = np.empty(shape=(scale, scale, 2)).astype(int)

for x in range(1,scale+1):
    for y in range(1,scale+1):
        #print([x,y])
        array[x-1,y-1] = [x,y] 

print(array)

2 个答案:

答案 0 :(得分:1)

您可以使用numpy来做到这一点。像这样

np.reshape(arr, (-1,10))

请参阅。 Convert a 1D array to a 2D array in numpy

答案 1 :(得分:0)

距离要实现的目标还很遥远,但是如果您只是想知道如何使用两个for循环生成10x10 numpy数组,这是您可以做的(尽管这不是大多数pythonic的实现方式) :

import numpy as np

scale = 10
array = np.empty(shape=(scale, scale))

for x in range(scale):
    for y in range(scale):
        array[x,y] = 42 # replace with whatever dynamically assigned value you want there    

print(array)