Dictionary

Python เบื้องต้นสำหรับนักพัฒนา

Jasmin Ludolf

Senior Data Science Content Developer

รายการส่วนผสม

ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]

# Ingredient quantities (in grams) quantities = [500, 400, 15, 20, 30, 5]
Python เบื้องต้นสำหรับนักพัฒนา

Dictionary

  • Dictionary = คู่ key-value

Dictionary ที่มีการระบุคำ (key) และความหมาย (value)

1 https://unsplash.com/@sandym10
Python เบื้องต้นสำหรับนักพัฒนา

ทำไมต้องใช้ Dictionary?

  • username
  • email
  • preferences

 

  • ip_address
  • location

$$

  • ตัวปรับสูตรอาหาร: เชื่อมส่วนผสมกับปริมาณ

ข้อมูลผู้ใช้

เครือข่ายคอมพิวเตอร์

1 ภาพผู้ใช้โดย Bilobaba Vladimir, Aviavlad; https://unsplash.com/@jjying
Python เบื้องต้นสำหรับนักพัฒนา

การสร้าง Dictionary

# Creating a dictionary
recipe = 
Python เบื้องต้นสำหรับนักพัฒนา

การสร้าง Dictionary

# Creating a dictionary
recipe = {
Python เบื้องต้นสำหรับนักพัฒนา

การสร้าง Dictionary

# Creating a dictionary
recipe = {"pasta"
Python เบื้องต้นสำหรับนักพัฒนา

การสร้าง Dictionary

# Creating a dictionary
recipe = {"pasta":
Python เบื้องต้นสำหรับนักพัฒนา

การสร้าง Dictionary

# Creating a dictionary
recipe = {"pasta": 500
Python เบื้องต้นสำหรับนักพัฒนา

การสร้าง Dictionary

# Creating a dictionary
recipe = {"pasta": 500,
Python เบื้องต้นสำหรับนักพัฒนา

การสร้าง Dictionary

# Creating a dictionary
recipe = {"pasta": 500,
          "tomatoes": 400,
Python เบื้องต้นสำหรับนักพัฒนา

การสร้าง Dictionary

# Creating a dictionary
recipe = {"pasta": 500,
          "tomatoes": 400, 
          "garlic": 15,
          "basil": 20, 
          "olive oil": 30,
          "salt": 5
Python เบื้องต้นสำหรับนักพัฒนา

การสร้าง Dictionary

# Creating a dictionary
recipe = {"pasta": 500,
          "tomatoes": 400, 
          "garlic": 15,
          "basil": 20, 
          "olive oil": 30,
          "salt": 5}
Python เบื้องต้นสำหรับนักพัฒนา

การเข้าถึงค่าด้วย key

  • Dictionary มีลำดับ

    • เข้าถึงค่าได้โดยการ subset ด้วย key
  • ต้องการพาสต้าเท่าไร?

# Find the ingredient's quantity
print(recipe["pasta"])
500
Python เบื้องต้นสำหรับนักพัฒนา

การเข้าถึงค่าทั้งหมด

# Get all values from a dictionary
print(recipe.values())
dict_values([500, 400, 15, 20, 30, 5])
Python เบื้องต้นสำหรับนักพัฒนา

การเข้าถึง key ทั้งหมด

# Retrieve all keys in a dictionary
print(recipe.keys())
dict_keys(['pasta', 'tomatoes', 'garlic', 'basil', 'olive oil', 'salt'])
Python เบื้องต้นสำหรับนักพัฒนา

การดู Dictionary ทั้งหมด

# Print the dictionary
print(recipe)
{'pasta': 500, 'tomatoes': 400, 'garlic': 15, 'basil': 20, 'olive oil': 30,
'salt': 5}
# Get all items (key-value pairs)
print(recipe.items())
dict_items([('pasta', 500), ('tomatoes', 400), ('garlic', 15), ('basil', 20), 
('olive oil', 30), ('salt', 5)])
Python เบื้องต้นสำหรับนักพัฒนา

การเพิ่มคู่ key-value

# Add a new key-value pair
recipe["parmesan"] = 50
print(recipe)
{'pasta': 500, 'tomatoes': 400, 'garlic': 15, 'basil': 20, 'olive oil': 30,
'salt': 5, 'parmesan': 50}
Python เบื้องต้นสำหรับนักพัฒนา

การอัปเดตค่า

print(recipe)
{'pasta': 500, 'tomatoes': 400, 'garlic': 15, 'basil': 20, 'olive oil': 30,
'salt': 5, 'parmesan': 50}
# Updating a value associated with an existing key
recipe["pasta"] = 1000
print(recipe)
{'pasta': 1000, 'tomatoes': 400, 'garlic': 15, 'basil': 20, 'olive oil': 30,
'salt': 5, 'parmesan': 50}
Python เบื้องต้นสำหรับนักพัฒนา

Key ที่ซ้ำกัน

# Creating a dictionary with a duplicate key
recipe = {"pasta": 500, "garlic": 5, 
          "garlic": 15, "basil": 20, 
          "olive oil": 30, "salt": 5}

# Print the duplicate key's value print(recipe["garlic"])
15
Python เบื้องต้นสำหรับนักพัฒนา

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

Python เบื้องต้นสำหรับนักพัฒนา

Preparing Video For Download...