spacepaste

  1.  
  2. import collections
  3. import operator
  4. def compare(x, y):
  5. if x[1] > y[1]:
  6. return 1
  7. elif x[1] < y[1]:
  8. return -1
  9. elif x[1] == y[1]:
  10. if x > y:
  11. return 1
  12. elif x < y:
  13. return -1
  14. return 0
  15. document = "Practice makes perfect, you'll get perfecT by practice. just practice! just just just!!"
  16. words = document.split()
  17. d = collections.defaultdict(int)
  18. for word in words:
  19. word = word.lower()
  20. word = [c if c >= 'a' and c <= 'z' else "" for c in word]
  21. word = "".join(word)
  22. d[word] += 1
  23. output = []
  24. d = sorted(d.items(), cmp=compare, reverse=True)
  25. print(d)
  26. #getting: [('just', 4), ('practice', 3), ('perfect', 2), ('youll', 1), ('makes', 1), ('get', 1), ('by', 1)]
  27. #expected: [["just","4"],["practice","3"],["perfect","2"],["makes","1"],["youll","1"],["get","1"],["by","1"]]
  28.