Woordenboeken

Introductie tot Python voor developers

Jasmin Ludolf

Senior Data Science Content Developer

Ingrediëntenlijst

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

# Hoeveelheden (in gram) quantities = [500, 400, 15, 20, 30, 5]
Introductie tot Python voor developers

Dictionary

  • Dictionary = key-valueparen

Woordenboek met annotaties: het woord (key) en de definitie (value)

1 https://unsplash.com/@sandym10
Introductie tot Python voor developers

Waarom dictionaries?

  • username
  • email
  • preferences

 

  • ip_address
  • location

$$

  • Receptschaler: koppel ingrediënten aan hoeveelheden

Gebruikersinfo

Computernetwerk

1 User image by Bilobaba Vladimir, Aviavlad; https://unsplash.com/@jjying
Introductie tot Python voor developers

Een dictionary maken

# Een dictionary maken
recipe = 
Introductie tot Python voor developers

Een dictionary maken

# Een dictionary maken
recipe = {
Introductie tot Python voor developers

Een dictionary maken

# Een dictionary maken
recipe = {"pasta"
Introductie tot Python voor developers

Een dictionary maken

# Een dictionary maken
recipe = {"pasta":
Introductie tot Python voor developers

Een dictionary maken

# Een dictionary maken
recipe = {"pasta": 500
Introductie tot Python voor developers

Een dictionary maken

# Een dictionary maken
recipe = {"pasta": 500,
Introductie tot Python voor developers

Een dictionary maken

# Een dictionary maken
recipe = {"pasta": 500,
          "tomatoes": 400,
Introductie tot Python voor developers

Een dictionary maken

# Een dictionary maken
recipe = {"pasta": 500,
          "tomatoes": 400, 
          "garlic": 15,
          "basil": 20, 
          "olive oil": 30,
          "salt": 5
Introductie tot Python voor developers

Een dictionary maken

# Een dictionary maken
recipe = {"pasta": 500,
          "tomatoes": 400, 
          "garlic": 15,
          "basil": 20, 
          "olive oil": 30,
          "salt": 5}
Introductie tot Python voor developers

Waarde opvragen via de key

  • Dictionaries zijn geordend

    • Je kunt waarden opvragen via subsetten op de key
  • Hoeveel pasta hebben we nodig?

# Zoek de hoeveelheid van het ingrediënt
print(recipe["pasta"])
500
Introductie tot Python voor developers

Alle waarden opvragen

# Alle waarden uit een dictionary halen
print(recipe.values())
dict_values([500, 400, 15, 20, 30, 5])
Introductie tot Python voor developers

Alle keys opvragen

# Alle keys uit een dictionary halen
print(recipe.keys())
dict_keys(['pasta', 'tomatoes', 'garlic', 'basil', 'olive oil', 'salt'])
Introductie tot Python voor developers

Volledige dictionary bekijken

# Print de dictionary
print(recipe)
{'pasta': 500, 'tomatoes': 400, 'garlic': 15, 'basil': 20, 'olive oil': 30,
'salt': 5}
# Alle items (key-valueparen) ophalen
print(recipe.items())
dict_items([('pasta', 500), ('tomatoes', 400), ('garlic', 15), ('basil', 20), 
('olive oil', 30), ('salt', 5)])
Introductie tot Python voor developers

Key-valuepaar toevoegen

# Een nieuw key-valuepaar toevoegen
recipe["parmesan"] = 50
print(recipe)
{'pasta': 500, 'tomatoes': 400, 'garlic': 15, 'basil': 20, 'olive oil': 30,
'salt': 5, 'parmesan': 50}
Introductie tot Python voor developers

Waarde bijwerken

print(recipe)
{'pasta': 500, 'tomatoes': 400, 'garlic': 15, 'basil': 20, 'olive oil': 30,
'salt': 5, 'parmesan': 50}
# Een waarde bijwerken voor een bestaande key
recipe["pasta"] = 1000
print(recipe)
{'pasta': 1000, 'tomatoes': 400, 'garlic': 15, 'basil': 20, 'olive oil': 30,
'salt': 5, 'parmesan': 50}
Introductie tot Python voor developers

Dubbele keys

# Een dictionary maken met een dubbele key
recipe = {"pasta": 500, "garlic": 5, 
          "garlic": 15, "basil": 20, 
          "olive oil": 30, "salt": 5}

# Print de waarde van de dubbele key print(recipe["garlic"])
15
Introductie tot Python voor developers

Laten we oefenen!

Introductie tot Python voor developers

Preparing Video For Download...