一.字典实例:建立学生学号成绩字典,做增删改查遍历操作
garden={'01':60,'02':75,'03':83,'04':90}
#增
garden['05']=92
#删
garden.pop('01')
#改
garden['02']=70
#查
garden.get('03')
print(garden)
二.列表,元组,字典,集合的遍历。
总结列表,元组,字典,集合的联系与区别。
l = list('123456')
t = tuple('654321')
d = {'小明':60,'小红':75,'小刚':95}
s = set([1,2,3,4,2,3,4])
for i in l:
print(i)
for i in t:
print(i)
for i in d:
print(d[i])
for i in s:
print(i)
联系与区别:
1.列表和元组都是有序的,而字典和集合是无序的
2.元组和列表很相似,但列表可以修改,元组不可修改
3.列表用“[]”表示。列表是可变对象,它支持在原处修改的操作.也可以通过指定的索引和分片获取元素。
4.元组用“()”表示。元组是只读的,不能修改。元组一旦定义其长度和内容都是固定的。
5.字典用“{}”表示。字典存储键值对(key-value)数据。每一组用冒号连起来,然后各组用逗号隔开。
6.集合没有特殊的表示方法,而是通过一个set函数转换成集合。集合内的元素没有重复的元素,主要是消除重复元素。
三.英文词频统计实例
- 待分析字符串
- 分解提取单词
- 大小写 txt.lower()
- 分隔符'.,:;?!-_’
- 单词列表
- 单词计数字典
word='''I'm a big big girl
in a big big world
It's not a big big thing
if you leave me
but I do do feel that
I too too will miss you much
miss you much...
I can see the first leaf falling
it's all yellow and nice
It's so very cold outside
like the way I'm feeling inside
I'm a big big girl
in a big big world
It's not a big big thing
if you leave me
but I do do feel that
I too too will
miss you much
miss you much...
Outside it's now raining
and tears are falling from my eyes
why did it have to happen
why did it all have to end
I'm a big big girl
in a big big world
It's not a big big thing
if you leave me
but I do do feel
that I too too will
miss you much
miss you much
I have your arms around me
ooooh like fire
but when I open my eyes
you're gone...
I'm a big big girl
in a big big world
It's not a big big thing
if you leave me
but I do do feel
that I too too will
miss you much
miss you much...
I'm a big big girl
in a big big world
It's not a big big thing
if you leave me
but I do feel I will
miss you much
miss you much...'''
word=word.lower()
word=word.replace('
',' ')
word=word.replace("'",' ')
words=word.split(' ')
dic={}
keys=set(words)
for i in keys:
dic[i]=word.count(i)
print(dic)