在我的 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 版本
关于Python list 2 项目符号形式的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44411345/