问题描述
我正在尝试编写一个可以输入随机句子的代码,并计算字母在此字符串中返回的次数:
I am trying to make a code where I can input a random sentence, and count the frequency of the times a letter returns in this string:
def getfreq(lines):
""" calculate a list with letter frequencies
lines - list of lines (character strings)
both lower and upper case characters are counted.
"""
totals = 26*[0]
chars = []
for line in lines:
for ch in line:
chars.append(totals)
return totals
# convert totals to frequency
freqlst = []
grandtotal = sum(totals)
for total in totals:
freq = totals.count(chars)
freqlst.append(freq)
return freqlst
到目前为止,我已经实现了将输入的每个字母追加到列表(字符)中.但是现在我需要一种方法来计算角色在该列表中返回的次数,并以此频率来表示.
So far I have achieved to append each letter of the input in the list (chars). But now I need a way to count the amount of times a character returns in that list, and express this in a frequency.
推荐答案
有一个非常方便的功能,collections模块中的rel ="nofollow noreferrer"> Counter
,它将计算序列中对象的频率:
There's a very handy function, Counter
, within the collections
module which will compute the frequency of objects within a sequence:
import collections
collections.Counter('A long sentence may contain repeated letters')
将产生:
Counter({' ': 6,
'A': 1,
'a': 3,
'c': 2,
'd': 1,
'e': 8,
'g': 1,
'i': 1,
'l': 2,
'm': 1,
'n': 5,
'o': 2,
'p': 1,
'r': 2,
's': 2,
't': 5,
'y': 1})
在您的情况下,您可能需要连接行,例如在进入Counter
之前使用''.join(lines)
.
In your case, you might want to concatenate your lines, e.g. using ''.join(lines)
before passing into the Counter
.
如果您想使用原始字典获得相似的结果,则可能需要执行以下操作:
If you want to achieve a similar result using raw dictionaries, you might want to do something like the following:
counts = {}
for c in my_string:
counts[c] = counts.get(c, 0) + 1
这取决于您的Python版本,可能会比较慢,但是会使用dict
的.get()
方法返回现有计数或默认值,然后再增加字符串中每个字符的计数.
Depending on your version of Python, this may be slower, but uses the .get()
method of dict
to either return an existing count or a default value before incrementing the count for each character in your string.
这篇关于获取句子中字母的频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!