Advertisement
Guest User

lesson 3 reducer

a guest
Sep 19th, 2014
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import sys
  4. import re
  5. import csv
  6.  
  7. reader = csv.reader(sys.stdin, delimiter='\t')
  8. writer = csv.writer(sys.stdout, delimiter='\t', quotechar='"', quoting=csv.QUOTE_ALL)
  9.  
  10. '''
  11. The reducer is to tabulate the counts for each path and return the path with the highest count
  12. '''
  13. path={}
  14.  
  15. for line in reader:
  16.  
  17.         #_path, _count = line.strip().split("\t")
  18.         _path, _count = line
  19.  
  20.         '''
  21.        Tabulate the counts for each path
  22.        '''
  23.         if path.has_key(_path):
  24.                 path[_path]=path[_path]+int(_count)
  25.         else:
  26.                 path[_path]=int(_count)
  27.  
  28.  
  29.  
  30. '''
  31. Return the path with the highest count
  32. '''
  33. maxValue=max(path.values())
  34. for key in path:
  35.         if path[key]==maxValue:
  36.                 writer.writerow([key,str(maxValue)])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement