Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
493 views
in Technique[技术] by (71.8m points)

python - Printing all the values from multiple lists at the same time

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 same time ? What's the pythonic way to do something like that ?

for f in l1,l2 and l3:
    print f 

This only seems to be taking 2 lists into account.

Desired output: for each element in all the lists, I'm printing them out using a different function

def print_row(filename, status, Binary_Type):
    print " %-45s %-15s %25s " % (filename, status, Binary_Type)

and I Call the above function inside the for loop.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 ]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...