题解 | #合并表记录#

合并表记录

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
全部评论

相关推荐

rndguy:个人思路,抛砖引玉。 要我的话我先问清楚需求:要什么精度,什么速度,什么环境。 如果精度要求很低,平台也有点柔性的话,只需要输出pwm,然后开个中断记录各多少个脉冲,如果脉冲时间不对齐了就反馈控制电流加减就行。要求同步要求稍微高点的话可以在脉冲间做个线性插值,同步精度会高些。 但总体来说,如果直流有刷只有脉冲没有好的编码器的话很难做精准定位什么的(除非用一些电机磁路结构相关的奇技淫巧如高频注入什么的),所以要求更高就需要大量参数辨识和校准,那就慢多了。
点赞 评论 收藏
分享
浩浩没烦恼:一二面加起来才一个小时? 我一面就一个小时多了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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