Search code examples
pythonlinuxsortinguniq

Python similar uniq -c command?


Does python have a command similar to the linux command:

cat file.txt | sort -n | uniq -c

Where it sorts and calculates the frequency of a text file with integers on every new line, and will output in the form:

76539  1 
100441 2 
108637 3 
108874 4 
103580 5 
 91869 6 
 78458 7 
 61955 8 
 46100 9 
 32701 10 
 21111 11 
 13577 12 
  7747 13 
  4455 14 
  2309 15 
  1192 16 
   554 17 
   264 18 
   134 19 
    63 20 
    28 21 
    15 22 
    12 23 
     7 24 
     5 25

If not, can I just simply os.system(cat file.txt | sort -n | uniq -c) ?


Solution

  • import collections
    
    c = collections.Counter()
    
    with open('file.txt') as f:
        for text in f:
            c.update( [int(text.strip())] )
    
    c_sorted = sorted(c.most_common())
    
    for key, val in c_sorted:
        print val, key