Riju21

9.1_std_lib_collection

Mar 27th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. from collections import Counter as cr
  2.  
  3. x = [1, 5, 'a', 'b', 'c', 1, 'b', 'a', 'c', 'c', 1, 1, 1]
  4. words = cr(x)  # which element how much time
  5.  
  6. #--------------------------------
  7.  
  8. c = cr()   # intialize counter
  9. c.update('xyzzzzxy')  # update c
  10.  
  11. #--------------------------------
  12.  
  13. c1 = cr('abcdaab')
  14. for l in 'abcde':
  15.     print('{} : {} '.format(l, c1[l]), end=' ')
  16.    
  17. print('\n')
  18. # element:
  19. # -------------
  20.  
  21. c2 = cr('extreme')
  22. c2['z'] = 0
  23. # print(c2)
  24. # print(list(c2.elements()))  # only key elements
  25.  
  26.  
  27. # get most common
  28. #---------------------
  29.  
  30. print('\n')
  31. dummyList = [1, 11, 1, 2, 4, 8, 'a', 'x', 'w',
  32.              5, 9, 'o', 'o', 10, 11, 11, 1, 1]
  33. c3 = cr()
  34. c3.update(dummyList)
  35. print(c3.most_common(3))   # most common words
Advertisement
Add Comment
Please, Sign In to add comment