练习004

来源:互联网 发布:淘宝小样真假 编辑:程序博客网 时间:2024/05/11 19:42

第 0004 题:任一个英文的纯文本文件,统计其中的单词出现的个数。
纯文本文件:Youth.txt

Youth Is Not a Time of LifeYouth 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 youngWhen 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 reline_count=0kong_count=0word_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_countprint kong_countfor 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)

0 0
原创粉丝点击