假设我有3个这样的 list

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

如何同时打印出这些列表中的所有内容?
做这样的事情的pythonic方式是什么?
for f in l1,l2 and l3:
    print f

这似乎只考虑了2个列表。

所需的输出:对于所有列表中的每个元素,我正在使用不同的功能将它们打印出来
def print_row(filename, status, Binary_Type):
    print " %-45s %-15s %25s " % (filename, status, Binary_Type)

然后在for循环中调用上述函数。

最佳答案

我认为您可能想要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:

有点奇怪。它基本上等效于for f in (l1,l3):,因为l2 and l3返回l3(假设l2l3都是非空的;否则,它将返回空的。)

如果只想连续打印每个列表,则可以执行以下操作:
for lst in (l1,l2,l3):  #parenthesis unnecessary, but I like them...
    print lst   #[ 1, 2, 3 ]
                #[ 4, 5, 6 ]
                #[ 7, 8, 9 ]

关于python - 同时打印多个列表中的所有值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12040989/

10-12 16:42