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

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

a='''Twinkle, twinkle, little star,
How I wonder what you are!
Up above the world so high,
Like a diamond in the sky.
Twinkle, twinkle, little star,
How I wonder what you are!

When the blazing sun is gone,
When he nothing shines upon,
Then you show your little light,
Twinkle, twinkle, all the night.
Twinkle, twinkle, little star,
How I wonder what you are!

Then the traveler in the dark
Thanks you for your tiny spark;
He could not see which way to go,
If you did not twinkle so.
Twinkle, twinkle, little star,
How I wonder what you are!

In the dark blue sky you keep,
And often through my curtains peep,
For you never shut your eye
Till the sun is in the sky.
Twinkle, twinkle, little star,
How I wonder what you are!

As your bright and tiny spark
Lights the traveler in the dark,
Through I know not what you are,
Twinkle, twinkle, little star.
Twinkle, twinkle, little star,
How I wonder what you are!'''
a=a.lower()
for i in ',.!?':
a=a.replace(i,' ')
words=a.split(' ')
print(words)

 

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

li=list('311123213332')
print(li)
for i in range(len(li)):
    li[i]=int(li[i])
print(li)
#排序
li.sort()
print(li)
#做增加一个1分的
li.append('1')
print(li)
#做删除第4个
li.pop(3)
print(li)
#做修改第2个
li[1]='2'
print(li)
#做查询第3个
print(li[2])
#计算有多少个一分的
print(li.count(1))
#计算有多少个三分的
print(li.count(3))

 

 

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

在列表中,你可以添加、删除或是搜索列表中的项目。
由于你可以增加或删除项目,所以列表是可变的数据类型,
即这种类型是可以被改变的。

元组和列表十分类似,但是元组是不可变的.
也就是说你不能修改元组。
元组通常用在使语句或用户定义的函数能够安全地采用一组值的时候,
即被使用的元组的值不会改变。

posted @ 2017-09-20 20:28  013洪辉  阅读(99)  评论(0编辑  收藏  举报