题解 | #合并表记录#python 字典可读取多段输入方法
合并表记录
http://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
貌似大家都假定是一段数据,我这个可以读入多段数据。
即可处理如下输入:
3
3 1
2 2
1 3
4
0 1
0 2
3 1
2 1
data = []
while True:
try:
sub_data = str(input())
data.append(sub_data)
except:
break
dic = {}
i = 0
while i < len(data):
index_len = int(data[i])
start = i+1
end = start + index_len
for j in range(start,end):
key,value = data[j].split(" ")
key,value =int(key),int(value)
if key not in dic:
dic[key] = value
else:
dic[key] = dic[key] + value
i = end
sortDic = dict(sorted(dic.items(), key = lambda x:x[0], reverse = False))
for k,v in sortDic.items():
print(k,v)