Skip to content

Instantly share code, notes, and snippets.

@shaxbee
Last active December 28, 2015 15:29
Show Gist options
  • Save shaxbee/7522203 to your computer and use it in GitHub Desktop.
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.
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