【问题标题】:how can i chain two none stop lists in python3? [duplicate]我如何在 python3 中链接两个非停止列表? [复制]
【发布时间】:2019-03-03 13:02:18
【问题描述】:

我有两个函数生成两个非停止列表,我想打印两个列表,但是在链函数中,第二个列表只有在第一个列表完成后才开始打印,我怎样才能打印两个列表,但是轮流

generator = chain(nonestoplist1(), nonestoplist2())
for item in generator:
  print(item)

喜欢:

first_list=[1,2,3,4,5,6,......]

second_list=[a,b,c,d,e,f,g,.....]

发电机打印:

1,a,2,b,3,c,4,d,.....

【问题讨论】:

  • 你 cn 压缩它们并遍历压缩的元组
  • 这与建议的副本不太一样,因为您希望将输入展平为单个迭代。你正在寻找chain.from_iterable(zip(gen1, gen2))

标签: python python-3.x list generator chain


【解决方案1】:
for item1, item2 in zip(nonestoplist1(), nonestoplist2()):
    print(item1)
    print(item2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-04
    • 2021-04-29
    • 2020-02-29
    • 2020-06-04
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    相关资源
    最近更新 更多