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