如何从列表中返回字典,其中键是列表中的项,值是python中该列表中出现的次数?
从列表创建字典,其中列表项成为键,值成为它们在列表中的次数。计数器将值序列转换为类似defaultdict(int)的对象,将键映射到计数如何从列表中返回字典,其中键是列表中的项,值是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
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)