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

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

str='''Look how they shine for you,

And everything you do,

Yeah, they were all Yellow.
I came along,
I wrote a song for you,

And all the things you do,

And it was called Yellow.

So then I took my turn,

Oh what a thing to have done,

And it was all Yellow.

Your skin
Oh yeah, your skin and bones,
Turn into something beautiful,
You know, you know I love you so,

You know I love you so.

I swam across,
I jumped across for you,

Oh what a thing to do.

Cos you were all Yellow,
I drew a line,
I drew a line for you,

Oh what a thing to do,

And it was all Yellow.'''
str=str.lower()
for i in ',.':
str=str.replace(i,' ')
str=str.split(' ')
print(str)

 

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

lb = list('1324864532')
print(lb)
for i in range(len(lb)):
     lb[i]=int(lb[i])
print(lb.index(3))
print(lb.count(1))
print(lb.count(3))
lb.insert(1,9)
lb.pop(2)
lb.sort()
print(lb)

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

列表是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目。

列表中的项目。列表中的项目应该包括在方括号中,由于你可以增加或删除项目,我们说列表是可变的数据类型,即

这种类型是可以被改变的。列表是可以嵌套的。

元组和列表十分相似,不过元组是不可变的。即你不能修改元组。元组通过圆括号中用逗号分隔的项目定义。

元组也是可以嵌套的。

posted @ 2017-09-20 20:51  yishhaoo  阅读(135)  评论(0编辑  收藏  举报