如何将Counter对象dict转换为字符串

时间:2019-11-17 23:28:32

标签: python string dictionary collections

Python中是否有一种方法可以将Counter字典转换为字符串?

我尝试过str(counterobj)方法,但是Counter(来自集合库)不支持此方法

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用此:

str(dict(Counter([1,3,4,5,6,4,334,4,4,4,3,2,5,4,2])))                                                                                                                                              
# '{1: 1, 3: 2, 4: 6, 5: 2, 6: 1, 334: 1, 2: 2}'