arr = {'a': [1,2,3], 'b': [6,7,8], 'c': [7,11,2], 'e': [45,2,10]}
res = {'a': [1,3], 'b': [6,8], 'c': [11], 'e': [45,10], 'a,c,e':[2], 'b,c': [7]}
arr = {'a': [1,2,3], 'b': [6,7,8], 'c': [7,11,2], 'e': [45,2,10]}
res_arr = []
for key, value in arr.items():
for v in value:
for r in res_arr:
if v in r['key']:
r['value'].add(key)
break
else:
res_arr.append({
'key': set([v]),
'value': set([key])
})
res_dict = {}
for r in res_arr:
key = ','.join(r['value'])
if res_dict.get(key):
res_dict[key].extend(r['key'])
else:
res_dict[key] = list(r['key'])
from functools import reduce
from operator import add
def yolo(arr):
unique = set
def concatenate(lists):
return reduce(add, lists)
def keys_for(val):
return [key for key in arr if val in arr[key]]
def pairs():
return [(keys_for(val), val) for val in unique(concatenate(arr.values()))]
result = {}
for pair in pairs():
keys, val = ','.join(pair[0]), pair[1]
if keys not in result:
result[keys] = []
result[keys].append(val)
return result