同时打印多个列表中的所有值 [英] Printing all the values from multiple lists at the same time

查看:47
本文介绍了同时打印多个列表中的所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有3个这样的列表

Suppose I have 3 lists such as these

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

如何同时打印出这些列表中的所有内容? 做这样的事情的pythonic方式是什么?

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)

然后我在for循环中调用上面的函数.

and I Call the above function inside the for loop.

推荐答案

我认为您可能需要zip:

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

您在做什么:

for f in l1,l2 and l3:

有点奇怪.

如果只想连续打印每个列表,则可以执行以下操作:

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 ]

这篇关于同时打印多个列表中的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆