|||
方案一 统计字符串中的字母频数
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)
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-12-26 03:12
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社