1、字典实例:建立学生学号成绩字典,做增删改查遍历操作。
a={'01':'95','02':'80','03':'98','04':'88','05':'92','06':'85'}
print(a.keys())print(a.values())a['07']=89a['06']=86a.pop('05')print(a.get('04'))print(a)
2、列表,元组,字典,集合的遍历。
总结列表,元组,字典,集合的联系与区别。
l = list('123212131231231')
t = tuple('abcdefghijklmn')s = set(l)d = dict(zip([1,2,3,4],[4,3,2,1]))print(l)for i in l: print(i)for j in t: print(j)for k in s: print(k)for m in d: print(m)列表是可变对象,它支持在原处修改的操作.也可以通过指定的索引和分片获取元素。
元组和列表在结构上没有什么区别,唯一的差异在于元组是只读的,不能修改。元组用()表示。元组一旦定义其长度和内容都是固定的。
集合是一个无序不重复元素集
字典最外面用大括号,每一组用冒号连起来,然后各组用逗号隔开,字典最大的价值是查询,通过键,查找值。
3、英文词频统计实例
A、待分析字符串
B、分解提取单词
a、大小写 txt.lower()
b、分隔符'.,:;?!-_’
c、单词列表
C、单词计数字典
t='''Wake up in the morning feeling like P Diddy
Put my glasses on, I'm out the doorI'm gonna hit this city (Let's go)Before I leave,Brush my teeth with a bottle of JackCause when I leave for the night,I ain't coming backI'm talking - pedicure on our toes, toesTrying on all our clothes, clothesBoys blowing up our phones, phonesDrop-toping, playing our favorite CDsPulling up to the partiesTrying to get a little bit tipsyDon't stop, make it popDJ, blow my speakers upTonight, Imma fightTill we see the sunlightTick tock, on the clockBut the party don't stopWoah-oh oh ohWoah-oh oh ohDon't stop, make it popDJ, blow my speakers upTonight, Imma fightTill we see the sunlightTick tock, on the clockBut the party don't stopWoah-oh oh ohWoah-oh oh ohAin't got a care in world,But got plenty of beerAin't got no money in my pocket,But I'm already hereAnd now, the dudes are lining upCause they hear we got swaggerBut we kick 'em to the curbunless they look like Mick JaggerI'm talking abouteverybody getting crunk, crunkBoys trying to touch my junk, junkGonna smack him if he getting too drunk, drunkNow, now - we goin' til they kick us out, outOr the police shut us down, downPolice shut us down, downPo-po shut us -Don't stop, make it popDJ, blow my speakers upTonight, Imma fightTill we see the sunlightTick tock, on the clockBut the party don't stopWoah-oh oh ohWoah-oh oh ohDon't stop, make it popDJ, blow my speakers upTonight, Imma fightTill we see the sunlightTick tock, on the clockBut the party don't stopWoah-oh oh ohWoah-oh oh ohDJ, you build me upYou break me downMy heart, it pounds yeah, you got meWith my hands upYou got me nowYou gotta that sound yeah, you got meDJ, you build me upYou break me downMy heart, it pounds yeah, you got meWith my hands upPut your hands upPut your hands upNo, the party don't stop until I walk inDon't stop, make it popDJ, blow my speakers upTonight, Imma fightTill we see the sunlightTick tock, on the clockBut the party don't stopWoah-oh oh ohWoah-oh oh ohDon't stop, make it popDJ, blow my speakers upTonight, Imma fightTill we see the sunlightTick tock, on the clockBut the party don't stopWoah-oh oh ohWoah-oh oh oh'''t=t.lower()for i in ',.?!': t=t.replace(i,' ')t=t.replace('\n',' ')words=t.split(' ')s=set(words)
dic={}lis=[]
value=[]for i in s: if(i==' '): continue if(i==''): continue dic[i]=words.count(i) lis.append(words.count(i)) vgalue=dic.values()lis=list(dic.items())
lis.sort(key=lambda x:x[1],reverse=True)for i in range(10): print(lis[i])