Can you make Counter not write-out “Counter”?

后端 未结 4 1717
渐次进展
渐次进展 2021-01-15 13:22

So when I print the Counter (from collections import Counter) to a file I always get this the literal Counter ({\'Foo\': 12})

Is there anyw

4条回答
  •  终归单人心
    2021-01-15 13:47

    What about explicitly formatting it into the form that you want?

    >>> import collections
    >>> data = [1, 2, 3, 3, 2, 1, 1, 1, 10, 0]
    >>> c = collections.Counter(data)
    >>> '{' + ','.join("'{}':{}".format(k, v) for k, v in c.iteritems()) + '}'
    "{'0':1,'1':4,'2':2,'3':2,'10':1}"
    

提交回复
热议问题