【问题标题】:Printing simultaneously from 2 lists从 2 个列表同时打印
【发布时间】:2020-02-02 21:29:55
【问题描述】:

假设以下 2 个列表:

l1 = ['a', 'b', 'c', 'd', 'e']
l2 = ['f', 'g', 'h', 'i']

我正在寻找以下输出

a f
b g
c h
d i
e

这是我尝试过的

for x, y in l1, l2:
    print(x, y)

但是这有太多的东西要打开,有人知道我怎样才能得到我需要的输出吗?

【问题讨论】:

    标签: python-3.x list for-loop


    【解决方案1】:

    使用pythonzip_longest

    from itertools import zip_longest
    
    l1 = ['a', 'b', 'c', 'd', 'e']
    l2 = ['f', 'g', 'h', 'i']
    
    print (zip_longest (l1, l2, fillvalue = ''))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      • 1970-01-01
      • 2022-12-11
      • 1970-01-01
      • 1970-01-01
      • 2019-02-08
      相关资源
      最近更新 更多