python-字典(第二篇(四):字典)
【Python之旅】第二篇(四):字典
摘要: 说明: 显然Python中字典的学习过程与列表是一样的,主要是围绕下面的函数来进行重点学习: 1 2 3 4 5 6 7 8 9 10 11 >>> xpleaf. xpleaf.clear( xpleaf.copy( xpleaf.get( xpleaf.has_key(...
说明:
显然Python中字典的学习过程与列表是一样的,主要是围绕下面的函数来进行重点学习:
|
1
2
3
4
5
6
7
8
9
10
11
|
>>> xpleaf.xpleaf.clear(xpleaf.copy(xpleaf.get(xpleaf.has_key(xpleaf.items(xpleaf.keys(xpleaf.pop(xpleaf.popitem(xpleaf.setdefault(xpleaf.update( |
1.基本操作
--创建一个字典
|
1
2
3
4
5
6
7
8
|
>>> xpleaf = {... 'name':'xpleaf',... 'occupation':'student',... 'hobby':'computer',... 'dream':'excellent hacker'... }>>> xpleaf{'hobby': 'computer', 'dream': 'excellent hacker', 'name': 'xpleaf', 'occupation': 'student'} |
·容易观察到字典的输出并没有按照创建字典时的顺序进行输出,因为字典按哈希值查找内容,而不是按索引号;
·{key:value}是字典的基本语法格式,key是唯一的,value可为大多数数据类型;
--查看键值对应的内容
|
1
2
|
>>> xpleaf['hobby']'computer' |
--修改键值对应的内容
|
1
2
3
|
>>> xpleaf['hobby'] = 'IT'>>> xpleaf{'hobby': 'IT', 'dream': 'excellent hacker', 'name': 'xpleaf', 'occupation': 'student'} |
--添加一个键值对
|
1
2
3
|
>>> xpleaf['girlfriend'] = 'none'>>> xpleaf{'girlfriend': 'none', 'hobby': 'IT', 'dream': 'excellent hacker', 'name': 'xpleaf', 'occupation': 'student'} |
·添加的元素在字典中的排序是随机的,因为索引号对字典没有意义(按照哈希值进行value值的查找);
2.has_key()函数
·功能:接受key的查询,以bool值形式返回查询字典中是否有该key;
·演示如下:
|
1
2
3
4
|
>>> xpleaf.has_key('dream')True>>> xpleaf.has_key('wife')False |
3.items()函数
·功能:将字典转换为列表,列表的元素为元组,其中左元素为key,右元素为value;
·演示如下:
|
1
2
|
>>> xpleaf.items()[('girlfriend', 'none'), ('hobby', 'IT'), ('dream', 'excellent hacker'), ('name', 'xpleaf'), ('occupation', 'student')] |
·基于上述输出形式,可对字典的key和value进行遍历,如下:
|
1
2
3
4
5
6
7
8
|
>>> for key,value in xpleaf.items():... print key,value... girlfriend nonehobby ITdream excellent hackername xpleafoccupation student |
·item()函数的原理是把字典转换为列表存储在内存中,对于数据量大的情况下,会比较慢;
·大数据量的字典遍历方法:
|
1
2
3
4
5
6
7
8
|
>>> for key in xpleaf:... print key,xpleaf[key]... girlfriend nonehobby ITdream excellent hackername xpleafoccupation student |
4.get()函数
·功能:取对应key的value值;
·演示如下:
|
1
2
3
4
5
6
|
>>> xpleaf{'girlfriend': 'none', 'hobby': 'IT', 'dream': 'excellent hacker', 'name': 'xpleaf', 'occupation': 'student'}>>> xpleaf.get('dream')'excellent hacker'>>> xpleaf.get('wife') ===>如果没有该key值则不会有输出>>> |
·即相当于dict[key]的方法取value值;
5.keys()函数
·功能:取出字典中的key值,并生成相应的列表;
·演示如下:
|
1
2
|
>>> xpleaf.keys()['girlfriend', 'hobby', 'dream', 'name', 'occupation'] |
5.pop()函数
·功能:弹出一个key,即删除一个键值对;
·演示如下:
|
1
2
3
4
5
6
|
>>> xpleaf{'girlfriend': 'none', 'hobby': 'IT', 'dream': 'excellent hacker', 'name': 'xpleaf', 'occupation': 'student'}>>> xpleaf.pop('girlfriend')'none'>>> xpleaf{'hobby': 'IT', 'dream': 'excellent hacker', 'name': 'xpleaf', 'occupation': 'student'} |
6.popitem()函数
·功能:按顺序删除字典中的元素;
·演示如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
>>> a{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 6: 'f'}>>> a.popitem()('a', 1)>>> a.popitem()('c', 3)>>> a.popitem()('b', 2)>>> a.popitem()('e', 5)>>> a.popitem()('d', 4)>>> a.popitem()(6, 'f') |
7.setdefault()函数
·在字典中添加元素,如果原来存在该元素,则不进行任何修改;
·演示如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
>>> xpleaf{'hobby': 'IT', 'dream': 'excellent hacker', 'name': 'xpleaf', 'occupation': 'student'}>>> xpleaf.setdefault('hobby','computer') ===>'hobby'键值对已经存在,不会添加'IT'>>> xpleaf{'hobby': 'IT', 'dream': 'excellent hacker', 'name': 'xpleaf', 'occupation': 'student'}>>> xpleaf.setdefault('weight','55kg') ===>'weight'键值对不存在,会进行添加'55kg'>>> xpleaf{'name': 'xpleaf', 'weight': '55kg', 'hobby': 'IT', 'dream': 'excellent hacker', 'occupation': 'student'}>>> xpleaf.setdefault('wife') ===>添加没有的键值对,>>> xpleaf{'name': 'xpleaf', 'weight': '55kg', 'wife': None, 'hobby': 'IT', 'dream': 'excellent hacker', 'occupation': 'student'} |
8.update()函数
·功能:合并两个字典
·演示如下:
|
1
2
3
4
5
6
7
8
9
|
>>> a{'a': 1, 'c': 3, 'b': 2}>>> b{'e': 4, 'g': 6, 'f': 5}>>> a.update(b)>>> a{'a': 1, 'c': 3, 'b': 2, 'e': 4, 'g': 6, 'f': 5}>>> b{'e': 4, 'g': 6, 'f': 5} |
·合并的顺序依然是随机的,原理与前面一样;
·更新的只是字典a,字典b没有变化;
·如果合并字典时有重复的item项,则会进行覆盖:
|
1
2
3
4
5
6
7
|
>>> a{'a': 1, 'c': 3, 'b': 2, 'e': 4, 'g': 6, 'f': 5}>>> c{'b': 'cover2', 'g': 'cover1'}>>> a.update(c)>>> a{'a': 1, 'c': 3, 'b': 'cover2', 'e': 4, 'g': 'cover1', 'f': 5} |
9.values()函数
·功能:取字典中所有key的value值,并生成相应的列表
·演示如下:
|
1
2
3
4
|
>>> xpleaf{'name': 'xpleaf', 'weight': '55kg', 'wife': None, 'hobby': 'IT', 'dream': 'excellent hacker', 'occupation': 'student'}>>> xpleaf.values()['xpleaf', '55kg', None, 'IT', 'excellent hacker', 'student'] |
·多用在value值的数据类型都相同的字典中,以用于数据的批量分析;
10.clear()函数
·功能:清空字典的item项
·演示如下:
|
1
2
3
4
5
|
>>> a{'a': 1, 'c': 3, 'b': 'cover2', 'e': 4, 'g': 'cover1', 'f': 5}>>> a.clear()>>> a{} |
·与del不同,del是直接删除字典:
|
1
2
3
4
5
|
>>> del a>>> aTraceback (most recent call last): File "<stdin>", line 1, in <module>NameError: name 'a' is not defined |
11.copy()函数
·功能:对字典进行浅复制;
·Python中普通情况下的“复制”:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
>>> xpleaf{'name': 'xpleaf', 'weight': '55kg', 'wife': None, 'hobby': 'IT', 'dream': 'excellent hacker', 'occupation': 'student'}>>> xpleaf_copy = xpleaf>>> xpleaf_copy{'name': 'xpleaf', 'weight': '55kg', 'wife': None, 'hobby': 'IT', 'dream': 'excellent hacker', 'occupation': 'student'}>>> xpleaf['hobby'] = 'IT_Field'>>> xpleaf_copy{'name': 'xpleaf', 'weight': '55kg', 'wife': None, 'hobby': 'IT_Field', 'dream': 'excellent hacker', 'occupation': 'student'}>>> xpleaf_copy['wife'] = 'None!!!'>>> xpleaf_copy{'name': 'xpleaf', 'weight': '55kg', 'wife': 'None!!!', 'hobby': 'IT_Field', 'dream': 'excellent hacker', 'occupation': 'student'}>>> xpleaf{'name': 'xpleaf', 'weight': '55kg', 'wife': 'None!!!', 'hobby': 'IT_Field', 'dream': 'excellent hacker', 'occupation': 'student'} |
·即将变量赋给其它变量只是将对象(实际的字典)作一个引用传递而已,修改任何一个引用都会改变原来对象的值;
·copy()的浅复制功能则不是引用传递:
|
1
2
3
4
5
6
7
8
|
>>> xpleaf_copy2 = xpleaf.copy()>>> xpleaf_copy2{'name': 'xpleaf', 'weight': '55kg', 'wife': 'None!!!', 'hobby': 'IT_Field', 'dream': 'excellent hacker', 'occupation': 'student'}>>> xpleaf_copy2['wife'] = 'CL'>>> xpleaf_copy2{'name': 'xpleaf', 'weight': '55kg', 'wife': 'CL', 'hobby': 'IT_Field', 'dream': 'excellent hacker', 'occupation': 'student'}>>> xpleaf{'name': 'xpleaf', 'weight': '55kg', 'wife': 'None!!!', 'hobby': 'IT_Field', 'dream': 'excellent hacker', 'occupation': 'student'} |
·当然copy()更重要的作用不仅在于此,这里只是简单提及它的作用。

浙公网安备 33010602011771号