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.
名前でデータを保存
- 辞書は、キーと呼ばれる名前の下に値を保存します。
- 中括弧内にペアを記述します:
{"name": "Sam", "age": 15}。 - 各キーは一意であり、1つの値を指し示します。
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.
辞書をレコードとして
- レコードは、ある事柄に関する関連する事実をグループ化します。
- 辞書はレコードを保持するための整理された方法です。
- 各キーはフィールドであり、
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.
Cambridge擬似コードにおける表現
- 辞書は試験の
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.
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.
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).
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}.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。