反向打印行和列

时间:2015-07-02 19:39:37

标签: python python-3.x

请帮助,我一直在和这个争斗多年。我有一个代码:

contents =[     
        [0, 3, 1, 1, 6, 3, 7, 5, 2, 4, 1],
        [0, 3, 4, 5, 11, 14, 21, 26, 28, 32, 33],
        [4, 2, 3, 2, 3, 4, 2, 4, 5, 3, 4],
        [0, 1, 2, 4, 0, 0, 0, 0, 2, 3, 5],
        [0, 4, 6, 9, 11, 14, 21, 26, 30, 35, 38],
        [4, 6, 9, 11, 14, 18, 23, 30, 35, 38, 42],
        [4, 3, 5, 6, 3, 4, 2, 4, 7, 6, 9],
        [0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0],
    ]

我想打印它,例如: 代码是:

alpha = [
    ["a", "b", "c", "d", "e"],
    ["f", "g", "h", "i", "j"],
    ["k", "l", "m", "n", "o"],
]

结果应如下所示:

["a", "f", "k"] 
["b", "g", "l"] 
["c", "h", "m"] 
["d", "i", "n"] 
["e", "j", "o"] 

1 个答案:

答案 0 :(得分:3)

使用zip即可轻松实现。

for row in zip(*contents):
    print(row)

打印:

(0, 0, 4, 0, 0, 4, 4, 0)
(3, 3, 2, 1, 4, 6, 3, 0)
(1, 4, 3, 2, 6, 9, 5, 0)
(1, 5, 2, 4, 9, 11, 6, 0)
(6, 11, 3, 0, 11, 14, 3, 0)
(3, 14, 4, 0, 14, 18, 4, 0)
(7, 21, 2, 0, 21, 23, 2, 3)
(5, 26, 4, 0, 26, 30, 4, 3)
(2, 28, 5, 2, 30, 35, 7, 0)
(4, 32, 3, 3, 35, 38, 6, 0)
(1, 33, 4, 5, 38, 42, 9, 0)