Skip to Content
Python入门指南, 3rd Edition
book

Python入门指南, 3rd Edition

by Bill Lubanovic
September 2025
Intermediate to advanced
660 pages
7h 15m
Chinese
O'Reilly Media, Inc.
Content preview from Python入门指南, 3rd Edition

第 9 章 词典和集合 字典和集合

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

如果字典中的某个单词拼写错误,我们怎么知道呢?

史蒂文-赖特

字典是特别有用的键值数据结构,在 Python 中随处可见它们的应用。 集合只是由唯一键组成的包,通常非常方便。

字典

字典 类似于列表 ,但项目的顺序并不重要,也不是通过偏移量(如 0 或 1)来选择的。相反,您需要指定一个唯一的来关联每个。 这个键通常是一个字符串,但它可以是 Python 的任何不可变类型:布尔、整数、浮点数、元组、字符串和其它类型,甚至是您定义的类型。 字典是可变的,因此您可以添加、删除和更改它们的键值元素。 如果您使用过只支持数组或列表的语言,您就会喜欢字典

注意

在 其他语言中,字典可能被称为关联数组哈希哈希图。在 Python 中,字典也被称为dict,以节省音节,并让少男们窃笑。

用 {} 创建

{}要在中创建字典,需要在逗号分隔的 key:value最简单的字典是空的,不包含任何键或值:

>>> empty_dict = {}
>>> empty_dict
{}

让我们用 Ambrose Bierce 的《The Devil'sDictionary》中的引文创建一个小词典:

>>> bierce = {
...     "day": "A period of twenty-four hours, mostly misspent",
...     "positive": "Mistaken at the top of one's voice",
...     "misfortune": "The kind of fortune that never misses",
...     }
>>>

在交互式解释器中输入字典的名称,就会打印出它的键和

>>> bierce
{'day': 'A period of twenty-four hours, mostly misspent',
'positive': "Mistaken at the top of one's voice",
'misfortune': 'The kind of fortune that never misses'}
注意

在 Python 中,在 list、tuple 或 dictionary 的最后一项后面留一个逗号是没有问题的。 另外,当您在大括号中键入键和值时,您不需要缩进,就像我在上一个示例中所做的那样。 缩进只是有助于可读性。

用 dict() 创建

有些 人不喜欢键入这么多大括号和引号。您也可以通过向dict() 函数传递命名参数和值来创建字典。

下面是传统的方法:

>>> acme_customer = {'first': 'Wile', 'middle': 'E', 'last': 'Coyote'}
>>> acme_customer
{'first': 'Wile', 'middle': 'E', 'last': 'Coyote'}

这种方法使用dict()

>>> acme_customer = dict(first="Wile", middle="E", last="Coyote")
>>> acme_customer
{'first': 'Wile', 'middle': 'E', 'last': 'Coyote'}

第二种方法的一个限制是参数名必须是合法的变量名(无空格、无保留字):

>>>  ...
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.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

生成式人工智能设计模式

生成式人工智能设计模式

Valliappa Lakshmanan, Hannes Hapke

Publisher Resources

ISBN: 9798341668898