Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 KB | None | 0 0
  1. #!/usr/bin/python
  2. # classic reducer for wordcount
  3. # wyu@ateneo.edu
  4. import sys
  5.  
  6. last_key = None
  7. running_total = 0
  8.  
  9. for input_line in sys.stdin:
  10.    input_line = input_line.strip()
  11.    this_key, value = input_line.split("\t", 1)
  12.    value = int(value)
  13.  
  14.    if last_key == this_key:
  15.        running_total += value
  16.    else:
  17.        if last_key:
  18.            print( "%s\t%d" % (last_key, running_total) )
  19.        running_total = value
  20.        last_key = this_key
  21.  
  22. if last_key == this_key:
  23.    print( "%s\t%d" % (last_key, running_total) )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement