Python-生成一个列表的对称列表,其中列表数据的列表数量为nxn的一半

时间:2017-04-09 18:16:55

标签: python list symmetric

我有这个列表列表,我想将其转换为列表的对称列表。我该怎么办?

 [[0, 8, 4, 10, 12, 9, 15, 8, 11, 5, 9, 4, 10],
     [0, 0, 7, 6, 8, 6, 7, 10, 12, 9, 8, 7, 5],
     [0, 0, 0, 7, 9, 5, 8, 5, 4, 8, 6, 10, 8],
     [0, 0, 0, 0, 6, 11, 5, 9, 8, 12, 11, 6, 9],
     [0, 0, 0, 0, 0, 7, 9, 6, 9, 8, 4, 11, 10],
     [0, 0, 0, 0, 0, 0, 10, 4, 3, 10, 6, 5, 7],
     [0, 0, 0, 0, 0, 0, 0, 10, 9, 8, 5, 9, 10],
     [0, 0, 0, 0, 0, 0, 0, 0, 11, 5, 9, 6, 7],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 11, 11, 6],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7, 5],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

3 个答案:

答案 0 :(得分:1)

如果您的意思是"对称",您可以使用numpy并执行:

import numpy as np

m = np.array([[0, 8, 4, 10, 12, 9, 15, 8, 11, 5, 9, 4, 10],
     [0, 0, 7, 6, 8, 6, 7, 10, 12, 9, 8, 7, 5],
     [0, 0, 0, 7, 9, 5, 8, 5, 4, 8, 6, 10, 8],
     [0, 0, 0, 0, 6, 11, 5, 9, 8, 12, 11, 6, 9],
     [0, 0, 0, 0, 0, 7, 9, 6, 9, 8, 4, 11, 10],
     [0, 0, 0, 0, 0, 0, 10, 4, 3, 10, 6, 5, 7],
     [0, 0, 0, 0, 0, 0, 0, 10, 9, 8, 5, 9, 10],
     [0, 0, 0, 0, 0, 0, 0, 0, 11, 5, 9, 6, 7],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 11, 11, 6],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7, 5],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

sym = m + m.transpose()

sym.tolist()

#      [[ 0,  8,  4, 10, 12,  9, 15,  8, 11,  5,  9,  4, 10],
#       [ 8,  0,  7,  6,  8,  6,  7, 10, 12,  9,  8,  7,  5],
#       [ 4,  7,  0,  7,  9,  5,  8,  5,  4,  8,  6, 10,  8],
#       [10,  6,  7,  0,  6, 11,  5,  9,  8, 12, 11,  6,  9],
#       [12,  8,  9,  6,  0,  7,  9,  6,  9,  8,  4, 11, 10],
#       [ 9,  6,  5, 11,  7,  0, 10,  4,  3, 10,  6,  5,  7],
#       [15,  7,  8,  5,  9, 10,  0, 10,  9,  8,  5,  9, 10],
#       [ 8, 10,  5,  9,  6,  4, 10,  0, 11,  5,  9,  6,  7],
#       [11, 12,  4,  8,  9,  3,  9, 11,  0,  9, 11, 11,  6],
#       [ 5,  9,  8, 12,  8, 10,  8,  5,  9,  0,  6,  7,  5],
#       [ 9,  8,  6, 11,  4,  6,  5,  9, 11,  6,  0, 10,  7],
#       [ 4,  7, 10,  6, 11,  5,  9,  6, 11,  7, 10,  0,  9],
#       [10,  5,  8,  9, 10,  7, 10,  7,  6,  5,  7,  9,  0]]

答案 1 :(得分:0)

使用基础Python你可以这样做:

AT = [list(col) for col in zip(*A)]

B = [r[:i]+s[i:] for i,(r,s) in enumerate(zip(AT,A))]

然后:

>>> for r in B: print(r)

[0, 8, 4, 10, 12, 9, 15, 8, 11, 5, 9, 4, 10]
[8, 0, 7, 6, 8, 6, 7, 10, 12, 9, 8, 7, 5]
[4, 7, 0, 7, 9, 5, 8, 5, 4, 8, 6, 10, 8]
[10, 6, 7, 0, 6, 11, 5, 9, 8, 12, 11, 6, 9]
[12, 8, 9, 6, 0, 7, 9, 6, 9, 8, 4, 11, 10]
[9, 6, 5, 11, 7, 0, 10, 4, 3, 10, 6, 5, 7]
[15, 7, 8, 5, 9, 10, 0, 10, 9, 8, 5, 9, 10]
[8, 10, 5, 9, 6, 4, 10, 0, 11, 5, 9, 6, 7]
[11, 12, 4, 8, 9, 3, 9, 11, 0, 9, 11, 11, 6]
[5, 9, 8, 12, 8, 10, 8, 5, 9, 0, 6, 7, 5]
[9, 8, 6, 11, 4, 6, 5, 9, 11, 6, 0, 10, 7]
[4, 7, 10, 6, 11, 5, 9, 6, 11, 7, 10, 0, 9]
[10, 5, 8, 9, 10, 7, 10, 7, 6, 5, 7, 9, 0]

答案 2 :(得分:0)

这不会反转子列表元素,只会反转子列表

x=[[0, 8, 4, 10, 12, 9, 15, 8, 11, 5, 9, 4, 10],
 [0, 0, 7, 6, 8, 6, 7, 10, 12, 9, 8, 7, 5],
 [0, 0, 0, 7, 9, 5, 8, 5, 4, 8, 6, 10, 8],
 [0, 0, 0, 0, 6, 11, 5, 9, 8, 12, 11, 6, 9],
 [0, 0, 0, 0, 0, 7, 9, 6, 9, 8, 4, 11, 10],
 [0, 0, 0, 0, 0, 0, 10, 4, 3, 10, 6, 5, 7],
 [0, 0, 0, 0, 0, 0, 0, 10, 9, 8, 5, 9, 10],
 [0, 0, 0, 0, 0, 0, 0, 0, 11, 5, 9, 6, 7],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 11, 11, 6],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7, 5],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

reversex = x[::-1]
for l in reversex:
    print (l)

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