Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. #!/usr/bin/python
  2. import sys
  3. from operator import itemgetter
  4. # using a dictionary to map words to their counts
  5. current_word = None
  6. current_count = 0
  7. word = None
  8. # input comes from STDIN
  9. for line in sys.stdin:
  10. line = line.strip()
  11. word, count = line.split('\t', 1)
  12. try:
  13. count = int(count)
  14. except ValueError:
  15. continue
  16. if current_word == word:
  17. current_count += count
  18. else:
  19. if current_word:
  20. print '%s\t%s' % (current_word, current_count)
  21. current_count = count
  22. current_word = word
  23. if current_word == word:
  24. print '%s\t%s' % (current_word, current_count)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement