from collections import Counter
output = Counter()
with open("input.txt") as file:
for line in file.read().split('\n'):
if line:
key, value = line.split()
output[key] += int(value)
with open("output.txt", 'w+') as file:
for key, value in output.items():
file.write("{key} {value}\n".format(key=key, value=value))
以下应该可以实现所需的功能
Counter
仍会将其保存在内存中。如果你在一个受限的环境中运行,这对你没有任何好处李>我的建议是:
sort
发送它(我假设您的FS上有空间)相关问题 更多 >
编程相关推荐