辞書型

開発者のための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 annotated with the word (key) and definition (value)

1 https://unsplash.com/@sandym10
開発者のためのPython入門

辞書型を使う理由は?

  • username
  • email
  • preferences
  • ip_address
  • location

$$

  • レシピ換算ツール: 材料を数量にリンク

User info

Computer network

1 User image by Bilobaba Vladimir, Aviavlad; https://unsplash.com/@jjying
開発者のためのPython入門

辞書を作成する

# Creating a dictionary
recipe = 
開発者のためのPython入門

辞書を作成する

# Creating a dictionary
recipe = {
開発者のためのPython入門

辞書を作成する

# Creating a dictionary
recipe = {"pasta"
開発者のためのPython入門

辞書を作成する

# Creating a dictionary
recipe = {"pasta":
開発者のためのPython入門

辞書を作成する

# Creating a dictionary
recipe = {"pasta": 500
開発者のためのPython入門

辞書を作成する

# Creating a dictionary
recipe = {"pasta": 500,
開発者のためのPython入門

辞書を作成する

# Creating a dictionary
recipe = {"pasta": 500,
          "tomatoes": 400,
開発者のためのPython入門

辞書を作成する

# Creating a dictionary
recipe = {"pasta": 500,
          "tomatoes": 400, 
          "garlic": 15,
          "basil": 20, 
          "olive oil": 30,
          "salt": 5
開発者のためのPython入門

辞書を作成する

# Creating a dictionary
recipe = {"pasta": 500,
          "tomatoes": 400, 
          "garlic": 15,
          "basil": 20, 
          "olive oil": 30,
          "salt": 5}
開発者のためのPython入門

キーに基づいて値を取り出す

  • 辞書は順序付けられている

    • キーでのサブセット化により値を取り出せる
  • パスタはどれくらい必要?

# 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入門

すべてのキーを取り出す

# Retrieve all keys in a dictionary
print(recipe.keys())
dict_keys(['pasta', 'tomatoes', 'garlic', 'basil', 'olive oil', 'salt'])
開発者のためのPython入門

辞書全体を表示する

# 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入門

キーと値のペアを追加する

# 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入門

重複したキー

# 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...