组合数据类型练习,英文词频统计实例上
Posted yushanshan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了组合数据类型练习,英文词频统计实例上相关的知识,希望对你有一定的参考价值。
1 字典实例:建立学生学号成绩字典,做增删改查遍历操作
a={\'01\':100,\'02\':89,\'03\':88,\'04\':90}
print(\'字典\',a)
a[\'05\']=88
print(\'增加05学生成绩为88\',a)
a.pop(\'02\')
print(\'删除02的学生成绩\',a)
print(\'查找03的学生成绩\',a.get(\'03\'))
print(\'查找09的学生成绩\',a.get(\'09\',\'没有该学生\'))
a[\'04\']=77
print(\'修改04的学生成绩为77\',a)
2 列表,元组,字典,集合的遍历。
jian=(\'0123456\')
zhi=(\'abcdefg\')
biao=list(\'abcdefg\')
jihe=set(biao)
yuan=tuple(\'abcdefg\')
b=dict(zip(jian,zhi))
print(b)
print(\'打印列表\')
for y in biao:
print(y)
print(\'打印字典\')
for x in b:
print(x,b[x])
print(\'打印元祖\')
for y in yuan:
print(y)
print(\'打印集合\')
for y in jihe:
print(y)
总结列表,元组,字典,集合的联系与区别
列表和元组是有序的,字典和集合都是无序的。
列表用“[]”表示。列表是可变对象,它支持在原处修改的操作.也可以通过指定的索引和分片获取元素。
元组用“()”表示。元组是只读的,不能修改。元组一旦定义其长度和内容都是固定的。
字典用“{}”表示。字典存储键值对(key-value)数据。每一组用冒号连起来,然后各组用逗号隔开。
集合没有特殊的表示方法,而是通过一个set函数转换成集合。集合内的元素没有重复的元素,主要是消除重复元素。
3 英文词频统计实例
str=\'\'\'Ohh wooaah
You know you love me,I know you care
You shout whenever, And I\'ll be there
You want my love, You want my heart
And we will never ever ever be apart
Are we an item? Girl quit playing
Were just friends, Or are we saying
So theres another one, Looks right in my eyes
My first love broke my heart for the first time,
And I was like
Baby, baby, baby nooo
My baby, baby, baby noo
My baby, baby, baby nooo
I thought youd always be mine mine
Baby, baby, baby nooo
My baby, baby, baby noo
My baby, baby, baby nooo
I thought youd always be mine, oh oh
For you, I would have done whatever
Another chance and we, We get together
And wanna play it cool, About loosin you
I\'ll buy you anything, I buy you any ring
And i in piece , baby fix me
And you shake me til you wake me from this bad dream
Im going down, down, dooown
And just can\'t believe my first love won be around
Baby, baby, baby nooo
Baby, baby, baby nooo
My baby, baby, baby noo
My baby, baby, baby nooo
I thought youd always be mine
When i was 13 i had my first love
Here was nobody to compare my baby
And nobody came between us or could ever come above
She had me goin crazy
Oh i was starstruck.
She woke me up daily dont need no starbucks
She made my heart pound
Asking for a beat when i see her in the street
And in the school on the playground
But i really wanna see her on the weekends
She knows she got me dazy
Cause she was so amazing and now my heart is breaking
But i just keep on sayin
Baby, baby, baby nooo
My baby, baby, baby noo
My baby, baby, baby nooo
I thought youd always be mine
Now Im all gone\'\'\'
print(\'待分析字符串\',str)
for i in \',.?!\\n\':
str=str.replace(i,\' \')
str=str.lower()
str=str.split(" ")
print(\'单词计数词典\')
words=set(str)
dt={}
dt[\'baby\']=str.count(\'baby\')
dt[\'love\']=str.count(\'love\')
dt[\'my\']=str.count(\'my\')
dt[\'me\']=str.count(\'me\')
for j in words:
dt[j]=str.count(j)
for j in dt:
print("{0:<11}{1}".format(j,dt[j]))
以上是关于组合数据类型练习,英文词频统计实例上的主要内容,如果未能解决你的问题,请参考以下文章