题解 | #合并表记录#

合并表记录

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

a = int(input())
dic = {}
for i in range(a):
    tem = input().split( )
    index = int(tem[0])
    value = int(tem[1])
    if index in dic:##直接判断引索字符串在不在字典里
        dic[index] = dic[index] + value
    else:
        dic[index] = value
# print(sorted(dic.items()))     [(0, 3), (1, 2), (3, 4)]#sorted帮你转变成列表
# print(type((dic.items())))      <class 'dict_items'>
# print(dic.items())                  dict_items([(0, 3), (1, 2), (3, 4)])
for w in sorted(dic.items()):
    print(w[0],w[1])

【牛客站内】华为机试题练习记录

全部评论

相关推荐

06-12 10:50
门头沟学院 Java
你的不定积分没加C:我怎么在学院群看到了同样的话
点赞 评论 收藏
分享
每晚夜里独自颤抖:这个在牛客不是老熟人了吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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