paizaの答えの載っている練習問題において、コードの意味が分かりません。
python
1#! /usr/local/bin/python3 2 3num = int(input()) 4inputs = {} 5result = {} 6 7for i in range(num): 8 tmp = input().split() 9 10 exist = False 11 for (key, value) in inputs.items(): 12 if key == tmp[0]: 13 exist = True 14 15 if exist: 16 inputs[tmp[0]] = inputs[tmp[0]] + int(tmp[1]) 17 else: 18 inputs[tmp[0]] = int(tmp[1]) 19 20# ソート用にkeyとvalueを反転させた辞書を作る 21for (key, value) in inputs.items(): 22 result[value] = key 23 24result = sorted(result.items(), reverse=True) 25 26for i in result: 27 print(i[1] + ' ' + str(i[0]))
入力 7 A 1 D 6 C 2 G 4 B 70 A 10 B 5
B 75 A 11 D 6 G 4 C 2
同じアルファベットのものは足して、大きい順に出力するという問題です。
上のコードのときに、
if exist:
inputs[tmp[0]] = inputs[tmp[0]] + int(tmp[1])
else:
inputs[tmp[0]] = int(tmp[1])
ここの部分がわかりません
どなたか教えていただけると嬉しいです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/08/18 02:06
2020/08/18 02:15
2020/08/18 02:29
2020/08/18 02:32
2020/08/18 02:56
2020/08/18 07:31
2020/08/18 09:18