[英]Can I iterate over several dicts in succession without merging them?
In python, let's say I have three dicts: 在python中,假设我有三个词:
d1, d2, d3 = {...}, {...}, {...}
I need to iterate over each of them and perform the same operation: 我需要迭代它们中的每一个并执行相同的操作:
for k, v in d1.iteritems():
do_some_stuff(k, v)
for k, v in d3.iteritems():
do_some_stuff(k, v)
for k, v in d3.iteritems():
do_some_stuff(k, v)
Is there a way to do this in one loop, such that each dictionary is iterated over in succession? 有没有办法在一个循环中执行此操作,以便每个字典连续迭代? Something like this, but the syntax is obviously incorrect here: 像这样的东西,但语法明显不正确:
for k, v in d1.iteritems(), d2.iteritems(), d3.iteritems():
do_some_stuff(k, v)
I don't want to merge the dictionaries. 我并不想合并的字典。 The best I can come up with is the nested loop below, but it seems like there should be "a more pythonic, single loop way." 我能想到的最好的是下面的嵌套循环,但似乎应该有“更多的pythonic,单循环方式”。
for d in (d1, d2, d3):
for k, v in d.iteritems():
do_some_stuff(k, v)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.