本文参考自《python基础教程 (第二版)》
操作 | 语法 | 举例 | 结果 |
建立字典 |
dict() 1.以关键字参数建立字典 2.以其他映射作为参数建立字典 |
1.d = dict(name=‘Gumby‘, age=42) d 2.items = [(‘name‘, ‘Gumby‘), (‘age‘, 42)] d = dict(items) d |
{‘age‘: 42, ‘name‘: ‘Gumby‘}
{‘age‘: 42, ‘name‘: ‘Gumby‘} |
fromkeys() 1.给指定的键建立新的字典 2.用dict调用fromkeys 3.如果不想使用None作默认值,也可以自己提供默认值 |
1.{}.fromkeys([‘name‘, ‘age‘]) 2.dict.fromkeys([‘name‘, ‘age‘]) 3.dict.fromkeys([‘name‘,‘age‘], ‘(unknown)‘) |
{‘age‘: None, ‘name‘: None} {‘age‘: None, ‘name‘: None} {‘age‘: ‘(unknown)‘, ‘name‘: ‘(unknown)‘} |
|
清除字典 |
clear() 1.清除字典中的所有项 2.无返回值(或返回值为None) |
1.d = {‘name‘: ‘Gumby‘, ‘age‘: 42} value = d.clear() d 2. print value |
{} None |
浅复制 |
copy() 1.返回一个具有相同键值对的新字典 2.当在副本中替换值的时候,原始字典不受影响 3.当在副本中修改某个值(不是替换)时,原始字典会改变 |
1.x = {‘username‘: ‘admin‘, ‘machines‘: [‘foo‘, ‘bar‘, ‘baz‘]} y = x.copy() y
2.y[‘username‘] = ‘mhl‘ y x
3.y[‘machines‘].remove(‘bar‘) y x
|
{‘username‘: ‘admin‘, ‘machines‘: [‘foo‘, ‘bar‘, ‘baz‘]}
{‘username‘: ‘mhl‘, ‘machines‘: [‘foo‘, ‘bar‘, ‘baz‘]} {‘username‘: ‘admin‘, ‘machines‘: [‘foo‘, ‘bar‘, ‘baz‘]}
{‘username‘: ‘mhl‘, ‘machines‘: [‘foo‘, ‘baz‘]} {‘username‘: ‘admin‘, ‘machines‘: [‘foo‘, ‘baz‘]} |
深复制 |
deepcopy() 1.当在副本中修改某个值(不是替换)时,原始字典不受影响 |
from copy import deepcopy d = {‘names‘: [‘Alfred‘, ‘Bertrand‘]} deep = deepcopy(d) deep[‘names‘].append(‘Clive‘) deep d |
{‘names‘: [‘Alfred‘, ‘Bertrand‘, ‘Clive‘]} {‘names‘: [‘Alfred‘, ‘Bertrand‘]} |
访问字典项/设置默认值 |
get() 1.更宽松的访问字典项,访问一个不存在的键时,不会报错 2.自定义默认值 |
1.d = {} print d.get(‘name‘) 2.d.get(‘name‘, ‘N/A‘) |
None
‘N/A‘ |
setdefault() 1.更宽松的访问字典项,访问一个不存在的键时,不会报错 2.自定义默认值 3.当键存在时,就返回相应的值 |
1.d = {} print d.setdefault(‘name‘) 2.d = {} d.setdefault(‘name‘, ‘N/A‘) 3.d = {‘name‘: ‘Gumby‘} d.setdefault(‘name‘, ‘N/A‘) |
None
‘N/A‘
‘Gumby‘ |
|
检查键 |
has_key() 1.d.has_key(key)相当于k in d |
d = {} d.has_key(‘name‘) d[‘name‘] = ‘Eric‘ d.has_key(‘name‘) |
False
True |
迭代 |
键的迭代:keys()和iterkeys() 1.keys()将字典中的键以列表形式返回 2.iterkeys()返回键的迭代器 |
1.d = {‘title‘: ‘Python Web Site‘, ‘url‘: ‘http://www.python.org‘, ‘spam‘: 0} d.keys() 2.ik = d.iterkeys() ik list(ik) |
[‘url‘, ‘spam‘, ‘title‘]
<dictionary-keyiterator object at 0x0000000002C1FA98> [‘url‘, ‘spam‘, ‘title‘] |
值的迭代:values()和itervalues() 1.values()将字典中的值以列表形式返回 2.itervalues()返回值的迭代器 |
1.d = {‘title‘: ‘Python Web Site‘, ‘url‘: ‘http://www.python.org‘, ‘spam‘: 0} d.values() 2.iv = d.itervalues() iv list(iv) |
[‘http://www.python.org‘, 0, ‘Python Web Site‘]
<dictionary-valueiterator object at 0x0000000002C1FAE8> [‘http://www.python.org‘, 0, ‘Python Web Site‘] |
|
键值对的迭代:items()和iteritems() 1.items()将所有字典项以列表形式返回 2.iteritems()返回所有字典项的迭代器 |
1.d = {‘title‘: ‘Python Web Site‘, ‘url‘: ‘http://www.python.org‘, ‘spam‘: 0} d.items() 2.it = d.iteritems() it list(it) |
[(‘url‘, ‘http://www.python.org‘), (‘spam‘, 0), (‘title‘, ‘Python Web Site‘)]
<dictionary-itemiterator object at 0x0000000002C1FB38> [(‘url‘, ‘http://www.python.org‘), (‘spam‘, 0), (‘title‘, ‘Python Web Site‘)] |
|
移除
|
pop() 1.获得给定键的值,然后将键值对从字典中移除 |
d = {‘x‘: 1, ‘y‘: 2| d.pop(‘x‘) d |
1 {‘y‘: 2} |
popitem() 1.移除随机的项 |
d = {‘url‘: ‘http://www.python.org‘, ‘spam‘: 0, ‘title‘: ‘Python Web Site‘} d.popitem() d |
(‘url‘, ‘http://www.python.org‘) {‘spam‘: 0, ‘title‘: ‘Python Web Site‘} |
|
更新 |
update() 1.利用一个字典项更新另外一个字典 |
d = {‘title‘: ‘Python Web Site‘, ‘url‘: ‘http://www.python.org‘, ‘changed‘: ‘Mar 14 22:09:15 MET 2008‘} x = {‘title‘: ‘Python Language Website‘} d.update(x) d |
{‘url‘: ‘http://www.python.org‘, ‘changed‘: ‘Mar 14 22:09:15 MET 2008‘, ‘title‘: ‘Python Language Website‘} |