英文词频统计预备,组合数据类型练习

时间:2023-02-14 17:24:52

实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。

news=('''When I was young I'd listen to the radio
Waiting for my favorite songs
When they played I'd sing along,
It make me smile.

Those were such happy times and not so long ago
How I wondered where they'd gone.
But they're back again just like a long lost friend
All the songs I love so well.
Every shalala every wo'wo
still shines.

Every shing-a-ling-a-ling that they're starting to sing
so fine

When they get to the part
where he's breaking her heart
It can really make me cry
just like before.
It's yesterday once more.
(Shoobie do lang lang)
(Shoobie do lang lang)
Looking bak on how it was in years gone by
And the good times that had
makes today seem rather sad,
So much has changed.''')
news=news.lower()
for i in ',./':
news=news.replace(i,' ')
words=news.split(' ')
print(news)
print(news.count('lang'))
print(news.split(' '))

英文词频统计预备,组合数据类型练习

 

2、列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。

grade=list('12323112333')
print(grade)
for i in range(len(grade)):
grade[i]=int(grade[i])
print(grade)
print(grade.index(3))
print(grade.count(1))
print(grade.count(3))

英文词频统计预备,组合数据类型练习

3、简要描述列表与元组的异同。

列表中的项目应该包括在方括号中,
你可以添加、删除或是搜索列表中的项目。
由于你可以增加或删除项目,所以列表是可变的数据类型,
即这种类型是可以被改变的。

 

元组和列表十分类似,但是元组是不可变的.
也就是说你不能修改元组。
元组通过圆括号中用逗号分割的项目定义。
元组通常用在使语句或用户定义的函数能够安全地采用一组值的时候,
即被使用的元组的值不会改变。

    好文要顶 关注我 收藏该文 英文词频统计预备,组合数据类型练习