Python字典是另一种可变容器模型(无序),且可存储任意类型对象,如字符串、数字、元组等其他容器模型。本文章主要介绍Python中字典(Dict)的详解操作方法,包含创建、访问、删除、其它操作等,需要的朋友可以参考下。

字典由键和对应值成对组成。字典也被称作关联数组或哈希表。基本语法如下:

1.创建字典

1 >>> dict = {'ob1':'computer', 'ob2':'mouse', 'ob3':'printer'}2 技巧:3 字典中包含列表:dict={'yangrong':['23','IT'],"xiaohei":['22','dota']}4 字典中包含字典:dict={'yangrong':{"age":"23","job":"IT"},"xiaohei":{"'age':'22','job':'dota'"}}5 注意:6 每个键与值用冒号隔开(:),每对用逗号,每对用逗号分割,整体放在花括号中({})。7 键必须独一无二,但值则不必。

2.访问字典里的值

1 >>> dict = {'ob1':'computer', 'ob2':'mouse', 'ob3':'printer'}2 >>> print(dict['ob1'])3 computer4 如果用字典里没有的键访问数据,会输出错误如下:5 >>> print(dict['ob4'])6 Traceback (most recent call last):7 File "", line 1, in 
8 print(dict['ob4'])9
10 访问所有值11 >>> dict1 = {'ob1':'computer', 'ob2':'mouse', 'ob3':'printer'}12 >>> for key indict1:13 print(key,dict1[key])14 ob3 printer15 ob2 mouse16 ob1 computer
3.修改字典
1 >>> dict = {'ob1':'computer', 'ob2':'mouse', 'ob3':'printer'}2 >>> dict['ob1']='book'
3 >>> print(dict)4 {'ob3': 'printer', 'ob2': 'mouse', 'ob1': 'book'}
4.删除字典
1能删单一的元素2 >>> dict = {'ob1':'computer', 'ob2':'mouse', 'ob3':'printer'}3 >>> del dict['ob1']4 >>> print(dict)5 {'ob3': 'printer', 'ob2': 'mouse'}6
7 删除字典中所有元素8 >>> dict1={'ob1':'computer','ob2':'mouse','ob1':'printer'}9 >>>dict1.clear()10 >>> print(dict1)11 {}12
13
14 删除整个字典,删除后访问字典会抛出异常。15 >>> dict1 = {'ob1':'computer', 'ob2':'mouse', 'ob3':'printer'}16 >>> deldict117 >>> print(dict1)18 Traceback (most recent call last):19 File "", line 1, in 
20 print(dict1)21 NameError: name 'dict1' is not defined
5.更新字典
1 update()方法可以用来将一个字典的内容添加到另外一个字典中:2 >>> dict1 = {'ob1':'computer', 'ob2':'mouse'}3 >>> dict2={'ob3':'printer'}4 >>>dict1.update(dict2)5 >>> print(dict1)6 {'ob3': 'printer', 'ob2': 'mouse', 'ob1': 'computer'}
6.映射类型相关的函数
1 >>> dict(x=1, y=2)2 {'y': 2, 'x': 1}3 >>> dict8 = dict(x=1, y=2)4 >>>dict85 {'y': 2, 'x': 1}6 >>> dict9 = dict(**dict8)7 >>>dict98 {'y': 2, 'x': 1}9
10 dict9 = dict8.copy()
7.字典键的特性
1 字典值可以没有限制地取任何python对象,既可以是标准的对象,也可以是用户定义的,但键不行。2 两个重要的点需要记住:3 1)不允许同一个键出现两次。创建时如果同一个键被赋值两次,后一个值会被记住4 >>> dict1={'ob1':'computer','ob2':'mouse','ob1':'printer'}5 >>> print(dict1)6 {'ob2': 'mouse', 'ob1': 'printer'}7
8 2)键必须不可变,所以可以用数,字符串或元组充当,用列表就不行9 >>> dict1 = {['ob1']:'computer', 'ob2':'mouse', 'ob3':'printer'}10 Traceback (most recent call last):11 File "", line 1, in 
12 dict1 = {['ob1']:'computer', 'ob2':'mouse', 'ob3':'printer'}13 TypeError: unhashable type: 'list'
8.字典内置函数&方法
Python字典包含了以下内置函数:1、cmp(dict1, dict2):比较两个字典元素。(python3后不可用)2、len(dict):计算字典元素个数,即键的总数。3、str(dict):输出字典可打印的字符串。4、type(variable):返回输入的变量类型,如果变量是字典就返回字典类型。
Python字典包含了以下内置方法:1、radiansdict.clear():删除字典内所有元素2、radiansdict.copy():返回一个字典的浅复制3、radiansdict.fromkeys():创建一个新字典,以序列seq中元素做字典的键,val为字典所有键对应的初始值4、radiansdict.get(key, default=None):返回指定键的值,如果值不在字典中返回default值5、radiansdict.has_key(key):如果键在字典dict里返回true,否则返回false6、radiansdict.items():以列表返回可遍历的(键, 值) 元组数组7、radiansdict.keys():以列表返回一个字典所有的键8、radiansdict.setdefault(key, default=None):和get()类似, 但如果键不已经存在于字典中,将会添加键并将值设为default9、radiansdict.update(dict2):把字典dict2的键/值对更新到dict里10、radiansdict.values():以列表返回字典中的所有值