Guest User

Untitled

a guest
Dec 2nd, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. new = ['coin', 'coin', 'apple', 'axe', 'sword']
  2. items = {'coin': 43, 'apple': 5}
  3.  
  4. for x in new:
  5. if x in items:
  6. items[x] += 1
  7. else:
  8. items[x] = 0
  9.  
  10. print(items)
  11.  
  12. #{'sword': 0, 'coin': 45, 'apple': 6, 'axe': 0}
  13.  
  14. items = {'coin': 43, 'apple': 5}
  15. new = ['coin', 'coin', 'apple', 'axe', 'sword']
  16.  
  17. from collections import Counter
  18. def add_to_dict(x: dict, y: list):
  19. for k, v in Counter(y).items():
  20. try:
  21. x[k] += v
  22. except KeyError:
  23. x[k] = v
  24. add_to_dict(items, new)
  25. print(items)
Add Comment
Please, Sign In to add comment