Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 制作计数器。最常用的返回字典_Python_Dictionary_Python Collections - Fatal编程技术网

Python 制作计数器。最常用的返回字典

Python 制作计数器。最常用的返回字典,python,dictionary,python-collections,Python,Dictionary,Python Collections,我使用了文档中的示例: >>> Counter('abracadabra').most_common(3) [('a', 5), ('r', 2), ('b', 2)] 如何使结果为: { 'a': 5, 'r' :2 , 'b' :2} 假设我们想保留计数器()。最常见的()code?最简单的方法就是简单地使用dict() 输出: {'a': 5, 'r': 2, 'b': 2} 将很容易做到这一点: >>> dict(Counter('abracad

我使用了文档中的示例:

>>> Counter('abracadabra').most_common(3)
[('a', 5), ('r', 2), ('b', 2)]
如何使结果为:

{ 'a': 5, 'r' :2 , 'b' :2}

假设我们想保留
计数器()。最常见的()
code?

最简单的方法就是简单地使用
dict()

输出:

{'a': 5, 'r': 2, 'b': 2}
将很容易做到这一点:

>>> dict(Counter('abracadabra').most_common(3))
{'a': 5, 'r': 2, 'b': 2}
>>>
以下是
帮助(dict)
返回的部分内容,供进一步参考:

>>> dict(Counter('abracadabra').most_common(3))
{'a': 5, 'r': 2, 'b': 2}
>>>
     dict(iterable) -> new dictionary initialized as if via:
 |      d = {}
 |      for k, v in iterable:
 |          d[k] = v