组合数据类型练习,英文词频统计实例上
##1.字典实例:建立学生学号成绩字典,做增删改查遍历操作。
s={'01':1,'02':2,'03':2,'04':3,'05':2,'06':3} print('学号为1的分数是:',s['01']) s.pop('01') print('删除学号为1的分数:',s) s['02']=3 print('更改学号为2的分数为3:',s) s['07']=1 print('增加学号7的分数:',s) del(s['03']) print('删除学号为3的分数:',s) for i in s: print( i,s[i]) s.keys() s.values() s.items()
2列表,元组,字典,集合的遍历
name=['world','jocker','queen','star'] number=[10,12,13,15] nn=dict(zip(name,number)) for i in nn: print(i,nn[i]) d={'56732438753'} for i in d: print(i) g=['8','4','8','9','5','4'] for i in g: print(i) o=('4','1','9','7','7','5') for i in o: print(i)
总结列表,元组,字典,集合的联系与区别。
1元组和列表在结构上没有什么区别,唯一的差异在于元组是只读的,不能修改。元组用“()”表示。
2字典存储键值对数据。
3字典最外面用大括号,每一组用冒号连起来,然后各组用逗号隔开。
4字典最大的价值是查询,通过键,查找值。
5集合是一个无序不重复元素集,基本功能包括关系测试和消除重复元素.。
6由于集合是无序的。所以,sets 不支持 索引, 分片,或其它类序列(sequence-like)的操作。
7列表是可变对象,它支持在原处修改的操作.也可以通过指定的索引和分片获取元素。
区别于元组,可动态增加,删除,更新。python里的列表用“[]”表示。
英文词频统计实例
- 待分析字符串
- 分解提取单词
- 大小写 txt.lower()
- 分隔符'.,:;?!-_’
- 单词列表
- 单词计数字典
s='''i stay out too late Got nothing in my brain That's what people say, mmm-mmm That's what people say, mmm-mmm I go on too many dates [chuckle] But I can't make them stay At least that's what people say, mmm-mmm That's what people say, mmm-mmm But I keep cruising Can't stop, won't stop moving It's like I got this music In my mind Saying, "It's gonna be alright." 'Cause the players gonna play, play, play, play, play And the haters gonna hate, hate, hate, hate, hate Baby, I'm just gonna shake, shake, shake, shake, shake I shake it off, I shake it off Heart-breakers gonna break, break, break, break, break And the fakers gonna fake, fake, fake, fake, fake Baby, I'm just gonna shake, shake, shake, shake, shake I shake it off, I shake it off I never miss a beat I'm lightning on my feet And that's what they don't see, mmm-mmm That's what they don't see, mmm-mmm I'm dancing on my own (dancing on my own) I make the moves up as I go (moves up as I go) And that's what they don't know, mmm-mmm That's what they don't know, mmm-mmm But I keep cruising Can't stop, won't stop grooving It's like I got this music In my mind Saying, "It's gonna be alright." 'Cause the players gonna play, play, play, play, play And the haters gonna hate, hate, hate, hate, hate Baby, I'm just gonna shake, shake, shake, shake, shake I shake it off, I shake it off Heart-breakers gonna break, break, break, break, break And the fakers gonna fake, fake, fake, fake, fake Baby, I'm just gonna shake, shake, shake, shake, shake I shake it off, I shake it off Shake it off, I shake it off, I, I, I shake it off, I shake it off, I, I, I shake it off, I shake it off, I, I, I shake it off, I shake it off Hey, hey, hey''' s=s.lower() for i in ',.': s=s.replace(i,' ') s=s.split(' ') d = {} s.sort() disc = set(s) for i in disc: d[i] = 0 for i in s: d[i] = d[i]+1 n = d.items() print('单词计数:') print(n)