Dictionarys

Einführung in Python für die Softwareentwicklung

Jasmin Ludolf

Senior Data Science Content Developer

Zutatenliste

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

# Ingredient quantities (in grams) quantities = [500, 400, 15, 20, 30, 5]
Einführung in Python für die Softwareentwicklung

Dictionary

  • Dictionary = Schlüssel-Wert-Paare

Ausschnitt aus einem Wörterbuch mit Wörtern (Schlüssel) und ihren Definitionen (Werte)

1 https://unsplash.com/@sandym10
Einführung in Python für die Softwareentwicklung

Zweck von Dictionarys

  • username
  • email
  • preferences

 

  • ip_address
  • location

$$

  • Rezeptprogramm: Verknüpfung von Zutaten mit Mengenangaben

Symbolhafte Darstellung von Benutzerdaten

Computernetzwerk

1 Benutzerbild von Bilobaba Vladimir, Aviavlad; https://unsplash.com/@jjying
Einführung in Python für die Softwareentwicklung

Dictionary erstellen

# Creating a dictionary
recipe = 
Einführung in Python für die Softwareentwicklung

Dictionary erstellen

# Creating a dictionary
recipe = {
Einführung in Python für die Softwareentwicklung

Dictionary erstellen

# Creating a dictionary
recipe = {"pasta"
Einführung in Python für die Softwareentwicklung

Dictionary erstellen

# Creating a dictionary
recipe = {"pasta":
Einführung in Python für die Softwareentwicklung

Dictionary erstellen

# Creating a dictionary
recipe = {"pasta": 500
Einführung in Python für die Softwareentwicklung

Dictionary erstellen

# Creating a dictionary
recipe = {"pasta": 500,
Einführung in Python für die Softwareentwicklung

Dictionary erstellen

# Creating a dictionary
recipe = {"pasta": 500,
          "tomatoes": 400,
Einführung in Python für die Softwareentwicklung

Dictionary erstellen

# Creating a dictionary
recipe = {"pasta": 500,
          "tomatoes": 400, 
          "garlic": 15,
          "basil": 20, 
          "olive oil": 30,
          "salt": 5
Einführung in Python für die Softwareentwicklung

Dictionary erstellen

# Creating a dictionary
recipe = {"pasta": 500,
          "tomatoes": 400, 
          "garlic": 15,
          "basil": 20, 
          "olive oil": 30,
          "salt": 5}
Einführung in Python für die Softwareentwicklung

Wert anhand des Schlüssels abrufen

  • Elemente im Dictionary sind geordnet

    • Abruf von Werten durch Teilmengenbildung anhand der Schlüssel
  • Wie viel Nudeln brauchen wir?

# Find the ingredient's quantity
print(recipe["pasta"])
500
Einführung in Python für die Softwareentwicklung

Alle Werte abrufen

# Get all values from a dictionary
print(recipe.values())
dict_values([500, 400, 15, 20, 30, 5])
Einführung in Python für die Softwareentwicklung

Alle Schlüssel abrufen

# Retrieve all keys in a dictionary
print(recipe.keys())
dict_keys(['pasta', 'tomatoes', 'garlic', 'basil', 'olive oil', 'salt'])
Einführung in Python für die Softwareentwicklung

Komplettes Dictionary anzeigen

# 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)])
Einführung in Python für die Softwareentwicklung

Schlüssel-Wert-Paar hinzufügen

# 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}
Einführung in Python für die Softwareentwicklung

Wert aktualisieren

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}
Einführung in Python für die Softwareentwicklung

Schlüsselduplikate

# 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
Einführung in Python für die Softwareentwicklung

Lass uns üben!

Einführung in Python für die Softwareentwicklung

Preparing Video For Download...