Python &引用;直方图“;功能:输入字符串和输出字典

Python &引用;直方图“;功能:输入字符串和输出字典,python,dictionary,histogram,Python,Dictionary,Histogram,我正在尝试创建一个直方图函数,该函数接受字符串并计算字符串被使用的次数,并将其放入字典中。我还在学习Python,所以任何提示都会很有帮助 >>> histogram('The Goose that Laid the Golden Egg') {'l': 2, 'n': 1, 'o': 3, 'h': 3, 'i': 1,'d': 2, 'e': 5, 'g': 4, ' ': 6, 'a': 2, 't': 4, 's': 1} 我不会为您解决这个问题,但会给您一个

我正在尝试创建一个
直方图
函数,该函数接受字符串并计算字符串被使用的次数,并将其放入字典中。我还在学习Python,所以任何提示都会很有帮助

>>> histogram('The Goose that Laid the Golden Egg') 
{'l': 2, 'n': 1, 'o': 3, 'h': 3, 'i': 1,'d': 2,   'e': 5, 'g': 4, ' ': 6, 'a': 2, 't': 4, 's': 1} 

我不会为您解决这个问题,但会给您一个提示:使用。将这一点与字符串是可编辑的这一事实结合起来,这将使您非常接近解决方案。

这就是它的用途:


不客气,
这个模块实现了专门的容器数据类型,为Python的通用内置容器、dict、list、set和tuple提供了替代方案。
它是Python数据结构中必不可少的模块!试着去学吧!
>>> from collections import Counter
>>> Counter('The Goose that Laid the Golden Egg')
Counter({' ': 6, 'e': 4, 'h': 3, 'o': 3, 't': 3, 'a': 2, 'd': 2, 'G': 2, 'g': 2, 'i': 1, 'L': 1, 'l': 1, 's': 1, 'T': 1, 'E': 1, 'n': 1})