Printing all the values from multiple lists at the same time

后端 未结 7 1896
遥遥无期
遥遥无期 2020-12-19 05:02

Suppose I have 3 lists such as these

l1 = [1,2,3]
l2 = [4,5,6]
l3 = [7,8,9]

how do I get to print out everything from these lists at the s

7条回答
  •  借酒劲吻你
    2020-12-19 05:36

    I think you might want zip:

    for x,y,z in zip(l1,l2,l3):
        print x,y,z  #1 4 7
                     #2 5 8
                     #3 6 9
    

    What you're doing:

    for f in l1,l2 and l3:
    

    is a little strange. It is basically equivalent to for f in (l1,l3): since l2 and l3 returns l3 (assuming that l2 and l3 are both non-empty -- Otherwise, it will return the empty one.)

    If you just want to print each list consecutively, you can do:

    for lst in (l1,l2,l3):  #parenthesis unnecessary, but I like them...
        print lst   #[ 1, 2, 3 ]
                    #[ 4, 5, 6 ]
                    #[ 7, 8, 9 ]
    

提交回复
热议问题