[英]Possible combinations of key and values without duplicates
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. 我正在尝试以可读的形式从字典(无重复项)中获取键(str)和对应值(str)的所有唯一组合(由于顺序不重要,因此不进行排列)。
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. 因此,您需要键的
itertools.combinations
结果,然后是这些键的值的组合集。
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: 使用
frozenset
作为新字典的键可能更有用:
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: 这样,您就可以访问值而无需知道
combinations
产生的顺序:
# 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)}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.