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

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

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)

结果为:

 

 

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

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

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

posted @ 2017-09-24 13:53  086黄向薇  阅读(84)  评论(0编辑  收藏  举报