您可以将传递
Counter给
dict:
counter = collections.Counter(...)counter = dict(counter)
In [56]: import collectionsIn [57]: counter = collections.Counter(['Foo']*12)In [58]: counterOut[58]: Counter({'Foo': 12})In [59]: counter = dict(counter)In [60]: counterOut[60]: {'Foo': 12}不过,我更喜欢JBernardo的想法:
In [66]: import jsonIn [67]: counterOut[67]: Counter({'Foo': 12})In [68]: json.dumps(counter)Out[68]: '{"Foo": 12}'这样,您就不会丢失
counter的特殊方法,例如
most_common,并且在Python根据构造字典时不需要额外的临时内存
Counter。



