Liquidity
Liquidity

Reputation: 625

Python: Comparing value overlap between keys in a dictionary

I have a dict like this:

dict = defaultdict(list, {'a': [['1', '2', 'A', 'cat'],
                               ['1', '3', 'A', 'dog']], 
                          'b': [['1', '2', 'A', 'cat'],
                               ['1', '3', 'A', 'dog']],
                          'c': [['1', '2', 'A', 'cat'],
                               ['2', '2', 'A', 'snake'],
                               ['2', '2', 'A', 'bird']]}

I'd like to get all pairwise comparisons for overlapping values using the full list for each value. (Every position in the value list must match for it to be considered a match between keys)

Since a and b share ['1', '3', 'A', 'dog'] and c doesn't, a/b: ['1', '3', 'A', 'dog'].

a, b, c, all share ['1', '2', 'A', 'cat'], a/b/c: ['1', '2', 'A', 'cat'].

Only c has ['2', '2', 'A', 'snake'], so c: ['2', '2', 'A', 'snake']

Preferred output is a dictionary combining the above, something like

combine_dict = {'a/b': ['1', '3', 'A', 'dog'], 'a/b/c': ['1', '2', 'A', 'cat'], 'c': [['2', '2', 'A', 'snake'], ['2', '2', 'A', 'bird']]}

Upvotes: 1

Views: 208

Answers (1)

Ajax1234
Ajax1234

Reputation: 71451

You can use collections.defaultdict:

import collections
d = {'a': [['1', '2', 'A', 'cat'], ['1', '3', 'A', 'dog']], 'b': [['1', '2', 'A', 'cat'], ['1', '3', 'A', 'dog']], 'c': [['1', '2', 'A', 'cat'], ['2', '2', 'A', 'snake'], ['2', '2', 'A', 'bird']]}
new_d = collections.defaultdict(list)
for a, b in d.items():
  for i in b:
     new_d[tuple(i)].append(a)


new_r = collections.defaultdict(list)
for a, b in new_d.items():
   new_r['/'.join(b)].append(list(a))

new_result = {a:b[0] if len(b) == 1 else b for a, b in new_r.items()}

Output:

{'a/b/c': ['1', '2', 'A', 'cat'], 'a/b': ['1', '3', 'A', 'dog'], 'c': [['2', '2', 'A', 'snake'], ['2', '2', 'A', 'bird']]}

Upvotes: 2

Related Questions