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

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

dic={}
d={'Bob':75,'Michael':95,'Tracy':85}
d['Bob']
'Thomas'in d
d.pop('Bob')
d.keys()
d.values()
d.items()
d.get('Bob','No Found')
d.pop('Bob','No Found')

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

ls=list('19970516')
for i in ls:
    print(i)

tu=tuple('19911007')
for i in tu:
    print(i)

se=set('19921127')
for i in se:
    print(i)

names=['Lay','Chanyeol','Xiumin','Chen']
numbers=['10','61','99','21']
ns=dict(zip(names,numbers))
for i in ns:
    print(i,ns[i])

列表特点就是:可重复,类型可不同。类型不同也是跟数组最本质的区别了。python里的列表用“[]”表示。

列表是可变对象,它支持在原处修改的操作.也可以通过指定的索引和分片获取元素。

区别于元组,可动态增加,删除,更新。

可以和字符串作为比较。因为字符串具备列表的一些特点。

元组和列表在结构上没有什么区别,唯一的差异在于元组是只读的,不能修改。元组用“()”表示。

元组一旦定义其长度和内容都是固定的。

一旦创建元组,则这个元组就不能被修改,即不能对元组进行更新、增加、删除操作。

若想创建包含一个元素的元组,则必须在该元素后面加逗号“,”,否则创建的不是一个元组,而是一个字符串。

集合就是我们数学学的集合,没有什么特殊的定义。集合最好的应用是去重。所以,集合内的元素没有重复的元素。

集合没有特殊的表示方法,而是通过一个set函数转换成集合。

集合是一个无序不重复元素集,基本功能包括关系测试和消除重复元素.。

由于集合是无序的。所以,sets 不支持 索引, 分片,或其它类序列(sequence-like)的操作。

字典存储键值对数据。

字典最外面用大括号,每一组用冒号连起来,然后各组用逗号隔开。

字典最大的价值是查询,通过键,查找值。

注意:字典和集合都是无序的。

3、英文词频统计实例

  1. 待分析字符串
  2. 分解提取单词
    1. 大小写 txt.lower()
    2. 分隔符'.,:;?!-_’
    3. 单词列表
  3. 单词计数字典
lyric='''Just one last dance....oh baby...just one last dance  
We meet in the night in the Spanish café 
I look in your eyes just don't know what to say 
It feels like I'm drowning in salty water 
A few hours left 'til the sun's gonna rise  
tomorrow will come an it's time to realize  
our love has finished forever 
how I wish to come with you (wish to come with you)  
how I wish we make it through 
Just one last dance 
before we say goodbye 
when we sway and turn round and round and round 
it's like the first time 
Just one more chance 
hold me tight and keep me warm 
cause the night is getting cold 
and I don't know where I belong 
Just one last dance 
The wine and the lights and the Spanish guitar 
I'll never forget how romantic they are  
but I know, tomorrow I'll lose the one I love 
There's no way to come with you 
it's the only thing to do 
Just one last dance 
before we say goodbye 
when we sway and turn round and round and round 
it's like the first time 
Just one more chance 
hold me tight and keep me warm 
cause the night is getting cold 
and I don't know where I belong 
Just one last dance, just one more chance, just one last dance 
'''
lyric=lyric.lower()
for i in ',.?-_':
    lyric=lyric.replace(i,' ')
words=lyric.split()

 

posted @ 2017-09-26 11:43  011赖颖璇  阅读(125)  评论(0编辑  收藏  举报