题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
import sys n = int(input()) l = list() #用list(tuple,tuple)存储 res = dict() #用dic存index for i in range(n): index, value = input().split(' ') l.append((int(index), int(value))) res[int(index)] = 0 # 更新dic的value for item in l: res[item[0]] = res[item[0]] + item[1] # 根据字典的key排序 res = dict(sorted(res.items(), key=lambda x: x[0])) for key, value in res.items(): print(key, value)