每天CookBook之Python-020

  • 字典的合并
  • ChainMap的使用
a = {'x': 1, 'z': 3}
b = {'y': 2, 'z': 4}

merged = dict(b)
merged.update(a)
print(merged)
{'x': 1, 'z': 3, 'y': 2}
from collections import ChainMap

a = {'x': 1, 'z': 3}
b = {'y': 2, 'z': 4}

c = ChainMap(a,b)

print(c)
print(c['x'])
print(c['y'])
print(c['z'])
print(list(c.keys()))
print(list(c.values()))
c['z'] = 10
c['w'] = 40
del c['x']
del c['z']
print(a)
print(b)
print(c['z'])
ChainMap({'z': 3, 'x': 1}, {'z': 4, 'y': 2})
1
2
3
['x', 'z', 'y']
[1, 3, 2]
{'w': 40}
{'z': 4, 'y': 2}
4
posted @ 2016-07-10 20:05  4Thing  阅读(95)  评论(0编辑  收藏  举报