SHARE
TWEET

Untitled

a guest Dec 13th, 2017 139 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def make_comparator(less_than):
  2.     def compare(x, y):
  3.         if less_than(x, y):
  4.             return -1
  5.         elif less_than(y, x):
  6.             return 1
  7.         else:
  8.             return 0
  9.     return compare
  10.  
  11. def cmp_(x, y):
  12.     return x[0] > y[0] or (x[0] == y[0] and x[1] < y[1])
  13.  
  14. words_dict = dict()
  15.  
  16. for i in range(int(input())):
  17.     for word in input().split():
  18.         words_dict[word] = words_dict.get(word, 0) + 1
  19.  
  20. print([(v, k) for k, v in words_dict.items() ])
  21.  
  22. print(sorted([(v, k) for k, v in words_dict.items() ], cmp = make_comparator(cmp_)))
  23.  
  24.  
  25. """
  26. 2
  27. asd asd
  28. qwe q
  29. [(2, 'asd'), (1, 'qwe'), (1, 'q')]
  30. Traceback (most recent call last):
  31.  File "c:/Users/Asus/AppData/Local/Programs/Python/Python36/456.py", line 23, in <module>
  32.    print(sorted([(v, k) for k, v in words_dict.items() ], cmp = make_comparator(cmp_)))
  33. TypeError: 'cmp' is an invalid keyword argument for this function
  34. """
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top