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

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

by Bill Lubanovic
March 2022
Intermediate to advanced
522 pages
13h 52m
Chinese
Posts & Telecom Press
Content preview from Python语言及其应用(第2版)
98
8
字典和集合
我们怎么才能知道字典中的某个词拼写错了呢?
——
Steven W
right
8.1
字典
字典
类似于列表,但字典项的顺序并不重要,而且也不是按照
0
1
这样的偏移来选择
的。你需要指定与每个
关联的不重复的
。键多是字符串,但实际上也可以是任何
Python
的不可变类型
:布尔值、整数、浮点数、元组、字符串等。因为字典是可变的,所
以可以添加、删除、修改字典中的键
值元素
。如果你用过的语言只支持数组或列表,那
你一定会爱上字典的。
在其他语言中,字典也叫作
关联数组
哈希
哈希图
。在
Python
中,字典
dictionary
)也简称为
dict
8.1.1
使用
{}
创建字典
要想创建字典,可以将以逗号分隔的一系列
key
:
value
对放入花括号(
{}
)内。不包含任
何键或值的空字典是最简单的字典:
>>> empty_dict = {}
>>> empty_dict
{}
字典和集合
99
下面来创建一个小字典,其中包含一些取自
Ambrose Bierce
所著的《魔鬼词典》(
The
Devil’s Dictionary
)的引文:
>>> bierce = {
... "day": "A period of twenty-four hours, mostly misspent",
... ...
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