2013-10-04 42 views

回答

3

您可以使用collections.Counter.update

>>> c = Counter('aaab') 
>>> c.update(Counter('babac')) 
>>> c 
Counter({'a': 5, 'b': 3, 'c': 1}) 

在Python 3.3,__iadd__被重寫:(http://hg.python.org/cpython/rev/5cced40374df

>>> sys.version 
'3.3.0 (default, Sep 29 2012, 17:14:58) \n[GCC 4.7.2]' 
>>> print(Counter.__iadd__.__doc__) 
Inplace add from another counter, keeping only positive counts. 

     >>> c = Counter('abbb') 
     >>> c += Counter('bcc') 
     >>> c 
     Counter({'b': 4, 'c': 2, 'a': 1})