题解 | #合并表记录#

合并表记录

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

while True:
    try:
        n = int(input())
        res = {}
        for i in range(n):
            a, b = map(int, input().split())
            if a in res:
                res[a] = res[a] + b
            else:
                res[a] = b
        res1 = sorted(res.items(), key=lambda x: x[0])
        for i, j in res1:
            print(f"{i} {j}")
    except:
        break

Python刷华为机考题 文章被收录于专栏

无聊刷刷!

全部评论
a b 输入时注意用split划分空格 sorted排序字典时,先把字典转成list,用items,再用匿名函数key=lambda x: x[0] 0代表排序key值 最后打印的时候逐行打印
点赞 回复 分享
发布于 2024-07-17 23:18 上海

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务