Python-同时迭代2个列表
可能的重复:
如何并行迭代两个列表?
我有 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?
I have 2 lists:
l = ["a", "b", "c"]
m = ["x", "y", "z"]
And I want to iterate through both at the same time, something like this:
for e, f in l, m:
print e, f
Must show:
a x
b y
c z
The thing is that is totally illegal. How can I do something like this? (In a Pythonic way)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 itertools izip。它看起来像这样
zip 函数也可以工作,但 izip 创建一个迭代器,它不会强制创建第三个列表。
Look at itertools izip. It'll look like this
The zip function will also work but izip creates an iterator which does not force the creation of a third list.