在我的 python 代码中,我有 2 个列表
myList = ["Example", "Example2", "Example3"]
mySecondList = ["0000", "1111", "2222"]
我需要打印这些,使它们看起来像这样:
- Example 0000
- Example2 1111
- Example3 2222
有什么办法可以实现吗?
请您参考如下方法:
是的,寻找zip :
myList = ["Example", "Example2", "Example3"]
mySecondList = ["0000", "1111", "2222"]
for a, b in zip(myList, mySecondList):
print("- {} {}".format(a, b))
- Example 0000 - Example2 1111 - Example3 2222
如果列表具有相同的大小,上面的方法将起作用,否则你应该查看 izip_longest或 zip_longest来自 itertools 模块,具体取决于您使用的 python 版本
