Python - 字典的对称差分
给定两个字典,任务是编写一个Python程序来获取对称差异。
例子:
Input : test_dict1 = {‘Gfg’ : 4, ‘is’ : 3, ‘best’ : 7, ‘for’ : 3, ‘geek’ : 4},
test_dict2 = {‘Gfg’ : 4, ‘is’ : 3, ‘good’ : 7, ‘for’ : 3, ‘all’ : 4}
Output : {‘all’: 4, ‘good’: 7, ‘best’: 7, ‘geek’: 4}
Explanation : all, good, best and geek are mutually unique keys.
Input : test_dict1 = {‘Gfg’ : 4, ‘is’ : 3, ‘good’ : 7, ‘for’ : 3, ‘geek’ : 4},
test_dict2 = {‘Gfg’ : 4, ‘is’ : 3, ‘good’ : 7, ‘for’ : 3, ‘all’ : 4}
Output : {‘all’: 4, ‘geek’: 4}
Explanation : all, geek are mutually unique keys.
方法 #1:使用 ^运算符+ keys() +字典理解
在此,我们使用 keys() 提取所有密钥,并使用 ^ 运算符获得所有密钥的对称差异。所需的字典是使用字典理解编译的。
Python3
# Python3 code to demonstrate working of
# Symmetric Difference of Dictionaries
# Using ^ operator + keys() + dictionary comprehension
# initializing dictionaries
test_dict1 = {'Gfg': 4, 'is': 3, 'best': 7, 'for': 3, 'geek': 4}
test_dict2 = {'Gfg': 4, 'is': 3, 'good': 7, 'for': 3, 'all': 4}
# printing original dictionaries
print("The original dictionary 1 is : " + str(test_dict1))
print("The original dictionary 2 is : " + str(test_dict2))
# getting symmetric difference using ^ operation
res = {key: test_dict1[key] if key in test_dict1 else test_dict2[key]
for key in test_dict1.keys() ^ test_dict2.keys()}
# printing result
print("The symmetric difference : " + str(res))
Python3
# Python3 code to demonstrate working of
# Symmetric Difference of Dictionaries
# Using set.symmetric_difference() + keys()
# initializing dictionaries
test_dict1 = {'Gfg': 4, 'is': 3, 'best': 7, 'for': 3, 'geek': 4}
test_dict2 = {'Gfg': 4, 'is': 3, 'good': 7, 'for': 3, 'all': 4}
# printing original dictionaries
print("The original dictionary 1 is : " + str(test_dict1))
print("The original dictionary 2 is : " + str(test_dict2))
# computing sym. difference using set inbuilt function
res = {key: test_dict1[key] if key in test_dict1 else test_dict2[key] for key in
set(test_dict1.keys()).symmetric_difference(test_dict2.keys())}
# printing result
print("The symmetric difference : " + str(res))
输出:
The original dictionary 1 is : {‘Gfg’: 4, ‘is’: 3, ‘best’: 7, ‘for’: 3, ‘geek’: 4}
The original dictionary 2 is : {‘Gfg’: 4, ‘is’: 3, ‘good’: 7, ‘for’: 3, ‘all’: 4}
The symmetric difference : {‘geek’: 4, ‘best’: 7, ‘all’: 4, ‘good’: 7}
方法 #2:使用 set.symmetric_difference() + keys()
在这里,我们使用内置函数symmetric_difference() 执行获取不常见元素的任务。
蟒蛇3
# Python3 code to demonstrate working of
# Symmetric Difference of Dictionaries
# Using set.symmetric_difference() + keys()
# initializing dictionaries
test_dict1 = {'Gfg': 4, 'is': 3, 'best': 7, 'for': 3, 'geek': 4}
test_dict2 = {'Gfg': 4, 'is': 3, 'good': 7, 'for': 3, 'all': 4}
# printing original dictionaries
print("The original dictionary 1 is : " + str(test_dict1))
print("The original dictionary 2 is : " + str(test_dict2))
# computing sym. difference using set inbuilt function
res = {key: test_dict1[key] if key in test_dict1 else test_dict2[key] for key in
set(test_dict1.keys()).symmetric_difference(test_dict2.keys())}
# printing result
print("The symmetric difference : " + str(res))
输出:
The original dictionary 1 is : {‘Gfg’: 4, ‘is’: 3, ‘best’: 7, ‘for’: 3, ‘geek’: 4}
The original dictionary 2 is : {‘Gfg’: 4, ‘is’: 3, ‘good’: 7, ‘for’: 3, ‘all’: 4}
The symmetric difference : {‘good’: 7, ‘all’: 4, ‘geek’: 4, ‘best’: 7}