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

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

on = '''When I was young I'd listen to the radio
Waiting for my favorite songs
When they played I'd sing along,
It make me smile.

Those were such happy times and not so long ago
How I wondered where they'd gone.
But they're back again just like a long lost friend
All the songs I love so well.
Every shalala every wo'wo
still shines.

Every shing-a-ling-a-ling that they're starting to sing
so fine

When they get to the part
where he's breaking her heart
It can really make me cry
just like before.
It's yesterday once more.
(Shoobie do lang lang)
(Shoobie do lang lang)
Looking bak on how it was in years gone by
And the good times that had
makes today seem rather sad,
So much has changed.
'''
on=on.lower()
print('所有大写转换为小写:'+on+'\n')

for i in ',.?!"':
    on=on.replace(i,' ')
print('所有分隔符替换为空格:'+on+'\n')

print('分割出所有单词:',on.split(' '),'\n')

print('统计单词i出现次数:',on.count('i'))

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

grade=list('12233232121')
print('作业评分列表:\n',grade)
grade.append('3')
print('增加一个评分3:\n',grade)
grade.pop(1)
print('删除下标为1的评分:\n',grade)
grade[3]='4'
print('改下标为3的评分为4:\n',grade)
grade.insert(5,'3')
print('在下标为5的评分插入评分3:\n',grade)
number=grade.index('3')
print('第一个3分的下标为:',number)
a=grade.count('1')
print('1分的同学有:',a,'')
a=grade.count('3')
print('3分的同学有:',a,'')

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

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

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

                   同:list和tuple是内置的有序集合,一个可变,一个不可变。

    

posted @ 2017-09-22 17:04  062许立帅  阅读(126)  评论(0)    收藏  举报