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

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

s='''Met you by surprise I didn't realize 
That my life would change forever 
Saw you standing there 
I didn't know I cared,There was something special in the air 
Dreams are my reality   
The only kind of real fantasy,
illusions are a common thing   
I try to live in dreams 
It seems as it's meant to be 
Dreams are my reality!
A different kind of reality 
I dream of loving in the night   
And loving seems alright 
Although it's only fantasy 
If you do exist honey don't resist   
Show me a new way of loving   
Tell me that it's true 
show me what to do 
I feel something special about you 
Dreams are my reality 
The only kind of reality  
May be my foolishness is past 
And may be now at last 
I'll see how the real thing can be   
Dreams are my reality! '''
s=s.lower()
s=s.replace(',',' ')
s=s.replace('!',' ')
s=s.split(' ')
print("my出现的次数:",s.count("my"))
print("reality出现的次数:",s.count("reality"))
print("改变后的英文歌曲:",s)

截图:

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

ls=list('122312133')     
ls.append('3')
ls.insert(2,'1')
ls.pop(5)
print(ls)
        
print("3分的起始位置是",ls.index('3'))
print("得1分同学的个数:",ls.count("1"))
print("得3分同学的个数:",ls.count("3"))

 

截图:

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

同:都是有序的序列。

异:列表函数新增的元素,会改变列表本身。可以随时进行增删改操作。

       元组一旦初始化,不能进行增删改和排序的操作。

posted on 2017-09-22 14:51  083李笑晴  阅读(104)  评论(0)    收藏  举报

导航