循环如何在python中工作?

时间:2015-03-01 21:29:53

标签: python list loops matrix

我有变量lists中的列表列表,如下所示:

[7, 6, 1, 8, 3]
[1, 7, 2, 4, 2]
[5, 6, 4, 2, 3]
[0, 3, 3, 1, 6]
[3, 5, 2, 14, 3]
[3, 11, 9, 1, 1]
[1, 10, 2, 3, 1]

当我写lists[1]时,我得到了垂直:

6
7
6
3
5
11
10

但是当我循环它时:

for i in list:
    print(i)

我横向得到这个。

7
6
1
8
3
etc...

那么,它是如何工作的?如何修改循环以便全部垂直给我?

3 个答案:

答案 0 :(得分:4)

简短回答:

for l in lists:
   print l[1]  

答案 1 :(得分:1)

列表清单

list_of_lists = [ [1, 2, 3], [4, 5, 6], [7, 8, 9]]
for list in list_of_lists:
    for x in list:
        print x

答案 2 :(得分:0)

以下是打印列表列的方法。

lists = [[7, 6, 1, 8, 3],
[1, 7, 2, 4, 2],
[5, 6, 4, 2, 3],
[0, 3, 3, 1, 6],
[3, 5, 2, 14, 3],
[3, 11, 9, 1, 1],
[1, 10, 2, 3, 1]]

for i in range(0, len(lists[1])):
    for j in range(0, len(lists)):
        print lists[j][i],
    print "\n"