码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-09-22 23:53:08      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:long   i know   es2017   评分   lower   tin   分享   span   sway   


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

str=‘‘‘Just one last dance
The wine and the lights and the Spanish guitar
I‘ll never forget how romantic they are
But I know‘ tomorrow I‘ll lose the one I love
There‘s no way to come with you.
It‘s the only thing to do
Just one last dance
Before we say goodbye
When we sway and turn round and round and round
It‘s like the first time
Just one more chance
Hold me tight and keep me warm
Cause the night is getting cold
And I don‘t know where I belong
Just one last dance
Oh‘Baby
Just one last dance
Before we say goodbye
When we sway and turn round and round and round
It‘s like the first time
Just one more chance
Hold me tight and keep me warm‘‘‘
#将所有大写转换为小写#
str=str.lower()print(全部转换为小写的结果:+str+\n)
#将所有分隔符(,.?!)替换为空格
for i in ,.?!:
    str=str.replace(i, )print(分隔符替换为空格的结果:+str+\n)
#统计单词‘just’出现的次数
count=str.count(just)print(单词just出现的次数为:,count)
#分隔出一个一个单词
str=str.split( )print(分隔结果为:,str)
 
 

技术分享

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

str=list(1223211231212123)
print(作业评分表:,str)

a=str.index(3)
print(第一个3分的下标为:,a)

one=str.count(1)
print(1分的同学有:,one)

three=str.count(3)
print(3分的同学有:,three)

str.sort()
print(排序为:,str)

技术分享

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

list是处理一组有序项目的数据结构,即可以在一个列表中存储一个序列的项目。列表中的项目。列表中的项目应该包括在方括号中一旦创建了一个列表,可以添加,删除,或者是搜索列表中的项目。列表是可变的数据类型,即这种类型是可以被改变的。

元组是不可变的,即不能修改元组。元组通过圆括号中用逗号分隔的项目定义。元组通常用在使语句或用户定义的函数能够安全的采用一组值的时候,即被使用的元组的值不会改变.

元组,列表可以嵌套。

 

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

标签:long   i know   es2017   评分   lower   tin   分享   span   sway   

原文地址:http://www.cnblogs.com/1244581939cls/p/7577269.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!