xyzg198891的个人博客分享 http://blog.sciencenet.cn/u/xyzg198891

博文

Python统计字母频数和频率

已有 9952 次阅读 2016-11-3 23:10 |个人分类: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)





https://blog.sciencenet.cn/blog-645111-1012606.html

上一篇:Protein Quantification
下一篇:Python统计单词频数
收藏 IP: 110.201.37.*| 热度|

1 sulailai123

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-12-26 03:12

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部