当前位置:文档之家› Python 3语法小记 字典 dictionary

Python 3语法小记 字典 dictionary

Python 3语法小记字典dictionary字典是Python里面一种无序存储结构,存储的是键值对key - value。

关键字应该为不可变类型,如字符串、整数、包含不可变对象的元组。

字典的创建很简单,用d = {key1 : value2, key2 : value2}的形式就可以创建一个新的字典,当然也可以通过dict 接受一个含有键,值的序列对或者关键字参数来创建字典。

键可以是多种类型,但键是唯一的不重复的,值可以不唯一[python]view plain copy print?1.>>> d = {'a':1, 'b':2}2.>>> d3.{'b': 2, 'a': 1}4.>>> L = [('Jonh',18), ('Nancy',19)]5.>>> d = dict(L) #通过包含键值的列表创建6.>>> d7.{'Jonh': 18, 'Nancy': 19}8.>>> T = tuple(L)9.>>> T10.(('Jonh', 18), ('Nancy', 19))11.>>> d = dict(T) #通过包含键值的元组创建12.>>> d13.{'Jonh': 18, 'Nancy': 19}14.>>> d = dict(x = 1, y = 3) #通过关键字参数创建15.>>> d16.{'x': 1, 'y': 3}17.>>> d[3] = 'z'18.>>> d19.{3: 'z', 'x': 1, 'y': 3}还有一个创建字典的方法就是fromkeys(S [ , v]) python里的解释是New dict with key from S and value equal to v ,即将S里的元素作为键,v作为所有键的值,v 的默认值为None。

可以通过已存在的字典d 调用d.fromkeys(S [, v] ) 也可以通过类型调用dict.fromkeys( S [, v] )[python]view plain copy print?1.>>> d2.{3: 'z', 'y': 3}3.>>> L1 = [1,2,3]4.>>> d.fromkeys(L1)5.{1: None, 2: None, 3: None}6.>>> {}.fromkeys(L1,'nothing')7.{1: 'nothing', 2: 'nothing', 3: 'nothing'}8.>>> dict.fromkeys(L1,'over')9.{1: 'over', 2: 'over', 3: 'over'}字典是无序的,所以不能通过索引来获取值,要通过键来找到关联值。

对于不存在的键,会出现错误KeyError[python]view plain copy print?1.>>> d2.{3: 'z', 'x': 1, 'y': 3}3.>>> d[3]4.'z'5.>>> d['x']6. 17.>>> d[0]8.Traceback (most recent call last):9. File "<pyshell#26>", line 1, in <module>10. d[0]11.KeyError: 0字典操作和方法:len( d ) 返回字典d里面的键值对数目x in d 查询字典d中是否有键x[python]view plain copy print?1.>>> d = {'x':1,'y':3}2.>>> len(d)3. 24.>>> 'x'in d5.True6.>>> 'z'not in d7.Trued [ x ] = y 若键x 存在,则修改x 对应的值为y,若键x 不存在,则在字典d 中增加键值对x : y[python]view plain copy print?1.>>> d2.{'x': 1, 'y': 3}3.>>> d['x'] = 1.54.>>> d5.{'x': 1.5, 'y': 3}6.>>> d['z'] = 57.>>> d8.{'z': 5, 'x': 1.5, 'y': 3}del d[x] 删除字典d 中键为x 的键值对,若x 不存在会出现KeyError[python]view plain copy print?1.>>> d = {'z': 5, 'x': 1.5, 'y': 3}2.>>> del d['x']3.>>> d4.{'z': 5, 'y': 3}5.>>> del d['x']6.Traceback (most recent call last):7. File "<pyshell#66>", line 1, in <module>8.del d['x']9.KeyError: 'x'd.clear() 清空字典dd.copy() 对字典d 进行浅复制,返回一个和d有相同键值对的新字典[python]view plain copy print?1.>>> d2.{'z': 5, 'y': 3}3.>>> d.copy()4.{'z': 5, 'y': 3}d.get( x [ , y]) 返回字典d 中键x 对应的值,键x 不存在的时候返回y,y 的默认值为None[python]view plain copy print?1.>>> d = {'z': 5, 'x': 1.5, 'y': 3}2.>>> d.get('x')3. 1.54.>>> del d['x']5.>>> d.get('x')6.>>> d.get('x','nothing')7.'nothing'd.items() 将字典d 中所有键值对以dict_items的形式返回(Python 2中d.iteritems() 返回一个针对键值对的迭代器对象,Python 3中没有iteritems 方法了)[python]view plain copy print?1.>>> d = {'z': 5, 'x': 1.5, 'y': 3}2.>>> d.items()3.dict_items([('z', 5), ('x', 1.5), ('y', 3)])4.>>> list(d.items())5.[('z', 5), ('x', 1.5), ('y', 3)]d.keys() 将字典d 中所有的键以dict_keys形式返回(Python 2 中d.iterkeys() 返回一个针对键的迭代器对象,Python 3 没有此语法)[python]view plain copy print?1.>>> d.keys()2.dict_keys(['z', 'x', 'y'])3.>>> for x in d.keys():4.print(x)5.6.7.z8.x9.yd.pop( x ) 返回给定键x 对应的值,并将该键值对从字典中删除[python]view plain copy print?1.>>> d = {'z': 5, 'x': 1.5, 'y': 3}2.>>> d.pop('x')3. 1.54.>>> d.pop('x')5.Traceback (most recent call last):6. File "<pyshell#92>", line 1, in <module>7. d.pop('x')8.KeyError: 'x'd.popitem( ) 返回并删除字典d 中随机的键值对[python]view plain copy print?1.>>> d = {'z': 5, 'x': 1.5, 'y': 3}2.>>> d.popitem()3.('z', 5)4.>>> d.popitem()5.('x', 1.5)d.setdefault( x, [ , y ] ) 返回字典d 中键x 对应的值,若键x 不存在,则返回y,并将x : y 作为键值对添加到字典中,y 的默认值为None[python]view plain copy print?1.>>> d = {'z': 5, 'x': 1.5, 'y': 3}2.>>> d.setdefault('x')3. 1.54.>>> del d['x']5.>>> d.setdefault('x','Look!')6.'Look!'7.>>> d8.{'z': 5, 'x': 'Look!', 'y': 3}d.update( x ) 将字典x 所有键值对添加到字典d 中(不重复,重复的键值对用字典x 中的键值对替代字典d 中)[python]view plain copy print?1.>>> d1 = {'x':1, 'y':3}2.>>> d2 = {'x':2, 'z':1.4}3.>>> d1.update(d2)4.>>> d15.{'z': 1.4, 'x': 2, 'y': 3}d.values( ) 将字典里所有的值以dict_values 的形式返回(Python 2 中d.itervalues() 返回针对字典d里所有值的迭代器对象,Python 3无此语法)[python]view plain copy print?1.>>> d12.{'z': 1.4, 'x': 2, 'y': 3}3.>>> d1.values()4.dict_values([1.4, 2, 3])5.>>> list(d1.values())6.[1.4, 2, 3]。

相关主题