Last active
December 28, 2015 15:29
-
-
Save shaxbee/7522203 to your computer and use it in GitHub Desktop.
Given two dictionaries containing key -> list of values mapping create intersection mapping common keys to dot product of values.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import product | |
def intersect_values(left, right): | |
intersection = left.viewkeys() & right.viewkeys() | |
return {key: list(product(left[key], right[key])) for key in intersection} | |
assert intersect_values({'A': [1, 2], 'B': [5]}, {'A': [3, 7]}) == {'A': [(1, 3), (1, 7), (2, 3), (2, 7)]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment