Python中的字母频率

Python中的字母频率,第1张

Python中的字母频率

您可以使用翻译者配方来删除所有不在中的字符

alpha
。由于这样做使make
letters
中除了字符外仅包含其他字符
alpha
,因此它
n
是正确的分母。

然后,您可以使用a

collections.defaultdict(int)
来计数字母的出现:

import collectionsimport stringdef translator(frm='', to='', delete='', keep=None):    # Python Cookbook Recipe 1.9    # Chris Perkins, Raymond Hettinger    if len(to) == 1: to = to * len(frm)    trans = string.maketrans(frm, to)    if keep is not None:        allchars = string.maketrans('', '')        # delete is expanded to delete everything except        # what is mentioned in set(keep)-set(delete)        delete = allchars.translate(allchars, keep.translate(allchars, delete))    def translate(s):        return s.translate(trans, delete)    return translatealpha = 'abcdefghijklmnopqrstuvwxyz'keep_alpha=translator(keep=alpha)while True:    speech = raw_input("Enter file name:")    wholeFile = open(speech, 'r+').read()    lowlet = wholeFile.lower()    letters = keep_alpha(lowlet)    n = len(letters)    occurrences = collections.defaultdict(int)        for x in letters:        occurrences[x]+=1    for x in occurrences:        print x, occurrences[x], occurrences[x]/float(n)


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5617507.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-15
下一篇2022-12-15

发表评论

登录后才能评论

评论列表(0条)

    保存