如何将值从2d列表追加到另一个列表并打印出来

时间:2016-05-18 11:20:37

标签: python list append 2d

我有这个清单:

a = [(741.0, 0), (743.0, 0), (3386.0, 0), (284577.0, 0), (290611.0, 0), (300889.0, 3), (305256.0, 0), (917458.0, 0), (917905.0, 0), (917906.0, 0), (922187.0, 0), (925852.0, 0), (1260021.0, 0), (1377096.0, 0), (1524210.0, 0), (1680657.0, 0), (1692571.0, 0), (1692645.0, 0), (1692647.0, 0), (1713958.0, 0), (1801008.0, 0), (1818975.0, 0), (1858888.0, 0), (1880544.0, 0), (1880898.0, 0), (1880899.0, 0), (1880900.0, 0), (1881062.0, 0), (1881073.0, 0), (1881240.0, 0), (1881433.0, 0), (1881434.0, 0), (1881435.0, 0), (1881436.0, 0), (1881438.0, 0), (1958358.0, 0), (1958478.0, 0), (1958479.0, 0), (1958481.0, 0), (1967310.0, 0)]

我想制作2个列表...第一个列表将包含上面每个列表中的第一个值b[0] = [741.0,743.0,3386.0,....],第二个列表将具有第二个值a = [(j,k,l),(j1,k1,l1),...]。我想这样做,以便如果a = [(j,k,l),(j1,k1,l1),...]我会创建3个列表而不是2 ...我有这个代码,但它不按我想要的方式工作:

代码示例:

b = []
for i in range(N):
    b.append([])
    for j in range(R):
        b[i].append([])
        for k in range(0,M,2):
            b[i][j].append(j - k)
print b

N = 2,R = 40,M = 80(2×40)

1 个答案:

答案 0 :(得分:2)

更多pythonic方法包括使用列表理解:

for i in range(len(a[0])):
    b.append([elt[i] for elt in a])