<分区>
Possible Duplicate:
How to iterate through two lists in parallel?
我有 2 个列表:
l = ["a", "b", "c"]
m = ["x", "y", "z"]
我想同时遍历两者,像这样:
for e, f in l, m:
print e, f
必须显示:
a x
b y
c z
问题是那是完全非法的。我怎么能做这样的事情? (以 Pythonic 的方式)
<分区>
Possible Duplicate:
How to iterate through two lists in parallel?
我有 2 个列表:
l = ["a", "b", "c"]
m = ["x", "y", "z"]
我想同时遍历两者,像这样:
for e, f in l, m:
print e, f
必须显示:
a x
b y
c z
问题是那是完全非法的。我怎么能做这样的事情? (以 Pythonic 的方式)
最佳答案
看itertools压缩包。看起来像这样
for i,j in izip( mylistA, mylistB ):
print i + j
zip 函数也可以工作,但 izip 会创建一个迭代器,它不会强制创建第三个列表。
关于Python - 同时遍历 2 个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3945809/