ข้อมูลคีย์-ค่าด้วย Dict
ค้นหาค่าด้วยคีย์
ข้อมูลคีย์-ค่าด้วย Dict เป็นบทเรียน Mojo Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Mojo Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ข้อมูลคีย์-ค่าด้วย Dict”
ค้นหาค่าด้วยคีย์ คุณปฏิบัติ Mojo Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Mojo Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Mojo Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ข้อมูลคีย์-ค่าด้วย Dict” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Mojo Academy นี้ได้ไหม
ได้ บทเรียน Mojo Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การสร้างและขยายรายการ
- ข้อมูลคีย์-ค่าด้วย Dict
- รายการสมาชิกไม่ซ้ำด้วย Set
- การเลือกโครงสร้างข้อมูลที่เหมาะสม