Skip to content

Commit faf6e42

Browse files
committed
Day7
1 parent c1fb63a commit faf6e42

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#字符串示例
2+
text = '''
3+
愚公移山
4+
太行,王屋二山的北面,住了一個九十歲的老翁,名叫愚公。二山佔地廣闊,擋住去路,使他和家人往來極為不便。
5+
一天,愚公召集家人說:「讓我們各盡其力,剷平二山,開條道路,直通豫州,你們認為怎樣?」
6+
大家都異口同聲贊成,只有他的妻子表示懷疑,並說:「你連開鑿一個小丘的力量都沒有,怎可能剷平太行、王屋二山呢?況且,鑿出的土石又丟到哪裏去呢?」
7+
大家都熱烈地說:「把土石丟進渤海裏。」
8+
於是愚公就和兒孫,一起開挖土,把土石搬運到渤海去。
9+
愚公的鄰居是個寡婦,有個兒子八歲也興致勃勃地走來幫忙。
10+
寒來暑往,他們要一年才能往返渤海一次。
11+
住在黃河河畔的智叟,看見他們這樣辛苦,取笑愚公說:「你不是很愚蠢嗎?你已一把年紀了,就是用盡你的氣力,也不能挖去山的一角呢?」
12+
愚公歎息道:「你有這樣的成見,是不會明白的。你比那寡婦的小兒子還不如呢!就算我死了,還有我的兒子,我的孫子,我的曾孫子,他們一直傳下去。而這二山是不會加大的,總有一天,我們會把它們剷平。」
13+
智叟聽了,無話可說:
14+
二山的守護神被愚公的堅毅精神嚇倒,便把此事奏知天帝。天帝佩服愚公的精神,就命兩位大力神揹走二山。
15+
How The Foolish Old Man Moved Mountains
16+
Yugong was a ninety-year-old man who lived at the north of two high mountains, Mount Taixing and Mount Wangwu.
17+
Stretching over a wide expanse of land, the mountains blocked yugong’s way making it inconvenient for him and his family to get around.
18+
One day yugong gathered his family together and said,”Let’s do our best to level these two mountains. We shall open a road that leads to Yuzhou. What do you think?”
19+
All but his wife agreed with him.
20+
“You don’t have the strength to cut even a small mound,” muttered his wife. “How on earth do you suppose you can level Mount Taixin and Mount Wanwu? Moreover, where will all the earth and rubble go?”
21+
“Dump them into the Sea of Bohai!” said everyone.
22+
So Yugong, his sons, and his grandsons started to break up rocks and remove the earth. They transported the earth and rubble to the Sea of Bohai.
23+
Now Yugong’s neighbour was a widow who had an only child eight years old. Evening the young boy offered his help eagerly.
24+
Summer went by and winter came. It took Yugong and his crew a full year to travel back and forth once.
25+
On the bank of the Yellow River dwelled an old man much respected for his wisdom. When he saw their back-breaking labour, he ridiculed Yugong saying,”Aren’t you foolish, my friend? You are very old now, and with whatever remains of your waning strength, you won’t be able to remove even a corner of the mountain.”
26+
Yugong uttered a sigh and said,”A biased person like you will never understand. You can’t even compare with the widow’s little boy!”
27+
“Even if I were dead, there will still be my children, my grandchildren, my great grandchildren, my great great grandchildren. They descendants will go on forever. But these mountains will not grow any taler. We shall level them one day!” he declared with confidence.
28+
The wise old man was totally silenced.
29+
When the guardian gods of the mountains saw how determined Yugong and his crew were, they were struck with fear and reported the incident to the Emperor of Heavens.
30+
Filled with admiration for Yugong, the Emperor of Heavens ordered two mighty gods to carry the mountains away.
31+
'''
32+
import stats_word #import用来导入模块的,它可以出现在程序中的任何位置,其中stats_word就是模块名字
33+
#调用stats_word中的stats_text函数统计中英文词频
34+
mdict={}
35+
mdict=stats_word.stats_text(text)
36+
print(mdict) #原顺序以字典形式输出
37+
print(sorted(mdict.items(), key=lambda d: d[1],reverse=True)) #排序后以列表形式输出
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
'''
24+
25+
#去除text里的标点符号
26+
s = ",.!-*" #将,.!-*赋值为变量s
27+
text = text.replace(s,'') #调用函数replace()将字符串text中的,.!-*替换为空格后生成新变量text
28+
29+
#创建一个名为stats_text_en的函数
30+
#使用字典(dict)统计字符串样本text中各个英文单词出现的次数
31+
import re #re模块提供正则表达式的匹配操作
32+
def stats_text_en(text): #定义函数 stats_text_en(),接收参数text
33+
result = re.sub("[^A-Za-z]", " ", text.strip())
34+
d = {}
35+
for x in result.split( ):
36+
if not x in d:
37+
d[x] = 1
38+
else:
39+
d[x] = d[x]+1
40+
return d
41+
42+
print(stats_text_en(text))
43+
frequency = stats_text_en(text)
44+
print("按照出现次数从大到小输出所有的单词及出现的次数")
45+
print (sorted(frequency.items(), key=lambda frequency: frequency[1],reverse=True))
46+
47+
48+
#创建一个名为stats_text_cn的函数,功能:统计每个中文汉字出现的次数
49+
import re #re模块提供各种正则表达式的匹配操作
50+
def stats_text_cn(text):
51+
'''统计每个中文汉字出现的次数'''
52+
result = re.findall(u'[\u4e00-\u9fff]+', text)#\u是unincode编码,u4e00是十六进制表达值
53+
rep = ''.join(result)
54+
resoult = {}
55+
for i in rep:
56+
resoult[i] = rep.count(i)
57+
return resoult
58+
stats_text_cn(text = "")
59+
print(stats_text_cn(text))
60+
frequency = stats_text_cn(text)
61+
print("按照字频降序排列")
62+
print (sorted(frequency.items(), key=lambda frequency: frequency[1],reverse=True))
63+
64+
65+
def stats_text(text):
66+
return dict(stats_text_en(text),**stats_text_cn(text))
67+
68+
def main():
69+
mdict={}
70+
mdict=stats_text(text)
71+
print(mdict)
72+
73+
if __name__=='__main__':
74+
main()

0 commit comments

Comments
 (0)