python读取文件时,删除重复行并计数


from collections import Counter


with open('a.txt', 'r+') as f:
a = f.readlines()
for i in range(len(a)):
a[i] = a[i].strip().lower()

b = []
a = dict(Counter(a))
for i in a.keys():
b.append('%s %d\n' % (i, a[i]))

f = open('a.txt', 'w')
f.writelines(b)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM