0004题--任一个英文的纯文本文件,统计其中的单词出现的个数.

谁说我不能喝 提交于 2020-01-26 04:00:38

今天练习了一个简单的项目,后面有我自己在网上找的一段文章用作练习。
0004题–任一个英文的纯文本文件,统计其中的单词出现的个数.

import sys
x=input("请输入你查找的单词")
txt1=open(r'D:\python练习内容\learning1.txt')
txt2=txt1.read()

txt_list=txt2.split(' ')

a=0
i=0
for i in range(len(txt_list)):
    if txt_list[i] == x:
        a += 1
print(x,"在文章中出现的次数为:")
print(a)

我所使用的文章是这一段:
Hooray! It’s snowing! It’s time to make a snowman.James runs out. He makes a big pile of snow. He puts a big snowball on top. He adds a scarf and a hat. He adds an orange for the nose. He adds coal for the eyes and buttons.In the evening, James opens the door. What does he see? The snowman is moving! James invites him in. The snowman has never been inside a house. He says hello to the cat. He plays with paper towels.A moment later, the snowman takes James’s hand and goes out.They go up up up into the air! They are flying! What a wonderful night!The next morning, James jumps out of bed. He runs to the door.He wants to thank the snowman. But he’s gone.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!