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.

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

可编程网络自动化

可编程网络自动化

Jason Edelman, Scott S. Lowe, Matt Oswalt
Python实用技能学习指南

Python实用技能学习指南

Posts & Telecom Press, Robert Smallshire, Austin Bingham
量子计算机编程:从入门到实践

量子计算机编程:从入门到实践

Eric R. Johnston, Nicholas Harrigan, Mercedes Gimeno-Segovia
Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano

Publisher Resources

ISBN: 9787115586223