<分区>
在 python 中,如何遍历文本文件并计算每个字母出现的次数?我意识到我可以只使用“for x in file”语句来遍历它,然后设置 26 个左右的 if elif 语句,但肯定有更好的方法吗?
谢谢。
<分区>
在 python 中,如何遍历文本文件并计算每个字母出现的次数?我意识到我可以只使用“for x in file”语句来遍历它,然后设置 26 个左右的 if elif 语句,但肯定有更好的方法吗?
谢谢。
最佳答案
from collections import Counter
with open(file) as f:
c = Counter()
for line in f:
c += Counter(line)
如果文件不是很大,你可以把它作为一个字符串全部读入内存,并用一行代码将它转换成一个Counter
对象:
c = Counter(f.read())
例子:
>>> c = Counter()
>>> c += Counter('aaabbbcccddd eee fff ggg')
>>> c
Counter({'a': 3, ' ': 3, 'c': 3, 'b': 3, 'e': 3, 'd': 3, 'g': 3, 'f': 3})
>>> c += Counter('aaabbbccc')
Counter({'a': 6, 'c': 6, 'b': 6, ' ': 3, 'e': 3, 'd': 3, 'g': 3, 'f': 3})
或使用 count()
字符串的方法:
from string import ascii_lowercase # ascii_lowercase =='abcdefghijklmnopqrstuvwxyz'
with open(file) as f:
text = f.read().strip()
dic = {}
for x in ascii_lowercase:
dic[x] = text.count(x)
关于python - 统计文本文件中字母出现的频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12342207/