Dictによるキーと値のデータ
キーで値を検索します
「Dictによるキーと値のデータ」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMojo Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Mojo Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Look Up by Key
When you want to find a value by a name instead of a position, use a Dict. It maps keys to values. 🔑
Two Types in Brackets
A Dict declares two types: the key type and the value type, like Dict[String, Int].
var ages = Dict[String, Int]()Add a Pair
Store a value by assigning through its key in brackets. This inserts the key-value pair into the dict.
ages["Ada"] = 36Read a Value
Fetch a value by putting its key in brackets. The dict returns whatever you stored under that key.
var a = ages["Ada"]Update in Place
Assigning to an existing key overwrites its value. Keys stay unique, so you never get duplicates.
ages["Ada"] = 37Check Membership
Test whether a key exists with the in operator. It returns True or False without raising an error.
if "Ada" in ages:
print("found")Size of a Dict
Count the pairs with len. It reports how many key-value entries the dict currently holds.
var count = len(ages)Iterate the Keys
Looping a dict walks its keys. Use each key to reach its value inside the loop body.
for name in ages:
print(name)Remove a Pair
Delete an entry with the pop method, passing the key. It removes the pair and returns its value.
var removed = ages.pop("Ada")Keys Must Be Hashable
A dict finds values fast because keys are hashable. Strings and Ints work great as keys.
Fast Average Lookup
Lookups, inserts, and updates are roughly constant time. That speed is why dicts power so much real code.
Quick Check
A Dict already has the key "Ada". You assign ages["Ada"] = 37. What happens to the dict size?
Recap
A Dict maps unique keys to values for fast lookup. Use brackets to set and get, in to check, and len to count. 🎯
よくある質問
「Dictによるキーと値のデータ」レッスンは無料ですか?
はい。「Dictによるキーと値のデータ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。
「Dictによるキーと値のデータ」で何を学びますか?
キーで値を検索します ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Mojo Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「Dictによるキーと値のデータ」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMojo Academyレッスンでコードを書いて実行できますか?
はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Listの構築と拡張
- Dictによるキーと値のデータ
- Setによる一意な項目の管理
- 適切なコレクションの選択