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

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

时间:2017-09-24 14:30:15      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:技术   can   index   统计   词频统计   flow   har   att   下载   

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

str=‘‘‘It took a spark and my heart was energized.Electrocute like a hot live wire.
I looked away cause her eyes will hypnotize.An attitude that can start a fire.
Baby your love‘s a phenomena.Tower of love rising higher higher.Gotta g-get a thermometer.
Running a fever like I‘m on fire.Baby it‘s a miracle.Something more than physical.
Flow of attraction.Refraction tonight!Running with the devil.Hot like heavy metal.Power me like a BATTERY!
I‘m flying like an eagle.Sharper than a needle.Charge me up cause I need your love!
Let‘s get Higher Higher.Your love‘s like a Battery.Let‘s get Higher Higher.‘‘‘

for i in ‘,.?!‘:
    str=str.replace(i,‘ ‘)
print(‘将所有,.?!等替换为空格:‘,str,‘\n‘)

str=str.lower()
print(‘所有转换为小写:‘,str,‘\n‘)

count=str.count(‘higher‘)
print(‘单词higher出现的次数:‘,count,‘\n‘)

str=str.split(‘ ‘)
print(‘分隔单词:‘,str)

结果如图:

 

 

 

技术分享

 

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

str=list(‘1231221321223133‘)
print(str)

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

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

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

str[2]=‘1‘
print(‘修改后为:‘,str)

结果为:

 

技术分享

 

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

列表是一种有序的序列,可以随时添加和删除其中的元素。

元组是不可变的,不可以进行添加、删除或修改。

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

标签:技术   can   index   统计   词频统计   flow   har   att   下载   

原文地址:http://www.cnblogs.com/arashinoj/p/7587032.html

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