Dictionary ตอนที่ 2

Python ระดับกลาง

Hugo Bowne-Anderson

Data Scientist at DataCamp

ทบทวน

world = {"afghanistan":30.55, "albania":2.77, "algeria":39.21}

world["albania"]
2.77
world = {"afghanistan":30.55, "albania":2.77, 
         "algeria":39.21, "albania":2.81}

world
{'afghanistan': 30.55, 'albania': 2.81, 'algeria': 39.21}
Python ระดับกลาง

ทบทวน

  • Key ต้องเป็นออบเจกต์ประเภท "immutable"
{0:"hello", True:"dear", "two":"world"}
{0: 'hello', True: 'dear', 'two': 'world'}
{["just", "to", "test"]: "value"}
TypeError: unhashable type: 'list'
Python ระดับกลาง

รัฐซีแลนด์

ch2_2_slides.015.png

1 ที่มา: Wikipedia
Python ระดับกลาง

Dictionary

world["sealand"] = 0.000027

world
{'afghanistan': 30.55, 'albania': 2.81, 
        'algeria': 39.21, 'sealand': 2.7e-05}
"sealand" in world
True
Python ระดับกลาง

Dictionary

world["sealand"] = 0.000028

world
{'afghanistan': 30.55, 'albania': 2.81, 
        'algeria': 39.21, 'sealand': 2.8e-05}
del(world["sealand"])

world
{'afghanistan': 30.55, 'albania': 2.81, 'algeria': 39.21}
Python ระดับกลาง

List vs. Dictionary

Python ระดับกลาง

List vs. Dictionary

Python ระดับกลาง

List vs. Dictionary

List Dictionary
เลือก อัปเดต และลบด้วย [] เลือก อัปเดต และลบด้วย []
Python ระดับกลาง

List vs. Dictionary

List Dictionary
เลือก อัปเดต และลบด้วย [] เลือก อัปเดต และลบด้วย []
Python ระดับกลาง

List vs. Dictionary

List Dictionary
เลือก อัปเดต และลบด้วย [] เลือก อัปเดต และลบด้วย []
ใช้ดัชนีเป็นช่วงตัวเลข
Python ระดับกลาง

List vs. Dictionary

List Dictionary
เลือก อัปเดต และลบด้วย [] เลือก อัปเดต และลบด้วย []
ใช้ดัชนีเป็นช่วงตัวเลข ใช้ key ที่ไม่ซ้ำกันเป็นดัชนี
Python ระดับกลาง

List vs. Dictionary

List Dictionary
เลือก อัปเดต และลบด้วย [] เลือก อัปเดต และลบด้วย []
ใช้ดัชนีเป็นช่วงตัวเลข ใช้ key ที่ไม่ซ้ำกันเป็นดัชนี
ชุดค่าที่ลำดับสำคัญ — เหมาะสำหรับเลือกกลุ่มข้อมูลทั้งชุด
Python ระดับกลาง

List vs. Dictionary

List Dictionary
เลือก อัปเดต และลบด้วย [] เลือก อัปเดต และลบด้วย []
ใช้ดัชนีเป็นช่วงตัวเลข ใช้ key ที่ไม่ซ้ำกันเป็นดัชนี
ชุดค่าที่ลำดับสำคัญ — เหมาะสำหรับเลือกกลุ่มข้อมูลทั้งชุด ตารางค้นหาที่มี key ไม่ซ้ำกัน
Python ระดับกลาง

มาฝึกกันเถอะ!

Python ระดับกลาง

Preparing Video For Download...