我有这个问题,它需要一个字典,并试图找到一个字母的每个实例,如果它存在并打印出来
get_letter_frequency('all animals are equal but some animals are more equal than others')
会打印:
a appears 10 times
b appears 1 time
e appears 7 times
h appears 2 times
i appears 2 times
l appears 6 times
m appears 4 times
n appears 3 times
o appears 3 times
q appears 2 times
r appears 4 times
s appears 4 times
t appears 3 times
u appears 3 times
到目前为止,我的get_letter_frequency函数
def get_letter_frequency(a_string):
dictionary = {}
words = a_string.split()
for letters in words:
if letters != " ":
if letters in dictionary:
dictionary[letters] += 1
else:
dictionary[letters] = 1
dictionary_keys = dictionary.keys()
new_list = list(dictionary_keys)
new_list.sort()
for alphabet in new_list:
if dictionary[alphabet] == 1:
print(alphabet, "appears", dictionary[alphabet], "time")
else:
print(alphabet, "appears", dictionary[alphabet], "times")
但这反过来给了我字典中的所有字母并告诉我它出现了多少次。
all appears 1 time
animals appears 2 times
are appears 2 times
but appears 1 time
equal appears 2 times
more appears 1 time
others appears 1 time
some appears 1 time
than appears 1 time
你能帮忙吗?谢谢。
答案 0 :(得分:3)
代码中的问题是:
for letters in words:
由于words
是单词列表,变量letters
将依次包含每个单词,而不是这些单词中的字母。
集合模块中的Counter类可以满足您的需求:
In [1]: from collections import Counter
In [2]: ctr = Counter('all animals are equal but some animals are more equal than others')
In [3]: list(ctr)
Out[3]: ['a', ' ', 'b', 'e', 'i', 'h', 'm', 'l', 'o', 'n', 'q', 's', 'r', 'u', 't']
In [4]: dict(ctr)
Out[4]:
{' ': 11,
'a': 10,
'b': 1,
'e': 7,
'h': 2,
'i': 2,
'l': 6,
'm': 4,
'n': 3,
'o': 3,
'q': 2,
'r': 4,
's': 4,
't': 3,
'u': 3}
如果必须使用for
循环并且没有模块执行此操作:
def get_letter_frequency(a_string):
dictionary = {}
for letter in a_string.replace(' ', ''):
dictionary[letter] = dictionary.get(letter, 0) + 1
for letter in sorted(dictionary.keys()):
print(letter, "appears", dictionary[letter], ["time", "times"][dictionary[letter]>1])
例如:
>>> get_letter_frequency('all animals are equal but some animals are more equal than others')
a appears 10 times
b appears 1 time
e appears 7 times
h appears 2 times
i appears 2 times
l appears 6 times
m appears 4 times
n appears 3 times
o appears 3 times
q appears 2 times
r appears 4 times
s appears 4 times
t appears 3 times
u appears 3 times
答案 1 :(得分:3)
错误在以下几行:
words = a_string.split()
for letters in words:
您期望它迭代words
中的所有字母,但它实际上遍历单个单词。在Python中,您可以使用字符串,就好像它们是list
个字符一样,因此将`for line更改为以下内容将起作用:
for letters in a_string:
答案 2 :(得分:1)
我认为使用collections.Counter
为您提供最优雅的解决方案,但如果您更喜欢使用自己的功能,请尝试
letters = list(a_string)
而不是str.split()
。
答案 3 :(得分:1)
假设你有这样的字符串(从你的例子中取出)
statement = 'all animals are equal but some animals are more equal than others'
characters = list(statement)
then
from collections import Counter
Counter(characters)
Output:
Counter({'a': 10,
'b': 1,
'e': 7,
'h': 2,
'i': 2,
'l': 6,
'm': 4,
'n': 3,
'o': 3,
'q': 2,
'r': 4,
's': 4,
't': 3,
'u': 3})
似乎所有的答案或多或少都在同一条线上:D
答案 4 :(得分:0)
标准库提供了这样的计数器:
>>> from collections import Counter
>>> words = "apple banana apple strawberry banana lemon cupcake battery staple"
>>> Counter(words)
Counter({' ': 8,
'a': 12,
'b': 4,
'c': 2,
'e': 7,
'k': 1,
'l': 4,
'm': 1,
'n': 5,
'o': 1,
'p': 6,
'r': 4,
's': 2,
't': 4,
'u': 1,
'w': 1,
'y': 2})
答案 5 :(得分:0)
words = a_string.split()
将字符串拆分为单词列表。然后循环遍历所有这些单词而不是字母。由于您似乎不关心空格" "
,您可以通过
no_whitespace_str = a_string.replace(" ", "")
for letters in no_whitespace_str:
...
较短的版本是
def get_letter_frequency(str):
occ = {v: str.count(v) for v in str.replace(" ","")}
for letter, num in sorted(occ.iteritems()):
print "{} appears {} times".format(letter, num)
答案 6 :(得分:0)
sent = "all animals are equal but some animals are more equal than others"
dic = {}
for ele in sent:
dic.setdefault(ele,0) += 1
这使得dic成为使用(print dic
)
{'a': 10, ' ': 11, 'b': 1, 'e': 7, 'i': 2, 'h': 2, 'm': 4, 'l': 6, 'o': 3, 'n': 3, 'q': 2, 's': 4, 'r': 4, 'u': 3, 't': 3}
在python string
中是list
,因此您可以迭代字符串中的单个字符