我有两个字典,一个在代码的主体和一个输入。我想比较两个字典,如果键是相同的,我想要乘以和打印这些值。以下是我迄今为止编写的代码。Python字典:如果键是相同的乘法值
dict_a = {
'r':100,
'y':110,
'a':210
}
print('Enter The Number Of Items You Wish To Input')
n = int(input())
dict_y={}
print('Enter your dictionary')
dict_y = [ map(str, input().split()) for x in range(n)]
total = []
for word, number in dict_y:
if word in dict_a.keys():
prod = dict_y[number] * dict_a[number]
print(prod)
我不断收到同样的错误,不知道为什么:
prod = dict_a[number] * dict_y[number]
TypeError: 'set' object is unsubscriptable
样本输入将是:
r 10
y 5
a 20
所需的输出将被
1000
550
210
我真的很感激任何帮助y OU可以给我,谢谢你提前:)
为了测试,如果在dict中使用'dict_a'中的单词。 'split'已经返回一个字符串列表,所以不需要'map'。 – Daniel
好的,谢谢! –