一、总结列表,元组,字典,集合的联系与区别:
区别:列表list,元组tuple是有顺序的,而字典dict和集合是没顺序的。列表是以[ ]形式表示,元组是以( )表示,字典以{ }表示,集合则是以[()]的形式表示。列表是可变对象,可以有增删改操作,而元组是只读的,不能修改。字典使用键-值(key-value)存储,键是不可变的对象。插入和查找效率高,不会随着键的增加而变慢,但是需要占用大量的内存。字典是用空间换取时间的一种方法。集合是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素。
联系:列表,元组,字典,集合可以相互转换。
列表,元组,字典,集合的遍历:
列表是可变序列,元组是不可变序列;列表的值可以修改,而元祖的值初始化后不可修改,两者都是有序的。字典和集合 两者都是无序的,数据量大时可用集合或列表来创建
li= list(‘列表的遍历‘) #列表的遍历 print(li) for i in li: print(i) tu = tuple(‘我爱上数据挖掘课‘) #元组的遍历 print(tu) for a in tu: print(a) st = set(‘集合的遍历‘) #集合的遍历 print(st) for b in st: print(b) dt = {‘a‘:80,‘b‘:79,‘c‘:90} #字典的遍历 print(dt) for i in dt: print(i,dt[i])
运行结果:
二、英文词频统计:
1、下载一首英文的歌词或文章str
2、分隔出一个一个的单词 list
3、统计每个单词出现的次数 dict
代码:
#下载一首英文的歌词 str=‘‘‘There‘s a fire starting in my heart,Reaching a fever pitch and it‘s bringing me out the dark Finally, I can see you crystal clear. Go ahead and sell me out and I‘ll lay your shipbare. See how I leave, with every piece of you Don‘t underestimate the things that I will do. There‘s a fire starting in my heart, Reaching a fever pitch and it‘s bringing me out the dark The scars of your love, remind me of us. They keep me thinking that we almost had it all The scars of your love, they leave me breathless I can‘t help feeling... We could have had it all Rolling in the Deep You had my heart... Inside of your hands And you played it... To the beat Baby I have no story to be told, But I‘ve heard one of you and I‘m gonna make your head burn. Think of me in the depths of your despair. Making a home down there,as mine sure won‘t be shared. The scars of your love,remind me of us. They keep me thinking that we almost had it all The scars of your love, they leave me breathless I can‘t help feeling "We could have had it all... Rolling in the Deep You had my heart inside of your hand And you played it To the beat We could have had it all Rolling in the deep. You had my heart inside of your hand, But you played it with your beat Throw yourself through every open door (Whoa) Count your blessings to find what look for (Whoa-uh) Turn my sorrow into treasured gold (Whoa) And pay me back in kind- You reap just what you‘ve sown. "(You‘re gonna wish you... Never had met me) We could have had it all (Tears are gonna fall... Rolling in the deep) We could have had it all yeah ( you‘re gonna wish you... never had met me) It all. (Tears are gonna fall) It all It all (Rolling in the deep) We could have had it all (you‘re gonna wish you, never had met me) Rolling in the deep (Tears are gonna fall rolling in the deep) You had my heart inside... (you‘re gonna wish you)... of your hand (Never had met me) And you played it... (Tears are gonna fall)... to the beat (Rolling in the deep) We could have had it all ( you‘re wish you never had met me) Rolling in the deep (tears are gonna fall, rolling in the deep) You had my heart... ( you‘re gonna wish you)... Inside of your hand (Never had met me)" But you played it You played it. You played it to the beat‘‘‘ #输出文章 print(str) #f分隔出一个一个的单词 li1=str.split() print(len(li1),li1) #统计每个单词出现的次数 strset=set(li1) #用set把字符串转换成集合 for word in strset: print(word,li1.count(word)) #统计每一个单词在歌曲中的次数
运行结果:
.
.
.
原文地址:https://www.cnblogs.com/123-feng/p/9690841.html
时间: 2024-10-11 01:23:22