题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
n = int(input()) l = [] for i in range(n): a = input() l.append([int(x) for x in a.split()]) dict = {} for k,v in l: if k in dict: dict[k] = dict[k] + v else: dict[k] = v sorted_l = sorted(dict.items(),key= lambda x:x[0]) for key,value in sorted_l: print(f'{key} {value}')
将输入存储到list中,得到一个二维数组,在二维数组中得到k和v,将相同k的v相加,得到一个字典,之后遍历该字典,dict.items()得到元组可迭代对象,并且输出