Guest User

Untitled

a guest
Dec 3rd, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 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. x[k] = x.get(k, 0) + v
  21. add_to_dict(items, new)
  22. print(items)
  23.  
  24. >>> from collections import Counter
  25. >>> Counter(items) + Counter(new)
  26. Counter({'coin': 45, 'apple': 6, 'axe': 1, 'sword': 1})
Add Comment
Please, Sign In to add comment