字典可以理解为列表的升级版,是一种通过名字或者关键字引用的得数据结构,其键可以是数字、字符串、元组,这种结构类型也称之为映射。字典类型是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] >>>--i tems和iteritems函数:items将所有的字典项以列表方式返回,列表中项来自(键,值),iteritems与items作用相似,但是返回的是一个迭代器对象而不是列表
# _*_ 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 >>>-- has_key函数:检查字典中是否含有给出的键
# _*_ 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} >>>-- popitem函数:移出字典中的项
# _*_ 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} >>>注:在复制的副本中对值进行替换后,对原来的字典不产生影响,但是如果修改了副本,原始的字典也会被修改。deepcopy函数使用深复制,复制其包含所有的值,这个方法可以解决由于副本修改而使原始字典也变化的问题。
# _*_ 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']} >>>--fromkeys函数:使用给定的键建立新的字典,键默认对应的值为None 或者指定默认的对应值
# _*_ 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} >>>
# _*_ 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'} >>>--setdefault函数:类似于get方法,获取与给定键相关联的值,也可以在字典中不包含给定键的情况下设定相应的键值
# _*_ 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} >>>--update函数:用一个字典更新另外一个字典
# _*_ 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 #qq #professional
3.2遍历字典中所有的value
for value in dic.values(): print value #526879116 #computerscience3.3遍历字典中所有的items
for item in dic.items(): print item #('qq','526879116') #('professional','computerscience')3.4遍历字典中所有的key-value(键值对)
for key,value in dic.items(): print('key=%s,value=%s'%(key,value)) #key=qq,value=526879116 #key=professional,value=computerscience