Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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_Python 3.x_List_Dictionary - Fatal编程技术网

如何从列表中返回字典,其中键是列表中的项,值是python中该列表中出现的次数?

如何从列表中返回字典,其中键是列表中的项,值是python中该列表中出现的次数?,python,python-3.x,list,dictionary,Python,Python 3.x,List,Dictionary,从列表创建字典,其中列表项成为键,值成为它们在列表中的次数。计数器将值序列转换为类似defaultdict(int)的对象,将键映射到计数 d = {} for items in your_list: d[items] = your_list.count(items) 从集合导入计数器 c=计数器([0,1,2,0])#c(基本上)是{0:2,1:1,2:1} 计数器的一种有用方法是最常用的 # print the 10 most common words and their

从列表创建字典,其中列表项成为键,值成为它们在列表中的次数。

计数器将值序列转换为类似defaultdict(int)的对象,将键映射到计数

 d = {}

 for items in your_list:
     d[items] = your_list.count(items)
从集合导入计数器
c=计数器([0,1,2,0])#c(基本上)是{0:2,1:1,2:1}
计数器的一种有用方法是
最常用的

# print the 10 most common words and their counts in the Counter Dict on myList:
for word, count in Counter(myList).most_common(10):
    print(word, count)