Your question is not accurate enough to give one answer.
1. Key intersection
If you want to cross the ID from messages ( credits to James ), do:
common_ids = p1.keys() & p2.keys()
However, if you want to iterate over documents, you have to consider which post has priority, I assume this is p1 . For iterating documents for common_ids , collections.ChainMap will be most useful:
from collections import ChainMap intersection = {id: document for id, document in ChainMap(p1, p2) if id in common_ids} for id, document in intersection: ...
Or, if you do not want to create a separate intersection dictionary:
from collections import ChainMap posts = ChainMap(p1, p2) for id in common_ids: document = posts[id]
2. Intersection of objects
If you want to cross the elements of both publications, which means the coincidence of ID and documents, use the code below ( DCPY credits ). However, this is only useful if you are looking for duplicates in terms.
duplicates = dict(p1.items() & p2.items()) for id, document in duplicates: ...
3. Go through p1 'AND' p2 .
In the case when, using the search '' AND 'and using iter you had in mind a search for both publications, and then again for collections.ChainMap ChainMap is best to iterate over (almost) all elements in several publications:
from collections import ChainMap for id, document in ChainMap(p1, p2): ...
Jcode Jan 17 '19 at 15:09 2019-01-17 15:09
source share