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

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

str='''Tyler was born infected with HIV: his mother was also infected. From the very beginning of his life,
he was dependent on medications to enable him to survive.When he was five,
he had a tube surgically inserted in a vein in his chest.This tube was connected to a pump,
which he carried in a small backpack on his back.
Medications were hooked up to this pump and were continuously supplied through this tube to his bloodstream.
At times, he also needed supplemented oxygen to support his breathing.'''

#将所有大写转换为小写
str=str.lower()
print('全部转换为小写的结果:'+str+'\n')

#将所有将所有其他做分隔符(,.?!)替换为空格
for i in ',.?!':
    str=str.replace(i,' ')
print('其他分隔符替换为空格的结果:'+str+'\n')

#统计单词‘was’出现的次数
count=str.count('was')
print('单词was出现的次数为:',count)

#分隔出一个一个单词
str=str.split(' ')
print('分隔结果为:',str)

 

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

str=list('1231212321323123')
print('作业评分列表:',str)

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

#查询1分的同学有多少个
one=str.count('1')
print('1分的同学有:',one)

#查询3分的同学有多少个
three=str.count('3')
print('3分的同学有:',three)

#追加一个2分的同学
str.append('2')
print('追加后的结果为:',str)

#修改下标为2的同学的分数为1
str[2]='1'
print('修改后的结果为:',str)

#在下标为5的位置插入一个分数为3的同学
str.insert(5,'3')
print('插入后的结果为:',str)

#删除最后一个同学的分数
str.pop()
print('删除后的结果为:',str)

#给评分列表排序
str.sort()
print('排序为:',str)

 

 

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

list是一种有序的序列,正向递增、反向递减序号,可以随时添加和删除其中的元素。没有长度限制、元素类型可以不同。

tuple和list非常类似,但是tuple一旦初始化就不能修改。

 

posted @ 2017-09-22 11:37  078刘凯敏  阅读(162)  评论(0编辑  收藏  举报