#函数defdo_something_with(l, dou):#统计单字母,双字母个数记录于字典a中
l =filter(str.isalpha, l.lower())#对字符串l,只提取字母,且规定为小写for i in l:#遍历单字母if(i in a):
a[i]= a[i]+1else:
a[i]=1#遍历双字母
dou = dou[1]+ i
if(dou[0]!=' '):if(dou in a):
a[dou]= a[dou]+1else:
a[dou]=1
a ={};
dou =' '
file_object =open('statistics.txt','r')try:for line in file_object:
do_something_with(line, dou)finally:
file_object.close()print(a)len(a)
import pandas as pd
df = pd.DataFrame(list(a.items()))
df.sort_index(axis =0,ascending =False,by =1)#output0112 e 11846126 t 798271 a 7424024 o 7168245 i 663016 n 6459830 s 594123 r 5610934 h 550068 d 44194.........603 éo 1639 èt 1610 ès 1625 jy 1337 xv 1630 mê 1631 êm 1336 sx 1638 lè 1662 cj 1663 rows × 2 columns
import matplotlib.pyplot as plt
df.plot(kind='barh', figsize=(10,6))
df.plot(kind='line')
plt.xlabel('number')# add to x-label to the plot
plt.ylabel('alpher')# add y-label to the plot
plt.title("statistics")# add title to the plot
plt.show()