简体   繁体   English

如何使用一个 for 循环遍历 2 个字典?

[英]How can I iterate over 2 dictionaries using one for loop?

I have two data structures like the following:我有两个数据结构,如下所示:

DATASET_0 = { 'key1': [ object1, object2, objectN ],
              'key2': [ object3, object4]
            }
DATASET_1 = { 'key1': [ object5, object6, objectN ],
              'key3': [ object7, object8],
              'key4': [ object9, object10],
            }

(This is a highly simplified version but it shows the two top level components - basically a dictionary of lists) (这是一个高度简化的版本,但它显示了两个顶级组件 - 基本上是一个列表字典)

So the same key might appear in both dictionaries but pointing to different objects.因此,相同的键可能出现在两个字典中,但指向不同的对象。 Normally I could just merge the two dictionaries on the fly and iterate over the merge but in this case I can't do this.通常我可以即时合并两个字典并迭代合并,但在这种情况下我不能这样做。 I can't make the top level item a list either.我也不能将顶级项目列为列表。

If the top level item was a list I could do this:如果顶级项目是一个列表,我可以这样做:

for item in DATASET_0 + DATASET_1:
    blah

Is there anyway to do the same thing using dictionaries?反正有没有用字典做同样的事情?

itertools.chain is a good friend in times like this.在这种情况下, itertools.chain是一个好朋友。

from itertools import chain

d1 = {'foo': ['bar']}
d2 = {'foo': ['baz']}
for k, v in chain(d1.iteritems(), d2.iteritems()):
   print k, v

If you want to just iterate over the keys, that becomes even easier (and ports to python3.x with no modifications!)如果您只想遍历键,那将变得更加容易(并且无需修改即可移植到 python3.x!)

for k in chain(d1, d2):
    print(k)

You can simply concatenate dict.items() lists:您可以简单地连接dict.items()列表:

for key, value in DATASET_0.items() + DATASET_1.items():

or chain the dict.iteritems() iterables (using itertools.chain() :或链接dict.iteritems()可迭代对象(使用itertools.chain()

from itertools import chain

for key, value in chain(DATASET_0.iteritems(), DATASET_1.iteritems()):

Using itertools.chain使用itertools.chain

Make an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted.创建一个迭代器,从第一个迭代器返回元素,直到它用尽,然后继续到下一个迭代器,直到所有的迭代器都用尽。 Used for treating consecutive sequences as a single sequence.用于将连续序列视为单个序列。

from itertools import chain

DATASET_0 = { 'key1': [ "object1", "object2", "objectN" ],
              'key2': [ "object3", "object4"]
            }
DATASET_1 = { 'key1': [ "object5", "object6", "objectN" ],
              'key3': [ "object7", "object8"],
              'key4': [ "object9", "object10"],
            }

for key, value in chain(DATASET_0.items(), DATASET_1.items()):
    print(key)  # Only printing key as proof of concept

>>> key1
>>> key2
>>> key1
>>> key3
>>> key4

Outdated in Python 3在 Python 3 中已过时

  • dict.items() no longer returns a list so it doesn't support the + operator dict.items()不再返回列表,因此它不支持+运算符
  • dict.iteritems() has been omitted dict.iteritems()已被省略

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM