题解 | #合并表记录#

合并表记录

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

OMG...

This is a hard one which took me 2 hours to finish.

The use of sorted() makes dict {} into list [], which is worth noticing. It also turns the groups of keys and values into tuple - (key, value) format.

d = {}
try:
    l = int(input())
    for i in range(0, l):
        ipt = input().split(' ')
        c_index = int(ipt[0])
        c_value = int(ipt[1])
        if c_index not in d.keys():
            d[c_index] = c_value
        else:
            d[c_index] += c_value
    out = sorted(d.items(), key = lambda x:x[0]) #items() is key and value!
    # in lambda function, 0 means sort by key
    j = len(out)
    for k in range(0, j):
        print(str(out[k][0]) + ' ' + str(out[k][1]))
except EOFError:
    pass
全部评论

相关推荐

安静的鲸鱼offer...:神仙级别hr,可遇不可求,甚至他可能也是突然有感而发。只能说遇上是件幸事。
秋招开始捡漏了吗
点赞 评论 收藏
分享
10-22 12:03
山东大学 Java
程序员小白条:26届一般都得有实习,项目可以随便写的,如果不是开源社区的项目,随便包装,技术栈也是一样,所以本质应该找学历厂,多投投央国企和银行,技术要求稍微低一点的,或者国企控股那种,纯互联网一般都得要干活
应届生简历当中,HR最关...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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