練習004

第 0004 題:任一個英文的純文本文件,統計其中的單詞出現的個數。
純文本文件:Youth.txt

Youth Is Not a Time of Life
Youth is not a time of life;it is a state of mind.
It is not a matter of rosy cheeks,red lips and supple knees.
It is a matter of the will,a quality of the imagination,vigor of the emotions;
It is the freshness of the deep spring of life.
Youth means a temperamental predominance of courage over timidity.
Of the appetite for adventure over the love of ease.
This often exist in a man of 60.more than a boy of 20.
Nobady grows merely by the number of years,we grow old by deserting our ideas.
Years may wrinkle the skin,but to give up enthusiasm wrikles the soul.
Worry,fear,self-distrustl bows the heart and turns the spirit back to dust.
whether 60 or 160,there is in every human being's heart the lurt of wondert.
the unfailing childlike appetite of what's next and the joy of the game of living .
In the center of yout hear and my heart there is a wireless station;
So long as it receives message of beauty,hope,cheer,courage and power from men  and from infinite.
So long as you are young
When the aerials are down,and your spirit is covered with the snows of cynicism and the ice of pessimism.
Then you're grown old,even at 20,but as long as your aerials are up.
to catch waves of optimism,there's hope you may die young at 80.

程序:

#!/usr/bin 
import re
line_count=0
kong_count=0
word_list=[]
word_dit={}
list=[] 
f=open('Youth.txt','r')
for line in f:
    word = re.findall(r'[a-zA-Z\-]+',line)
    line_count += 1 
    kong_count += len(line)
    for i in word:
        if i not in word_dit:
            word_dit[i] = 1 
        else:
            word_dit[i] += 1 
f.close()
print line_count
print kong_count

for k,v in word_dit.items():
    list.append((k,v))
list.sort(key= lambda t:t[1])
print list[::-1]

( 寫於2016年4月25日,http://blog.csdn.net/bzd_111

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章