字典可以理解为列表的升级版,是一种通过名字或者关键字引用的得数据结构,其键可以是数字、字符串、元组,这种结构类型也称之为映射。字典类型是Python中唯一內建的映射类型
一、字典的创建
-字典是另一种可变容器模型,且可存储任意类型对象
-用{}括起来,元素是键值对的形式,键和值通过:隔开{key:value}
每个元素之间通过‘,’分隔
-案例:dic = {'name':'张三','age':18,'height':170}
-key只能是不可变类型(字符串,数字) 不可以是列表或者字典
-key是唯一的,不可重复 值可以不唯一
-如果需要修改‘张三’,则需要先找到他的下边,而字典的下标是根据名字找
.字典和列表一样可以存取多喝数据
.列表查询元素通过下标,字典查询元素通过前面的key值
二、字典操作
--打印字典中的元素
print(dic['name'])
--修改 通过key找到
- temp=raw_input("请输入修改后的值")
- dic['name']=temp
- print("修改后的值为%s"%dic['name'])
--增 如果给字典中不存在的key赋值,则字典中会增加这个元素
- dic={'name':'淡淡','sex':'男','professional':'计算机科学与技术'}
- vqq=raw_input("请输入QQ号")
- dic['qq']=vqq
- print("添加后的QQ号为%s"%dic['qq'])
--查
--len() 查字典中元素个数 一个元素即一个key和一个值
- dic = {'name':'张三','age':18,'height':170}
- len(dic)
- #3
--keys和iterkeys:keys将字典中的键以列表形式返回,iterkeys返回键的迭代器
- # _*_ coding:utf-8 _*_
- d={'one':1,'two':2,'three':3}
- print d
- print 'keys方法:'
- list=d.keys()
- print list
- print u'\niterkeys方法:'
- it=d.iterkeys()
- for x in it:
- print x
- 运算结果:
- {'three': 3, 'two': 2, 'one': 1}
- keys方法:
- ['three', 'two', 'one']
- iterkeys方法:
- =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
- three
- two
- one
- >>>
- # _*_ coding:utf-8 _*_
- d={
- 'one':123,
- 'two':2,
- 'three':3,
- 'test':2
- }
- print d.values()
- 运算结果:
- =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
- [2, 3, 2, 123]
- >>>
- # _*_ coding:utf-8 _*_
- d={'one':1,'two':2,'three':3}
- print d
- list=d.items()
- for key,value in list:
- print key,':',value
- 运算结果:
- =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
- {'three': 3, 'two': 2, 'one': 1}
- three : 3
- two : 2
- one : 1
- >>>
- # _*_ coding:utf-8 _*_
- d={'one':1,'two':2,'three':3}
- print d
- it=d.iteritems()
- for k,v in it:
- print "d[%s]="%k,v
- 运算结果:
- =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
- {'three': 3, 'two': 2, 'one': 1}
- d[three]= 3
- d[two]= 2
- d[one]= 1
- >>>
- # _*_ coding:utf-8 _*_
- d={'one':1,'two':2,'three':3}
- print d
- print d.has_key('one')
- print d.has_key('four')
- 运算结果:
- =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
- {'three': 3, 'two': 2, 'one': 1}
- True
- False
- >>>
- # _*_ coding:utf-8 _*_
- d={'one':1,'two':2,'three':3}
- print d
- print d.get('one')
- print d.get('four')
- 运算结果:
- =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
- {'three': 3, 'two': 2, 'one': 1}
- 1
- None
- >>>
-- clear函数:清空整个字典,删除之后可以访问
- # _*_ coding:utf-8 _*_
- d={'one':1,'two':2,'three':3,'four':4}
- print d
- d.clear()
- print d
- 运算结果:
- =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
- {'four': 4, 'three': 3, 'two': 2, 'one': 1}
- {}
- >>>
--del:删除指定元素 删除整个字典(删除之后不能访问)
-- pop函数:删除字典中对应的键
- # _*_ coding:utf-8 _*_
- d={'one':1,'two':2,'three':3}
- print d
- d.pop('one')
- print d
- 运算结果:
- =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
- {'three': 3, 'two': 2, 'one': 1}
- {'three': 3, 'two': 2}
- >>>
- # _*_ coding:utf-8 _*_
- d={'one':1,'two':2,'three':3}
- print d
- d.popitem()
- print d
- 运算结果:
- =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
- {'three': 3, 'two': 2, 'one': 1}
- {'two': 2, 'one': 1}
- >>>
-- copy函数:返回一个具有相同键值的新字典
- # _*_ coding:utf-8 _*_
- x={'one':1,'two':2,'three':3,'test':['a','b','c']}
- print '初始X字典:'
- print x
- print 'X复制到Y:'
- y=x.copy()
- print 'Y字典:'
- print y
- y['three']=33
- print '修改Y中的值,观察输出:'
- print y
- print x
- print '删除Y中的值,观察输出'
- y['test'].remove('c')
- print y
- print x
- 运算结果:
- =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
- 初始X字典:
- {'test': ['a', 'b', 'c'], 'three': 3, 'two': 2, 'one': 1}
- X复制到Y:
- Y字典:
- {'test': ['a', 'b', 'c'], 'one': 1, 'three': 3, 'two': 2}
- 修改Y中的值,观察输出:
- {'test': ['a', 'b', 'c'], 'one': 1, 'three': 33, 'two': 2}
- {'test': ['a', 'b', 'c'], 'three': 3, 'two': 2, 'one': 1}
- 删除Y中的值,观察输出
- {'test': ['a', 'b'], 'one': 1, 'three': 33, 'two': 2}
- {'test': ['a', 'b'], 'three': 3, 'two': 2, 'one': 1}
- >>>
- # _*_ coding:utf-8 _*_
- from copy import deepcopy
- x={}
- x['test']=['a','b','c','d']
- y=x.copy()
- z=deepcopy(x)
- print '输出:'
- print y
- print z
- print '修改后输出:'
- x['test'].append('e')
- print y
- print z
- 运算输出:
- 输出:
- {'test': ['a', 'b', 'c', 'd']}
- {'test': ['a', 'b', 'c', 'd']}
- 修改后输出:
- =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
- {'test': ['a', 'b', 'c', 'd', 'e']}
- {'test': ['a', 'b', 'c', 'd']}
- >>>
- # _*_ coding:utf-8 _*_
- d=dict.fromkeys(['one','two','three'])
- print d
- 运算输出:
- =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
- {'three': None, 'two': None, 'one': None}
- >>> <span style="background-color:rgb(255,255,255);font-family:'-apple-system', 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun;color:rgb(79,79,79);font-size:16px;"> </span><span style="background-color:rgb(255,255,255);font-family:'-apple-system', 'SF UI Text', Arial, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', sans-serif, SimHei, SimSun;color:rgb(79,79,79);font-size:16px;"> </span>
- # _*_ coding:utf-8 _*_
- d=dict.fromkeys(['one','two','three'],'unknow')
- print d
- 运算结果:
- =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
- {'three': 'unknow', 'two': 'unknow', 'one': 'unknow'}
- >>>
- # _*_ coding:utf-8 _*_
- d={'one':1,'two':2,'three':3}
- print d
- print d.setdefault('one',1)
- print d.setdefault('four',4)
- print d
- 运算结果:
- {'three': 3, 'two': 2, 'one': 1}
- 1
- 4
- {'four': 4, 'three': 3, 'two': 2, 'one': 1}
- >>>
- # _*_ coding:utf-8 _*_
- d={
- 'one':123,
- 'two':2,
- 'three':3
- }
- print d
- x={'one':1}
- d.update(x)
- print d
- 运算结果:
- =======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
- {'three': 3, 'two': 2, 'one': 123}
- {'three': 3, 'two': 2, 'one': 1}
- >>>
三、遍历字典中的元素(与for循环遍历列表类似)
3.1遍历字典中所有的key
- for key in dic.keys():
- print key
- #professional
3.2遍历字典中所有的value
- for value in dic.values():
- print value
- #526879116
- #computerscience
- for item in dic.items():
- print item
- #('qq','526879116')
- #('professional','computerscience')
- for key,value in dic.items():
- print('key=%s,value=%s'%(key,value))
- #key=qq,value=526879116
- #key=professional,value=computerscience
原文链接 : https://blog.csdn.net/lqian1993/article/details/80226782