字典实例:建立学生学号成绩字典,做增删改查遍历操作。
================ RESTART: C:/Users/Administrator/Desktop/1.py ================>>> d={'hello':88,'insert':12,'input':45,'open':56}>>> d['open']56>>> d.pop('insert')12>>> d.keys()dict_keys(['hello', 'input', 'open'])>>> d.values()dict_values([88, 45, 56])>>> d.items()dict_items([('hello', 88), ('input', 45), ('open', 56)])>>> d.get('hi',' ')' '>>> d.get('jis','nius')'nius'>>> d.get('open',' ')56>>> del(d['hello'])>>> d{'input': 45, 'open': 56}>>> for i in d: print(i,d[i])input 45open 56>>>
列表,元组,字典,集合的遍历。
总结列表,元组,字典,集合的联系与区别。列表:list是一种有序的序列,正向递增、反向递减序号;可以随时添加和删除其中的元素;没有长度限制,元素类型可以不同。
元组:tuple和list非常类似;都是内置的集合;tuple一旦初始化就不能修改。字典:是用空间来换取时间的一种方法。 和list比较;特点是:查找和插入的速度极快,不会随着key的增加而变慢,需要占用大量的内存,内存浪费多。 和list相反;查找和插入的时间随着元素的增加而增加;占用空间小,浪费内存很少。集合:也是一组key的集合,但是不存储values;没有重复的key;创建一个set,需要提供一个list做为输入集合;set 是无序的。英文词频统计实例
Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.>>> sing='''Hello, it's meI was wondering if after all these years you'd like to meetTo go over everythingThey say that time's supposed to heal yaBut I ain't done much healingHello, can you hear me?I'm in California dreaming about who we used to beWhen we were younger and freeI've forgotten how it feltBefore the world fell at our feetThere's such a difference between usAnd a million milesHello from the other side
I must have called a thousand timesTo tell you I'm sorry for everything that I've doneBut when I call, you never seem to be homeHello from the outsideAt least I can say that I've triedTo tell you I'm sorry for breaking your heartBut it don't matter, it clearly doesn't tear you apartAnymoreHello, how are you?
It's so typical of me to talk about myself, I'm sorryI hope that you're wellDid you ever make it out of that townWhere nothing ever happened?It's no secret that the both of usAre running out of timeSo hello from the other side (other side)
I must have called a thousand times (thousand times)To tell you I'm sorry for everything that I've doneBut when I call, you never seem to be homeHello from the outside (outside)At least I can say that I've tried (I've tried)To tell you I'm sorry for breaking your heartBut it don't matter, it clearly doesn't tear you apartAnymoreOoh, anymore
Ooh, anymoreOoh, anymoreAnymoreSo hello from the other side (other side)
I must have called a thousand times (thousand times)To tell you I'm sorry for everything that I've doneBut when I call, you never seem to be homeHello from the outside (outside)At least I can say that I've tried (I've tried)To tell you I'm sorry for breaking your heartBut it don't matter, it clearly doesn't tear you apartAnymore'''>>> s=sing.lower()>>> for i in ',.?/!': s=sing.replace(i,' ')>>> s=sing.split(' ')>>> words=set(s)>>> d={}>>> d['we']=s.count('we')>>> for i in words: d[i]=s.count(i)
>>> for i in d: print('{0:<10}{1}'.format(i,d[i]))
we 2used 1sorry 6I've 6must 3timesTo 1triedTo 1miles
Hello1
hello 2ain't 1to 7outsideAt1go 1(other 2thousand 3least 3talk 1supposed 1I 7that 10dreaming 1so 1apartAnymore1sorryI 1when 3you're 1feetThere's1meetTo 1clearly 3usAnd 1was 1like 1much 1it 8(thousand 2called 3homeHello3times)To 2tried 2times 2everythingThey1timeSo 1
younger 1our 1side)I 2usAre 1you'd 1freeI've 1who 1I'm 7both 1never 3these 1forgotten 1meI 1heal 1all 1wondering 1world 1everything3running 1(outside)At2you?It's 1sideI 1don't 3(I've 2apartAnymoreHello,1
secret 1years 1between 1the 8Hello, 1me?I'm 1after 1for 6side 2myself, 1were 1California1doesn't 3done 1can 4ever 2typical 1out 2such 1anymoreAnymoreSo1
outside 2over 1heartBut 3difference1million 1a 5breaking 3are 1doneBut 3of 4wellDid 1healingHello,1other 3it's 1at 1yaBut 1tell 6hear 1call, 3townWhere1say 4from 6fell 1nothing 1and 1feltBefore1be 3have 3about 2hope 1no 1tried)To 2your 3make 1seem 3anymoreOoh,2beWhen 1me 1how 2you 14in 1apartAnymoreOoh,1
if 1matter, 3time's 1happened?It's1tear 3>>>