Python|字典减法
有时,在使用字典时,我们可能会遇到实用程序问题,我们需要在字典的公共键之间执行基本操作。这可以扩展到要执行的任何操作。让我们在本文中讨论相似键值的减法以及解决方法。
方法#1:使用字典理解+keys()
上述两者的组合可用于执行此特定任务。这只是较长的循环方法的简写,可用于在一行中执行此任务。
Python3
# Python3 code to demonstrate working of
# Subtraction of dictionaries
# Using dictionary comprehension + keys()
# Initialize dictionaries
test_dict1 = {'gfg' : 6, 'is' : 4, 'best' : 7}
test_dict2 = {'gfg' : 10, 'is' : 6, 'best' : 10}
# printing original dictionaries
print("The original dictionary 1 : " + str(test_dict1))
print("The original dictionary 2 : " + str(test_dict2))
# Using dictionary comprehension + keys()
# Subtraction of dictionaries
res = {key: test_dict2[key] - test_dict1.get(key, 0)
for key in test_dict2.keys()}
# printing result
print("The difference dictionary is : " + str(res))
Python3
# Python3 code to demonstrate working of
# Subtraction of dictionaries
# Using Counter() + "-" operator
from collections import Counter
from collections import subtract
# Initialize dictionaries
test_dict1 = {'gfg' : 6, 'is' : 4, 'best' : 7}
test_dict2 = {'gfg' : 10, 'is' : 6, 'best' : 10}
# printing original dictionaries
print("The original dictionary 1 : " + str(test_dict1))
print("The original dictionary 2 : " + str(test_dict2))
# Using Counter() + "-" operator
# Subtraction of dictionaries
temp1 = Counter(test_dict1)
temp2 = Counter(test_dict2)
res = temp2 - temp1
# printing result
print("The difference dictionary is : " + str(dict(res)))
# Using the subtract method of
test_dict2.subtract(test_dict1)
# printing result
print("The difference dictionary is : " + str(dict(test_dict2)))
输出 :
The original dictionary 1 : {'gfg': 6, 'is': 4, 'best': 7}
The original dictionary 2 : {'gfg': 10, 'is': 6, 'best': 10}
The difference dictionary is : {'gfg': 4, 'is': 2, 'best': 3}
方法 #2:使用 Counter() + “-”运算符
上述方法的组合可用于执行此特定任务。在此,Counter函数将字典转换为减号运算符可以执行减法任务的形式。
Python3
# Python3 code to demonstrate working of
# Subtraction of dictionaries
# Using Counter() + "-" operator
from collections import Counter
from collections import subtract
# Initialize dictionaries
test_dict1 = {'gfg' : 6, 'is' : 4, 'best' : 7}
test_dict2 = {'gfg' : 10, 'is' : 6, 'best' : 10}
# printing original dictionaries
print("The original dictionary 1 : " + str(test_dict1))
print("The original dictionary 2 : " + str(test_dict2))
# Using Counter() + "-" operator
# Subtraction of dictionaries
temp1 = Counter(test_dict1)
temp2 = Counter(test_dict2)
res = temp2 - temp1
# printing result
print("The difference dictionary is : " + str(dict(res)))
# Using the subtract method of
test_dict2.subtract(test_dict1)
# printing result
print("The difference dictionary is : " + str(dict(test_dict2)))
输出 :
The original dictionary 1 : {'gfg': 6, 'is': 4, 'best': 7}
The original dictionary 2 : {'gfg': 10, 'is': 6, 'best': 10}
The difference dictionary is : {'gfg': 4, 'is': 2, 'best': 3}