我有兩個字典,一個在代碼的主體和一個輸入。我想比較兩個字典,如果鍵是相同的,我想要乘以和打印這些值。以下是我迄今爲止編寫的代碼。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
好的,謝謝! –