题解 | #合并表记录#

合并表记录

https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201

round = int(input()) #输入数据的次数
while True:
    try:
        dict={} #创建字典接收数据
        for i in range(round):
            data = input().split(' ')
            index = int(data[0]) #获取索引
            qty = int(data[1]) #获取值
            dict[index] = dict.get(index, 0)+qty #若索引不在字典内,直接添加; 若存在,累加
        for i in sorted(dict.keys()): #按顺序索引输出
            print('{0} {1}'.format(i, dict[i]))
    except:
        break

全部评论

相关推荐

牛至超人:我将凌晨两点给你打电话
点赞 评论 收藏
分享
02-11 14:29
已编辑
字节跳动_QA
Edgestr:这种的写代码最狠了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务