Advertisement
Guest User

f

a guest
Dec 12th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3. import sys
  4.  
  5. last_key = None
  6. count = 0
  7. sum = 1
  8.  
  9. # keys come grouped together
  10. # so we need to keep track of state a little bit
  11. # thus when the key changes , we need to reset
  12. # our counter, and write out the count we've accumulated
  13.  
  14. for line in sys.stdin:
  15. line = line.strip()
  16. key, value = line.split("\t")
  17.  
  18. # we have to be able to deal with missing values
  19. if value =="NA":
  20. continue
  21.  
  22. value = int(value)
  23.  
  24. # if this is the first iteration
  25. if not last_key:
  26. last_key = key
  27. count = 1
  28. sum = value + 0.0
  29.  
  30. # if they're the same, log it
  31. elif key == last_key:
  32. count = count + 1
  33. sum = sum + value
  34.  
  35. else:
  36. result = [last_key, sum/count]
  37. print("\t".join(str(v) for v in result))
  38. last_key = key
  39. count = 1
  40. sum = value
  41.  
  42. # this is to catch the final value that we output
  43. print("\t".join(str(v) for v in [last_key, sum/count]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement