2017-08-08 89 views
-1

我有一个9 x 9的正方形网格。周围是一个更大的广场。方角的坐标存储无序列表中以这样的方式排序坐标python中的列表正方形的点数

# upper left corner = x1 y1 
# lower left corner = x2 y2 
# lower right corner = x3 x3 
# upper right corner = x4 y4 

my_list:

[[[x1 y1][x2 y2][x3 y3][x4 y4]] # square 1 
    [x1 y1][x2 y2][x3 y3][x4 y4]] # square 5 
    [x1 y1][x2 y2][x3 y3][x4 y4]] # square 23 
    . 
    . 
    . 
    [x1 y1][x2 y2][x3 y3][x4 y4]]] # square 3 

我知道,我可以

my_list[0][0] 
> [x1 y1] 

例如存取权限的角落和值为:

my_list[0][0][0] 
> x1 

my_list[0][0][1] 
> y1 

不过,我不知道如何说我有一个排序列表像这样的列表进行排序:

[[square1][square2][square3]...[square8][square9] 
[square10][square11][square12]...[square17][square18] 
. 
. 
. 
[square72][square73][square74]...[square80][square81]] 

square1应该是最低的X1和最低Y1平方。是对的,我只需要第一个,也许是左上角?因为,网格广场具有相同的长度和宽度。

+0

这是一个普通的Python列表,还是它是一个Numpy数组? –

+0

如何排序最低的x1是否与最低的y1配对? – stamaimer

+0

哦,对不起,我没有写它实际上是一个正常的Python列表。 – robinarthur

回答

0

您可以将形状为(9,9,2)的my_list平放到带形状(1,81,2)的1d列表中,对其进行分类,然后将其重新整形为原始形状。

flat_list = [item for row in my_list for item in row] 
flat_list.sort() 

# If you want to reshape the 1d list with shape(1, 81, 2) to 2d list with shape(9, 9, 2) 
result = [] 
temp = [] 
count = 0 

for item in flat_list: 
    temp.append(item) 
    count += 1 
    if count % 9 == 0: 
     result.append(temp) 
     temp = [] 
     count = 0 

# You also can use numpy to reshape flat_list to (9, 9, 2) 
import numpy to np 
result = np.array(flat_list).reshape(9, 9, 2) 
+0

谢谢你的帮助。 – robinarthur