练习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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值