考虑这个文件: cat 1.txt
1 Red
1 Red
2 Red
2 Green
2 Green
4 Blu
5 Black
5 Black
5 Yellow
5 Pink
11 Brown
15 Brown
16 White
和这个命令 cat file.txt |排序| uniq -c
我怎么能用python做同样的事情?
答案 0 :(得分:1)
类似的东西:
from collections import Counter
cnt = Counter()
with open('file.txt', 'r') as f:
for line in f:
cnt[line] += 1
for k, v in cnt.iteritems():
print v, k