组合数据类型练习,英文词频统计实例

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

hxh=list('24425464315331')
hxh.count('3')
hxh.count('1')
hxh.append('1')
hxh.pop(2)
hxh[1]='2'
hxh.insert(1,3)
hxh.index('2')
len(hxh)
max(hxh)
min(hxh)

2.字典实例:建立学生学号成绩字典,做增删改查遍历操作。

d={'1':'88','2':'70','3':'90','4':'60','5':'79'}
ls[4]
d['3']
d.pop('3')
d.keys()
d.values()
d.items()
d.get('4')
d.get('3','不知道')

3.列表,元组,字典,集合的遍历。
总结列表,元组,字典,集合的联系与区别。
列表:

hxh=list('24425464315331')
hxh.count('3')
hxh.count('1')
hxh.append('1')
hxh.pop(2)
hxh[1]='2'
hxh.insert(1,3)
hxh.index('2')
len(hxh)
max(hxh)
min(hxh)

元组:

t=('a','b',['A','B'])
t[2][0]='X'
t[2][1]='Y'

字典

d={'Micheal':95,'Bob':75,'Tracy':85}
d['Micheal']
'Thomas' in d
d.pop('Bob')

集合

s=set([1,2,3])
s=set([1,1,2,2,3,3])
s.add(4)
s.remove(4)
s1=set([1,2,3])
s2=set([2,3,4])
s1&s2
s1|s2

        列表是可变对象,它支持在原处修改的操作.也可以通过指定的索引和分片获取元素。区别于元组,可动态增加,删除,更新。    

       元组和列表在结构上没有什么区别,唯一的差异在于元组是只读的,不能修改。元组用“()”表示。元组一旦定义其长度和内容都是固定的。一旦创建元组,则这个元组就不能被修改,即不能对元组进行更新、增加、删除操作。若想创建包含一个元素的元组,则必须在该元素后面加逗号“,”,否则创建的不是一个元组,而是一个字符串。    

        集合就是我们数学学的集合,没有什么特殊的定义。集合最好的应用是去重。所以,集合内的元素没有重复的元素。集合没有特殊的表示方法,而是通过一个set函数转换成集合。集合是一个无序不重复元素集,基本功能包括关系测试和消除重复元素.。  

        字典存储键值对数据。字典最外面用大括号,每一组用冒号连起来,然后各组用逗号隔开。字典最大的价值是查询,通过键,查找值。字典和集合都是无序的。

4.英文词频统计实例

song='''
Someone Like You - Adele
I heard that you settled down
我听说你已心有所属
That you found a girl and your married now
你找到了一个合适的女孩并和她结了婚
I heard that your dreams came true
我听说你已梦想成真
Guess she gave you things I didn't give to you
我猜,她给了你我所未能给予的
Old friend why are you so shy
老朋友,你害什么羞呢
It ain't like you to hold back or hide from the lie
不像你啊,遮遮掩掩的
I hate to turn up out of the blue uninvited
我恨她代替了原本属于我的位置
But I couldn't stay away I couldn't fight it
但是我没办法离开,我无法释怀
I'd hoped you'd see my face & that you'd be reminded
我曾多么希望你再看看我并提醒你
That for me it isn't over
并提醒你,对我来说,一切都还未结束
Nevermind I'll find someone like you
无所谓,我会找到一个人像你一样
I wish nothing but the best for you too
我别无所求,只希望你能过得好
Don't forget me I beg I remember you said -
求求你,不要忘记我我记得你说过
Sometimes it lasts in love but sometimes it hurts instead
有时爱情可以很永久但有时也会如此伤人
Sometimes it lasts in love but sometimes it hurts instead yeah
有时爱情可以很永久但有时也会如此伤人 是啊
You'd know how the time flies
你知道时光飞逝
Only yesterday was the time of our lives
仿佛我们还在一起
We were born and raised in a summery haze
我的爱在夏日的烟雾中萌芽
Bound by the surprise of our glory days
充满着惊喜和喜悦
I hate to turn up out of the blue uninvited
我恨她代替了原本属于我的位置
But I couldn't stay away I couldn't fight it
但是我没办法离开,我无法释怀
I'd hoped you'd see my face & that you'd be reminded
我曾多么希望你再看看我并提醒你
That for me it isn't over yet
对我来说,一切都还未结束
Nevermind I'll find someone like you
无所谓,我会找到一个人像你一样
I wish nothing but the best for you too
我别无所求,只希望你能过得好
Don't forget me I beg I remember you said -
求求你,不要忘记我我记得你说过
Sometimes it lasts in love but sometimes it hurts instead
有时爱情可以很永久但有时也会如此伤人
Nothing compares no worries or cares
没什么可与它相比不必担心 不必牵挂
Regret's and mistakes they're memories made
我们曾经的爱情里充满了遗憾和误解
Who would have known how bittersweet this would taste
又有谁能体会着中间的苦与甜
Nevermind I'll find someone like you
无所谓,我会找到一个人像你一样
I wish nothing but the best for you too
我别无所求,只希望你能过得好
Don't forget me I beg I remembered you said -
求求你,不要忘记我我记得你说过
Sometimes it lasts in love but sometimes it hurts instead
有时爱情可以很永久但有时也会如此伤人
Nevermind I'll find someone like you
无所谓,我会找到一个人像你一样
I wish nothing but the best for you too
我别无所求,只希望你能过得好
Don't forget me I beg I remembered you said -
求求你,不要忘记我我记得你说过
Sometimes it lasts in love but sometimes it hurts instead
有时爱情可以很永久但有时也会如此伤人
Sometimes it lasts in love but sometimes it hurts instead yeah yeah
有时爱情可以很永久但有时也会如此伤人
-
'''
print(song.count("someone"))
print(song.replace(',',' '))
print(song.replace('.',' '))
print(song.replace('?',' '))
print(song.replace('!',' '))
print(song.lower())
words = song.split(' ')
dic={}
keys=set(words)
for i in keys:
    dic[i]=words.count(i)
print(dic)
wc=list(dic.items())
wc.sort(key=lambda x:x[1],reverse=True)
for i in range(10):
    print(wc[i])

 

posted @ 2017-09-20 11:34  21黄玺恒  阅读(187)  评论(1编辑  收藏  举报