elena1234

Counter , defaultdict in Python

Feb 11th, 2022 (edited)
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.35 KB | None | 0 0
  1. from collections import Counter, defaultdict
  2.  
  3. li = [1, 2, 4, 3, 4, 4, 5, 6, 6, 7, 8]
  4. print(Counter(li)) # Counter({4: 3, 6: 2, 1: 1, 2: 1, 3: 1, 5: 1, 7: 1, 8: 1})
  5.  
  6. dict = defaultdict(int, {'a': 1, 'b': 2, 'c': 4})
  7. print(dict['d']) # 0
  8.  
  9. dict2 = defaultdict(lambda: "value does not exist", {'a': 1, 'b': 2})
  10. print(dict2['d']) # value does not exist
  11.  
Add Comment
Please, Sign In to add comment