>我有这个问题,它需要一个字典并尝试找到一个字母的每个实例(如果它存在)并打印出来
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
你能帮忙吗?谢谢。
错误位于以下行中:
words = a_string.split()
for letters in words:
你期望它迭代words
中的所有字母,但它实际上遍历单个单词。在 Python 中,你可以像使用list
个字符一样使用字符串,因此将 'for 行更改为以下内容将起作用:
for letters in a_string:
代码中的问题是:
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
使用collections.Counter
可以为您提供最优雅的解决方案,但如果您更喜欢使用自己的函数,请尝试
letters = list(a_string)
而不是str.split()
.
你有这样的字符串(取自你的例子)
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
标准库提供以下计数器:
>>> 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})
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)
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
,因此您可以迭代字符串中的单个字符