I am trying to get all the unique combinations (not permutations as order is not important) of keys (str) and corresponding values (str) from the dictionary (without duplicates) in a readable form.
items_dict = {
'item1': {'value1', 'value2', 'value3'},
'item2': {'value1', 'value4'},
'item3': {'value5', 'value2', 'value6', 'value1'}
}
I put the values in sets so that one could union them. Maybe using dictionary and sets is not the right way of resolving it.
I tried with:
comb = combinations((items_dict.items()), 2)
but then got tuples of keys and values:
(('item2': {'value1', 'value4'}), ('item1': {'val ...
I need expected results to looks like this to identify combinations of keys and corresponding values separated:
'item1', 'item2': {'value1', 'value2', 'value3', 'value4'}
'item2', 'item 3': {'value5', 'value2', 'value6', 'value1', 'value4'}
'item3', 'item1': {'value1', 'value2', 'value3', 'value5', 'value6'}
So you need the itertools.combinations
result of the keys, then the set combines of those keys' values.
import itertools
from typing import Mapping, Tuple, Any
def get_combinations(d: Mapping[Any, set]) -> Mapping[Tuple[Any, Any], set]:
"""Combine keys into pairs and merge values together.
>>> d = {1: {1, 2}, 2: {3, 4}, 3: {1, 4}}
>>> get_combinations(d)
{(1, 2): {1, 2, 3, 4}, (1, 3): {1, 2, 4}, (2, 3): {1, 3, 4}}
"""
key_combos = itertools.combinations(d.keys(), 2)
result = {key_combo: set.union(*(d[key] for key in key_combo)) for key_combo in key_combos}
return result
you could pack that in a dict-comprehension:
from itertools import combinations
items_dict = {
'item1': {'value1', 'value2', 'value3'},
'item2': {'value1', 'value4'},
'item3': {'value5', 'value2', 'value6', 'value1'}
}
res = {(key1, key2): value1 | value2
for (key1, value1), (key2, value2) in combinations(items_dict.items(), r=2)}
# {('item1', 'item2'): {'value4', 'value3', 'value2', 'value1'},
# ('item1', 'item3'): {'value5', 'value3', 'value6', 'value2', 'value1'},
# ('item2', 'item3'): {'value4', 'value5', 'value6', 'value2', 'value1'}}
it might be even more useful to use a frozenset
as key of your new dictionary:
res = {frozenset((key1, key2)): value1 | value2
for (key1, value1), (key2, value2) in combinations(items_dict.items(), r=2)}
# {frozenset({'item1', 'item2'}): {'value2', 'value4', 'value1', 'value3'},
# frozenset({'item1', 'item3'}): {'value2', 'value1', 'value3', 'value5', 'value6'},
# frozenset({'item3', 'item2'}): {'value2', 'value4', 'value1', 'value5', 'value6'}}
that way you can access the values without knowing the order that combinations
produced:
# those will be the same
res[frozenset(('item3', 'item1'))]
res[frozenset(('item1', 'item3'))]
here is how you could adapt that if you need combinations of more than 2 items:
res = {frozenset(k[0] for k in kv): set.union(*(v[1] for v in kv))
for kv in combinations(items_dict.items(), r=2)}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.