Python 3语法小记(四)字典 dictionary
来源:互联网 发布:u5 gif动画制作软件 编辑:程序博客网 时间:2024/05/14 22:41
字典是Python里面一种无序存储结构,存储的是键值对 key - value。关键字应该为不可变类型,如字符串、整数、包含不可变对象的元组。
字典的创建很简单,用 d = {key1 : value2, key2 : value2}的形式就可以创建一个新的字典,当然也可以通过 dict 接受一个含有键,值的序列对或者关键字参数来创建字典。
键可以是多种类型,但键是唯一的不重复的,值可以不唯一
>>> d = {'a':1, 'b':2}>>> d{'b': 2, 'a': 1}>>> L = [('Jonh',18), ('Nancy',19)]>>> d = dict(L) #通过包含键值的列表创建>>> d{'Jonh': 18, 'Nancy': 19} >>> T = tuple(L)>>> T(('Jonh', 18), ('Nancy', 19))>>> d = dict(T) #通过包含键值的元组创建>>> d{'Jonh': 18, 'Nancy': 19}>>> d = dict(x = 1, y = 3) #通过关键字参数创建>>> d{'x': 1, 'y': 3}>>> d[3] = 'z'>>> d{3: 'z', 'x': 1, 'y': 3}
>>> d{3: 'z', 'y': 3}>>> L1 = [1,2,3]>>> d.fromkeys(L1){1: None, 2: None, 3: None}>>> {}.fromkeys(L1,'nothing'){1: 'nothing', 2: 'nothing', 3: 'nothing'}>>> dict.fromkeys(L1,'over'){1: 'over', 2: 'over', 3: 'over'}
字典是无序的,所以不能通过索引来获取值,要通过键来找到关联值。对于不存在的键,会出现错误KeyError
>>> d{3: 'z', 'x': 1, 'y': 3}>>> d[3]'z'>>> d['x']1>>> d[0]Traceback (most recent call last): File "<pyshell#26>", line 1, in <module> d[0]KeyError: 0
len( d ) 返回字典d里面的键值对数目
x in d 查询字典d中是否有键 x
>>> d = {'x':1,'y':3}>>> len(d)2>>> 'x' in dTrue>>> 'z' not in dTrue
d [ x ] = y 若键 x 存在,则修改 x 对应的值为 y, 若键 x 不存在,则在字典 d 中增加键值对 x : y
>>> d{'x': 1, 'y': 3}>>> d['x'] = 1.5>>> d{'x': 1.5, 'y': 3}>>> d['z'] = 5>>> d{'z': 5, 'x': 1.5, 'y': 3}
del d[x] 删除字典 d 中键为 x 的键值对,若 x 不存在会出现 KeyError
>>> d = {'z': 5, 'x': 1.5, 'y': 3}>>> del d['x']>>> d{'z': 5, 'y': 3}>>> del d['x']Traceback (most recent call last): File "<pyshell#66>", line 1, in <module> del d['x']KeyError: 'x'
d.clear() 清空字典d
d.copy() 对字典 d 进行浅复制,返回一个和d有相同键值对的新字典
>>> d{'z': 5, 'y': 3}>>> d.copy(){'z': 5, 'y': 3}
d.get( x [ , y]) 返回字典 d 中键 x 对应的值,键 x 不存在的时候返回 y, y 的默认值为None
>>> d = {'z': 5, 'x': 1.5, 'y': 3}>>> d.get('x')1.5>>> del d['x']>>> d.get('x')>>> d.get('x','nothing')'nothing'
d.items() 将字典 d 中所有键值对以dict_items的形式返回(Python 2中d.iteritems() 返回一个针对键值对的迭代器对象,Python 3中没有 iteritems 方法了)
>>> d = {'z': 5, 'x': 1.5, 'y': 3}>>> d.items()dict_items([('z', 5), ('x', 1.5), ('y', 3)])>>> list(d.items())[('z', 5), ('x', 1.5), ('y', 3)]
d.keys() 将字典 d 中所有的键以dict_keys形式返回(Python 2 中d.iterkeys() 返回一个针对键的迭代器对象,Python 3 没有此语法)
>>> d.keys()dict_keys(['z', 'x', 'y'])>>> for x in d.keys():print(x)zxy
d.pop( x ) 返回给定键 x 对应的值,并将该键值对从字典中删除
>>> d = {'z': 5, 'x': 1.5, 'y': 3}>>> d.pop('x')1.5>>> d.pop('x')Traceback (most recent call last): File "<pyshell#92>", line 1, in <module> d.pop('x')KeyError: 'x'
d.popitem( ) 返回并删除字典 d 中随机的键值对
>>> d = {'z': 5, 'x': 1.5, 'y': 3}>>> d.popitem()('z', 5)>>> d.popitem()('x', 1.5)
d.setdefault( x, [ , y ] ) 返回字典 d 中键 x 对应的值,若键 x 不存在,则返回 y, 并将 x : y 作为键值对添加到字典中,y 的默认值为 None
>>> d = {'z': 5, 'x': 1.5, 'y': 3}>>> d.setdefault('x')1.5>>> del d['x']>>> d.setdefault('x','Look!')'Look!'>>> d{'z': 5, 'x': 'Look!', 'y': 3}
d.update( x ) 将字典 x 所有键值对添加到字典 d 中(不重复,重复的键值对用字典 x 中的键值对替代字典 d 中)
>>> d1 = {'x':1, 'y':3}>>> d2 = {'x':2, 'z':1.4}>>> d1.update(d2)>>> d1{'z': 1.4, 'x': 2, 'y': 3}
d.values( ) 将字典里所有的值以dict_values 的形式返回(Python 2 中d.itervalues() 返回针对字典d里所有值的迭代器对象,Python 3无此语法)
>>> d1{'z': 1.4, 'x': 2, 'y': 3}>>> d1.values()dict_values([1.4, 2, 3])>>> list(d1.values())[1.4, 2, 3]
- Python 3语法小记(四)字典 dictionary
- Python字典(Dictionary)
- python dictionary(字典)
- Python中的字典(Dictionary)
- Python 3语法小记(五)字符串
- Python 3语法小记(七)函数
- Python学习练习3----Dictionary(字典)
- python学习(1)-字典 (Dictionary)
- Python 字典(dictionary)的排序
- Python dictionary 字典
- python Dictionary字典使用
- Python字典 (dictionary)
- Python 字典(Dictionary)
- Python 字典(Dictionary)
- Python字典 (dictionary)
- Python 字典(Dictionary)
- Python字典dictionary
- Python菜鸟--字典(Dictionary)
- Java中线程应用总结
- mysql 权限管理浅析
- openresty 下 echo命令简单尝试
- Sharepoint 2013 和Workflow Manager 安装配置问题
- CF Croc Champ 2013 - Finals (online version, Div. 1) C(greedy)
- Python 3语法小记(四)字典 dictionary
- Java中nio与buffer
- hdu 3507 Print Article
- 项目发布及演讲技巧
- c#检测有效串口的方法
- Android(Java):jni写文件打印调试信息
- 在cmd中跳转路径
- POJ 1401 (13.07.17)
- asp.net mvc3 对HttPost来的表单使用moq进行单元测试