Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
for循环在python中是如何工作的?_Python_List_Loops_Matrix - Fatal编程技术网

for循环在python中是如何工作的?

for循环在python中是如何工作的?,python,list,loops,matrix,Python,List,Loops,Matrix,我在变量list中有一个列表,类似这样: [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] 当我写列表[1]时,我得到: 6 7 6 3 5 11 10 但当我循环它时: for i in list: print(i) 我是水平的 7 6 1 8 3 etc... 那么,它是如何工作的呢?如何修改循环,

我在变量
list
中有一个列表,类似这样:

[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]
当我写
列表[1]
时,我得到:

6
7
6
3
5
11
10
但当我循环它时:

for i in list:
    print(i)
我是水平的

7
6
1
8
3
etc...
那么,它是如何工作的呢?如何修改循环,使其垂直运行?简短回答:

for l in lists:
   print l[1]  
简短答复:

for l in lists:
   print l[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
名单

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

下面是如何打印列表列的列表

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"

下面是如何打印列表列的列表

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"

你是怎么说的?当我写列表时[1]我得到了垂直:
67635110
?使用您提供的
列表
是不可能的!而
for
循环也不会给出该输出。请给出真实的例子,并澄清你的问题:“垂直地给我”到底是什么意思?你怎么说*当我写列表时[1]我垂直地:
67635110
?使用您提供的
列表
是不可能的!而
for
循环也不会给出该输出。请给出真实的例子,并澄清你的问题:“垂直地给我所有”到底是什么意思?不确定,因为索引超出了范围,你能详细说明吗?我在外循环中使用集合迭代器,而不是范围。当然,这假设您的所有列表都至少有两个元素。不确定,因为索引超出了范围。您能详细说明一下吗?我在外循环中使用集合迭代器,而不是范围。当然,这假设所有列表都至少有两个元素。我试过了,它给了我行,但我想要列。我试过了,它给了我行,但我想要列。是的,非常接近,但我想要在列表上循环,得到列的列表。不仅仅是第一列。@user3136858您能更具体一点吗?示例输出?我会修改我的答案,让它完全符合你的要求,所以很接近,但我希望在列表中循环,并获得列列表。不仅仅是第一列。@user3136858您能更具体一点吗?示例输出?我会修改我的答案,使之完全符合你的要求