Python - 连接所有具有相似值的键
给定一个带有字符串键并设置为值的字典,任务是编写一个Python程序来连接所有具有相似值顺序的键。
Input : test_dict = {‘gfg’ : {5, 4, 3}, ‘is’ : {4, 3, 5}, ‘best’ : {1, 4, 3}, ‘for’ : {1, 3, 4}, ‘geeks’ : {1, 2, 3}}
Output : {‘gfg-is’: frozenset({3, 4, 5}), ‘best-for’: frozenset({1, 3, 4}), ‘geeks’: frozenset({1, 2, 3})}
Explanation : Similar set keys are concatenated, {5, 4, 3} == {4, 3, 5}, hence gfg-is are concatenated.
Input : test_dict = {‘gfg’ : {5, 4, 3}, ‘is’ : {4, 3, 5}, ‘geeks’ : {1, 2, 3}}
Output : {‘gfg-is’: frozenset({3, 4, 5}), ‘geeks’: frozenset({1, 2, 3})}
Explanation : Similar set keys are concatenated, {5, 4, 3} == {4, 3, 5}, hence gfg-is are concatenated.
方法 #1:使用defaultdict() + join()
在此,我们执行散列每个集合并使用 defaultdict() 附加相应键的任务。下一步是将所有散列的键加入相似的集合值。
Python3
# Python3 code to demonstrate working of
# Concatenate similar set keys in Dictionary
# Using defaultdict() + join()
from collections import defaultdict
# initializing dictionary
test_dict = {'gfg': {5, 4, 3}, 'is': {4, 3, 5}, 'best': {
1, 4, 3}, 'for': {1, 3, 4}, 'geeks': {1, 2, 3}}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
hash_vals = defaultdict(list)
for key, val in test_dict.items():
# values are hashed using frozenset
hash_vals[frozenset(val)].append(key)
# perform joining
res = {'-'.join(keys): vals for (vals, keys) in hash_vals.items()}
# printing result
print("The concatenated keys : " + str(res))
Python3
# Python3 code to demonstrate working of
# Concatenate similar set keys in Dictionary
# Using groupby() + join() + dictionary comprehension
from itertools import groupby
# initializing dictionary
test_dict = {'gfg': {5, 4, 3}, 'is': {4, 3, 5},
'best': {1, 4, 3}, 'for': {1, 3, 4},
'geeks': {1, 2, 3}}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# elements grouped using groupby()
# dictionary comprehension provides shorthand to perform grouping
res = {'-'.join(val): key for key, val in groupby(
sorted(test_dict, key=test_dict.get), key=lambda sub: test_dict[sub])}
# printing result
print("The concatenated keys : " + str(res))
输出:
The original dictionary is : {‘gfg’: {3, 4, 5}, ‘is’: {3, 4, 5}, ‘best’: {1, 3, 4}, ‘for’: {1, 3, 4}, ‘geeks’: {1, 2, 3}}
The concatenated keys : {‘gfg-is’: frozenset({3, 4, 5}), ‘best-for’: frozenset({1, 3, 4}), ‘geeks’: frozenset({1, 2, 3})}
方法 #2:使用groupby() + join() +字典理解
在这种情况下,我们使用 groupby() 执行对类似元素进行分组的任务。之后,使用 join() 完成相似值键的连接。
蟒蛇3
# Python3 code to demonstrate working of
# Concatenate similar set keys in Dictionary
# Using groupby() + join() + dictionary comprehension
from itertools import groupby
# initializing dictionary
test_dict = {'gfg': {5, 4, 3}, 'is': {4, 3, 5},
'best': {1, 4, 3}, 'for': {1, 3, 4},
'geeks': {1, 2, 3}}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# elements grouped using groupby()
# dictionary comprehension provides shorthand to perform grouping
res = {'-'.join(val): key for key, val in groupby(
sorted(test_dict, key=test_dict.get), key=lambda sub: test_dict[sub])}
# printing result
print("The concatenated keys : " + str(res))
输出:
The original dictionary is : {‘gfg’: {3, 4, 5}, ‘is’: {3, 4, 5}, ‘best’: {1, 3, 4}, ‘for’: {1, 3, 4}, ‘geeks’: {1, 2, 3}}
The concatenated keys : {‘gfg-is’: {3, 4, 5}, ‘best-for’: {1, 3, 4}, ‘geeks’: {1, 2, 3}}