#文章中各字母的出现次数
file_reading= open("essay.txt")
file = file_reading.read()
for abc in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz":
number = file.count(abc)
print(abc, ':', number, ' ', sep='', end='')
#a:299 b:65 c:69 d:153 e:468 f:119 g:86 h:196 i:265 j:10 k:21 l:189 m:105 n:227 o:318 p:47 q:2 r:225 s:196 t:329 u:73 v:47 w:76 x:2 y:73 z:1 A:9 B:2 C:5 D:0 E:1 F:4 G:8 H:1 I:15 J:1 K:0 L:13 M:6 N:8 O:4 P:3 Q:0 R:1 S:8 T:5 U:0 V:0 W:5 X:0 Y:2 Z:0
#文章中出现频率前三十的单词及次数
file_reading = open("essay.txt")
file = file_reading.read()
def comparelist(file):
zzr = {}
list_a = file.split()
set_a = set(list_a)
for i in set_a:
data_b = file.replace(i, "")
list_b= data_b.split()
number = len(list_a) - len(list_b)
zzr[i] = number
return freq
zzr = comparelist(file)
dictsort = sorted(zzr.items(), key=lambda d: d[1], reverse=True)[:30]
print(str(dictsort).replace("[", "").replace("]", "").replace("'", ""))
#(of, 52), (the, 48), (to, 28), (and, 24), (a, 21), (be, 20), (will, 17), (freedom, 14), (that, 13), (I, 13), (have, 13), (from, 12), (ring, 12), (in, 11), (dream, 11), (we, 10), (day, 10), (every, 9), (one, 9), (Let, 8), (with, 8), (is, 8), (able, 8), (together, 7), (back, 7), (go, 7), (Negro, 6), (today, 6), (this, 6), (by, 5)