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

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

song='''Take my hand 
Why are we 
strangers when 
Our love is strong 
Why carry on without me 
Everytime I try to fly 
I fall without my wings 
I feel so small 
I guess I need you baby 
And everytime I see you in my dreams 
I see your face 
It s haunting me 
I guess I need you baby 
I make believe that you are here 
It s the only way I see clear 
What have I done 
You seem to move on easy 
Everytime I try to fly 
I fall without my wings 
I feel so small 
I guess I need you baby 
And everytime I see you in my dreams 
I see your face 
You re haunting me 
I guess I need you baby 
I may have made it rain 
Please forgive me 
My weakness caused your pain 
And this song is my sorry 
At night I pray 
that soon your face will fade away 
Everytime I try to fly 
I fall without my wings 
I feel so small 
I guess I need you baby 
And everytime I see you in my dreams 
I see your face 
You re haunting me 
I guess I need you baby '''

song=song.lower()
song=song.replace(',',' ')
song=song.replace('.',' ')
song=song.replace('-',' ')
print('me出现的次数:',song.count('me'))
print('baby 出现的次数:',song.count('baby'))

newsong=song.split(' ')
print('歌曲分隔出一个一个的单词:',newsong)

 

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

grade=list('123112332213')
print('列表:',grade)
print('列表长度:',len(grade))

grade=[int(x) for x in grade]
print('更改为数值型:',grade)

grade.append(5)
grade.insert(4,6)
print('增加后列表:',grade)
grade.pop()
grade.pop(3)
print('删除后列表:',grade)

print('第一个3分的下标:',grade.index(3))
print('1分的同学人数:',grade.count(1))
print('3分的同学人数:',grade.count(3))

 

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

相同:列表和元组是内置的有序集合,列表与元组都是容器,是一系列的对象。

不同:列表可以随时添加和删除其中的元素,元组可读取里面的元素,但是不能改变其中的元素。列表有一个 append() 的方法来添加更多的元素,而元组却没有这个方法。

posted on 2017-09-22 14:37  104鲍珊珊  阅读(129)  评论(0编辑  收藏  举报

导航