Reputation: 1821
I have two hash tables in the form of dictionaries. The keys map features to a list of occurrences of said features.
a_dict = {'a': [1,2], 'b': [2,], 'c': [1,3]}
b_dict = {'a': [6], 'c': [4]}
What I need is list or ideally a numpy array that contains all combinations of occurrences for two matching features. So in this case:
result = [[1,6],
[2,6],
[1,4],
[3,4]]
Since this is at some point is supposed to run as fast as possible on large dicts I was hoping to use comprehensions since they are understood by cython. But they have only gotten me to here:
>>> [itertools.product(value, a_dict[key]) for key,value in b_dict.items()]
[<itertools.product object at 0x1004a2960>, <itertools.product object at 0x1004a29b0>]
Thanks for your help!
Upvotes: 2
Views: 420
Reputation: 879919
import numpy as np
import itertools
a_dict = {'a': [1,2], 'b': [2,], 'c': [1,3]}
b_dict = {'a': [6], 'c': [4]}
print(list(itertools.chain.from_iterable(
itertools.product(value, b_dict[key]) for key,value in a_dict.iteritems()
if key in b_dict)))
# [(1, 6), (2, 6), (1, 4), (3, 4)]
Upvotes: 3