题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
num = int(input()) dic1 = {} for i in range(num): i_and_v = input() list1 = i_and_v.split(' ') if int(list1[0]) in dic1.keys():# 如果新的索引与原来索引有相同的,加入此指令实现对应值相加,而非覆盖 dic1[int(list1[0])] += int(list1[1]) else: dic1[int(list1[0])] = int(list1[1]) index_list = sorted(dic1) for m in index_list: #根据索引得到并输出值 print(m,dic1[m],sep=' ')