I would like to find a simple and / or quick way to find the whole common pair (pair: value), given the N dictionnaries in python. (It would be better 3.X)
PROBLEM
Given a set of 3 dicts(but it can be any dict, this is just an example)
n1 = {'a': 1, 'b': 2, 'c': 3}
n2 = {'a': 1, 'b': 4, 'c': 3, 'd': 4}
n3 = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
The result is for general (key: values) for n1, n2and n3
should be:
({'a': 1, 'c': 3})
And for n2and n3it should be
({'a': 1, 'c': 3, 'd': 4})
First I use brute force algorithm that will check every pair (key: value) for each dict
Here is an implementation using a recursive algorithm
SOLUTION A
list_dict = [n1, n2, n3]
def finding_uniquness(ls):
def recursion(ls, result):
if not ls:
return result
result = {k: v for k, v in result.items() for k1, v1 in ls[0].items() if k == k1 and v == v1}
return recursion(ls[1:], result)
return recursion(ls[1:], ls[0])
finding_uniquness(list_dict)
( , , dict, O (N²)?)
, Sets. , ,
B
import functools
list_dict = [n1, n2, n3]
set_list = [set(n.items()) for n in list_dict]
functools.reduce(lambda x, y: x & y, set_list)
, , key list , :
>>> n = {'a': [], 'b': 2, 'c': 3}
>>> set(n.items())
TypeError: unhashable type: 'list'
:
, .