-2

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?

Mawg says reinstate Monica
  • 36,598
  • 98
  • 292
  • 531
Pydavide
  • 59
  • 4

1 Answers1

1

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
0x5453
  • 10,410
  • 1
  • 27
  • 49