Dictionaries (records) · 字典(记录)
Store data by name
- A dictionary stores values under names called keys.
- Write pairs inside braces:
{"name": "Sam", "age": 15}. - Each key is unique and points to one value.
按名字存放数据
- 字典(dictionary)把值存放在叫作键(key)的名字下。
- 把键值对写在花括号里:
{"name": "Sam", "age": 15}。 - 每个键都是唯一的,指向一个值。
Get and set values
- Read a value with its key:
person["name"]. - Add or change a value the same way:
person["age"] = 16. - A list uses a number index; a dictionary uses a key.
读取和设置值
- 用键来读取值:
person["name"]。 - 用同样的方式添加或修改值:
person["age"] = 16。 - 列表用数字索引;字典用键。
ages = {"Sam": 12, "Mia": 15}
print(ages["Sam"])
ages["Leo"] = 9
print(ages)
A dictionary as a record
- A record groups related facts about one thing.
- A dictionary is a neat way to hold a record.
- Each key is a field, like
nameorgrade.
用字典表示记录
- 记录(record)把关于某一事物的相关信息组合在一起。
- 字典是保存一条记录的简便方式。
- 每个键就是一个字段(field),例如
name或grade。
pupil = {"name": "Ada", "grade": "A"}
print(pupil["name"])
print(pupil["grade"])
Check and loop
"name" in personisTrueif that key exists.for key in person:visits each key in turn.person.get("x", 0)returns a default if the key is missing.
判断和遍历
- 如果某个键存在,
"name" in person就是True。 for key in person:会依次访问每个键。- 如果键不存在,
person.get("x", 0)会返回一个默认值。
menu = {"tea": 2, "cake": 5}
for item in menu:
print(item, menu[item])
print("tea" in menu)
In Cambridge pseudocode
- A dictionary models the exam's
TYPErecord; each key is a field reached with a dot.
用剑桥伪代码表示
- 字典对应考试里的
TYPE记录;每个键就是一个字段,用点号来访问。
TYPE Student
DECLARE Name : STRING
DECLARE Age : INTEGER
ENDTYPE
DECLARE Pupil : Student
Pupil.Name ← "Sam"
Pupil.Age ← 15
Common mistakes
- Read a value by its key:
d["name"]; a missing key raises a KeyError. - Keys are unique — assigning the same key again overwrites the value.
- Use
d.get(key)when the key might be missing.
常见错误
- 用键读取值:
d["name"];键不存在会抛出 KeyError。 - 键是唯一的——给同一个键再赋值会覆盖旧值。
- 键可能不存在时,用
d.get(key)。
Now you try
- Build or update a dictionary in each task.
- Press Check answer to test your code.
现在轮到你
- 每个任务都要构建或更新一个字典。
- 按检查答案来测试你的代码。
Look up by key · 按键查找
A dictionary jumps straight to the value for a key — no scanning. · 字典按键直接跳到对应的值——不用逐个查找。
Create a dictionary book with two keys: title set to "Python" and pages set to 200. · 创建一个字典 book,有两个键:title 设为 "Python",pages 设为 200。
Click Run to see the output here. · 点击“运行”查看此处输出。
The dictionary student already has name and score. Change score to · 到 85, and add a new key passed set to True. · 字典 student 已经有 name 和 score。把 score 改为 85,再添加一个新键 passed,设为 True。
Click Run to see the output here. · 点击“运行”查看此处输出。
Loop over the dictionary prices and add up its values. Store the total in total (the answer is 10). · 遍历字典 prices,把它的值加起来。把总和存进 total(答案是 10)。
Click Run to see the output here. · 点击“运行”查看此处输出。
Write count_words(text) that returns · 返回值 a dictionary: each word of text (split on spaces) mapped to how many times it appears. count_words("the cat and the hat") gives {"the": 2, "cat": 1, "and": 1, "hat": 1}. · 编写 count_words(text),返回一个字典:text 里的每个单词(按空格分开)对应它出现的次数。count_words("the cat and the hat") 得到 {"the": 2, "cat": 1, "and": 1, "hat": 1}。
Click Run to see the output here. · 点击“运行”查看此处输出。