dic的排序方法参考:
https://blog.csdn.net/Eric_LH/article/details/77429740
https://www.cnblogs.com/Renqy/p/12786603.html
import sys
l = [d.strip('\n') for d in sys.stdin.readlines()]
n = int(l[0])
dic = {}
# for i in range(1,n+1):
# key_value = list(map(int,l[i].split()))
# #print(key_value)
# if key_value[0] not in dic: #判断key是否存在于字典中,不在的话,直接dic[index]插入key
# dic[key_value[0]] = key_value[1]
# elif key_value[0] in dic:
# dic[key_value[0]] += key_value[1]
# for j in sorted(dic):
# print(j,dic[j])
for i in range(1,n+1):
key_value = list(map(int,l[i].split()))
dic[key_value[0]] = dic.get(key_value[0],0) + key_value[1] #get函数获取某个key的值,若key不存在,返回第二个参数
for j in sorted(dic):
print(j,dic[j])
网友评论