python统计字母的频数和频率_科学网—Python统计字母频数和频率 - 吕波的博文

方案一 统计字符串中的字母频数

import collections

import re

d = collections.defaultdict(int)

S = "testTypecopyri4g3HACBCtcFor the ginpraise.ll be dog ssditsl-+-___*&^%icense()ation."

#convert to lower case

s = S.lower()

#match non-letter chars

rule = re.compile(r"[^a-z]")

#delete non-letter chars

s_result = rule.sub("", s)

#calculate the couter

for c in s_result:

d[c] += 1

print d

#rank according to the letter

print sorted(d.iteritems(), key = lambda d:d[0])

#rank according to the counter

print sorted(d.iteritems(), key = lambda d:d[1])

方案二 统计字符串中的字母频率

import collections

import re

sum = 0

d = collections.defaultdict(float)

S = "testTypecopyri4g3HACBCtcFor the ginpraise.ll be dog ssditsl-+-___*&^%icense()ation."

#convert to lower case

s = S.lower()

#match non-letter chars

rule = re.compile(r"[^a-z]")

#delete non-letter chars

s_result = rule.sub("", s)

#calculate the couter

for c in s_result:

d[c] += 1

#calculate the total number of letters

sum += 1

print "The total number of letters is: %d"%sum

#calculate the frequency

for c in d:

d[c] = d[c]/sum

d[c] = round(d[c], 10)

print d

#rank according to the letter

print sorted(d.iteritems(), key = lambda d:d[0])

print sorted(d.iteritems(), key = lambda d:d[0], reverse = True)

#rank according to the counter

print sorted(d.iteritems(), key = lambda d:d[1])

print sorted(d.iteritems(), key = lambda d:d[1], reverse = True)

方案三 统计文本文件中的字母频数

import collections

import re

d = collections.defaultdict(int)

#open the txt file

myfile = open('test.txt')

for line in myfile:

#delete the "n" at the end of the line

line = line.rstrip('n')

#convert to lower case

line = line.lower()

#match non-letter chars

rule = re.compile(r"[^a-z]")

#delete non-letter chars

line_result = rule.sub("", line)

#calculate the couter

for c in line_result:

d[c] += 1

myfile.close()

print d

#rank according to the letter

print sorted(d.iteritems(), key = lambda d:d[0])

#rank according to the counter

print sorted(d.iteritems(), key = lambda d:d[1])

方案四 统计文本文件中的字母频率

import collections

import re

sum = 0

d = collections.defaultdict(float)

#open the txt file

myfile = open('test.txt')

for line in myfile:

#delete the "n" at the end of the line

line = line.rstrip('n')

#convert to lower case

line = line.lower()

#match non-letter chars

rule = re.compile(r"[^a-z]")

#delete non-letter chars

line_result = rule.sub("", line)

#calculate the couter

for letter_x in line_result:

d[letter_x] += 1

sum += 1

myfile.close()

#calculate the total number of letters

print "The total number of letters is: %d"%sum

#calculate the frequency

for c in d:

d[c] = d[c]/sum

d[c] = round(d[c], 10)

print d

#rank according to the letter

print sorted(d.iteritems(), key = lambda d:d[0])

print sorted(d.iteritems(), key = lambda d:d[0], reverse = True)

#rank according to the counter

print sorted(d.iteritems(), key = lambda d:d[1])

print sorted(d.iteritems(), key = lambda d:d[1], reverse = True)

转载本文请联系原作者获取授权,同时请注明本文来自吕波科学网博客。

链接地址:http://blog.sciencenet.cn/blog-645111-1012606.html

上一篇:Protein Quantification

下一篇:Python统计单词频数


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部