题解 | #合并表记录#

合并表记录

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

对字典的索引和修改是关键,注意排序时按key值排序,最后输出的格式要注意。

num1 = int(input())

dict1 = {}

for i in range(num1):
    index,value = [int(x) for x in input().split()]

    k = 0
    for key in dict1:
        if index == key:
            dict1[index] += value
            k = 1
    if k == 0:
        dict1[index] = value

dict2 = sorted(dict1.items(), key = lambda x:x[0])

for key,value in dict2:
    print(key, value)
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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