datasets.py 493 B

123456789101112131415161718192021222324
  1. # coding=utf-8
  2. from collections import Counter
  3. from functools import reduce
  4. import operator
  5. def add_dicts(*args):
  6. """
  7. Adds two or more dicts together. Common keys will have their values added.
  8. For example::
  9. >>> t1 = {'a':1, 'b':2}
  10. >>> t2 = {'b':1, 'c':3}
  11. >>> t3 = {'d':4}
  12. >>> add_dicts(t1, t2, t3)
  13. {'a': 1, 'c': 3, 'b': 3, 'd': 4}
  14. """
  15. counters = [Counter(arg) for arg in args]
  16. return dict(reduce(operator.add, counters))