Skip to Content
Python语言及其应用(第2版)
book

Python语言及其应用(第2版)

by Bill Lubanovic
March 2022
Intermediate to advanced content levelIntermediate to advanced
522 pages
13h 52m
Chinese
Posts & Telecom Press
Content preview from Python语言及其应用(第2版)
字典和集合
103
8.1.9
使用
len()
获取长度
统计“键
值”对的个数。
>>> len(signals)
3
8.1.10
使用
{**a, **b}
合并字典
Python 3.5
开始,出现了一种合并字典的新方法:使用
**
(独角兽闪光),它在第
9
中还有大为不同的用法。
>>> first = {'a': 'agony', 'b': 'bliss'}
>>> second = {'b': 'bagels', 'c': 'candy'}
>>> {**first, **second}
{'a': 'agony', 'b': 'bagels', 'c': 'candy'}
实际上,可以传入多个字典:
>>> third = {'d': 'donuts'}
>>> {**first, **third, **second}
{'a': 'agony', 'b': 'bagels', 'd': 'donuts', 'c': 'candy'}
通过这种方法得到的都是浅复制。如果想获得键和值的完整副本,不希望与原始字典有任
何关联,可以参见
8.1.18
节有关
deepcopy()
的讨论。
8.1.11
使用
update()
合并字典
可以使用
update()
函数将一个字典的键和值复制到另一个字典。
先定义包含巨蟒剧团全体成员的字典
pythons
>>> pythons = {
... 'Chapman': 'Graham',
... 'Cleese': 'John',
... 'Gilliam': 'Terry', ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano
Python实用技能学习指南

Python实用技能学习指南

Posts & Telecom Press, Robert Smallshire, Austin Bingham
Python技术基础视频教程

Python技术基础视频教程

保罗·J·戴特尔
Python面向对象编程指南

Python面向对象编程指南

Posts & Telecom Press, Steven F. Lott

Publisher Resources

ISBN: 9787115586223