considering this file: 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
and this command cat file.txt | sort | uniq -c
how could I do the same thing with python?
considering this file: 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
and this command cat file.txt | sort | uniq -c
how could I do the same thing with python?
Something like:
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