第八次作业

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第八次作业相关的知识,希望对你有一定的参考价值。

1.读入待分析的字符串

fo=open(test.txt,w)
n=fo.write(‘‘‘The driver, being a sensitive man as well as an animal lover, pulled over t
o the side of the road, and got out to see what had become of the Bunny carrying
the basket. Much to his dismay, the colorful Bunny was dead. The driver felt gui
lty and began to cry. A woman driving down the same highway saw the man crying on the side of the roa
d and pulled over. She stepped out of her car and asked the man what was wrong.
"I feel terrible," he explained, "I accidentally hit the Easter Bunny and kille
d it. There may not be an Easter because of me. What should I do?"The woman told the man not to worry. She knew exactly what to do. She went to
her car trunk, and pulled ut a spray can. She walked over to the limp,dead Bunny, and sprayed the entire contents of the can onto the little furryanimalMiraculously4 the Easter Bunny came to back life, jumped up, picked up
the spilled eggs and candy, waved its paw at the two humans and hopped5 on down the road. 50 yards away the Easter Bunny stopped, turned around,
waved and hopped on down the road another 50 yards, turned, waved, hopped anoth
er 50 yards and waved again!‘‘‘)
fo.close()

 

2.分解提取单词 

n=‘‘‘The driver, being a sensitive man as well as an animal lover, pulled over t
o the side of the road, and got out to see what had become of the Bunny carrying
the basket. Much to his dismay, the colorful Bunny was dead. The driver felt gui
lty and began to cry. A woman driving down the same highway saw the man crying on the side of the roa
d and pulled over. She stepped out of her car and asked the man what was wrong.
"I feel terrible," he explained, "I accidentally hit the Easter Bunny and kille
d it. There may not be an Easter because of me. What should I do?"The woman told the man not to worry. She knew exactly what to do. She went to
her car trunk, and pulled ut a spray can. She walked over to the limp,dead Bunny, and sprayed the entire contents of the can onto the little furryanimalMiraculously4 the Easter Bunny came to back life, jumped up, picked up
the spilled eggs and candy, waved its paw at the two humans and hopped5 on down the road. 50 yards away the Easter Bunny stopped, turned around,
waved and hopped on down the road another 50 yards, turned, waved, hopped anoth
er 50 yards and waved again!‘‘‘
n=n.lower()
for i in ‘,!""?‘:
    n=n.replace(i,‘ ‘)
words=n.split(‘ ‘)
print(words)

  

3.计数字典

dic={}
keys=set(words)
print(keys)
for i in keys:
     dic[i]=words.count(i)
print(dic)

  

4.排除语法型词汇

n=n.lower()
exc={‘the‘,‘a‘,‘as‘,‘of‘,‘I‘,‘an‘,‘had‘,‘to‘,‘‘}
for i in ‘,!""?‘:
    n=n.replace(i,‘ ‘)
words=n.split(‘ ‘)
print(words)
dic={}
keys=set(words)
keys=keys-exc
for i in keys:
     dic[i]=words.count(i)
print(dic)

  

5.排序

nc=list(dic.items())
nc.sort(key=lambda x:x[1],reverse=True)

  

6.输出TOP(20)

for i in range(20):
print(nc[i])

  

以上是关于第八次作业的主要内容,如果未能解决你的问题,请参考以下文章

第八次团队作业:汇总博客

第八次团队作业:汇总博客

第八次团队作业:代码规范

第八次团队作业:代码规范

第八次团队作业:代码规范

第八次团队作业:代码规范