Advertisement
Guest User

reducer

a guest
Jul 28th, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import sys
  4. import datetime
  5. import operator
  6.  
  7. oldKey = None
  8. counter = 0
  9. countarray = {}  # dictionary
  10. oldHrs = None
  11.  
  12.  
  13. for line in sys.stdin:
  14.     data = line.strip().split("\t")
  15.  
  16.     if len(data) != 2:
  17.         continue
  18.  
  19.     userid,newHrs = data # data that is  taken from the array
  20.  
  21.     if oldKey and oldKey != userid:  # on first pass, olkey is not going to equal user id
  22.  
  23.         countarray[newHrs] = 1
  24.         print userid,"\t", max(countarray.iterkeys(), key=lambda k: countarray[k])
  25.        
  26.     else:         # so we insert the hr (as the key) and a 1 for (the value)
  27.         # if oldkey is equal to userid, then
  28.         if newHrs in countarray:
  29.             countarray[newHrs] += 1
  30.             #countarray.update({newHrs:1})
  31.         else:
  32.             countarray[newHrs] = 1
  33.  
  34.  
  35.     oldKey = userid
  36.     oldHrs = newHrs
  37.    
  38.    
  39.     countarray = {} # then we rest the dictionary
  40.  
  41.  
  42. if oldKey != None:
  43.  
  44.    #print userid,"\t", max(countarray.iterkeys(), key=lambda k: countarray[k])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement