Skip to content

Commit 32b2e16

Browse files
committed
Create 1001S02E05_stats_text.py
1 parent 88b0cbc commit 32b2e16

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

1901010091/1001S02E05_stats_text.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
text='''
2+
The Zen of Python, by Tim Peters
3+
Beautiful is better than ugly.
4+
Explicit is better than implicit.
5+
Simple is better than complex.
6+
Complex is better than complicated.
7+
Flat is better than nested.
8+
Sparse is better than dense.
9+
Readability counts.
10+
Special cases aren't special enough to break the rules.
11+
Although practicality beats purity.
12+
Errors should never pass silently.
13+
Unless explicitly silenced.
14+
In the face of ambxiguity, refuse the temptation to guess.
15+
There should be one-- and preferably only one --obvious way to do it.
16+
Although that way may not be obvious at first unless you're Dutch.
17+
Now is better than never.
18+
Although never is often better than *right* now.
19+
If the implementation is hard to explain, it's a bad idea.
20+
If the implementation is easy to explain, it may be a good idea.
21+
Namespaces are one honking great idea -- let's do more of those!
22+
'''
23+
d={}#新建字典集合
24+
t1=text.split()#将字符串转化为列表
25+
set1=set(t1)#将列表转为集合,去除列表中的重复字符
26+
t2=list(set1)#将集合转为没有重复字符的列表
27+
for x in range(len(t2)):#len函数能够返回列表的个数,x历遍列表中的元素
28+
d[t2[x]]=0#初始值为零
29+
for y in range(len(t1)):#y历遍t1
30+
if t2[x]==t1[y]:#如果x=y
31+
d[t2[x]] += 1#则x元素增加1
32+
print(d)
33+
34+
d1=sorted(d.items(),key=lambda item:item[1],reverse=True)#items()函数将字典转化为元组,key lambda item:item[1]表示选第二个元素做比较,reverse表示降序
35+
print(d1)
36+
37+
d2=str(d1)
38+
print(d2)
39+
40+
#清楚非单词字符
41+
word=[]
42+
s=',.*-!'
43+
for i in t1:
44+
for j in s:
45+
i=i.replace(j,'')
46+
if len(i):
47+
word.append(i)
48+
print(word)
49+
50+
#统计单词数
51+
counter={}
52+
w1=set(word)
53+
w2=list(w1)
54+
for a in range(len(w2)):
55+
counter[w2[a]]=0
56+
for b in range(len(word)):
57+
if w2[a]==word[b]:
58+
59+
counter[w2[a]]+=1
60+
print(counter)
61+
print('只包含英文单词的统计')
62+
counter1=sorted(counter.items(),key=lambda item:item[1],reverse=True)#items()函数将字典转化为元组,key lambda item:item[1]表示选第二个元素做比较,reverse表示降序
63+
print(counter1)
64+
65+
print('尝试教练的方法')
66+
for a in w2:
67+
counter[a]=word.count(a)#字典中count 函数能够统计元素的数量,通过等号赋值给元素
68+
print(counter)

0 commit comments

Comments
 (0)