Dictionaries

Introduktion till Python för utvecklare

Jasmin Ludolf

Senior Data Science Content Developer

Ingredienslista

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

# Ingredient quantities (in grams) quantities = [500, 400, 15, 20, 30, 5]
Introduktion till Python för utvecklare

Dictionary

  • Dictionary = nyckel-värde-par

Dictionary med ordet (nyckel) och definition (värde) markerade

1 https://unsplash.com/@sandym10
Introduktion till Python för utvecklare

Varför använda dictionaries?

  • username
  • email
  • preferences

 

  • ip_address
  • location

$$

  • Receptskalning: koppla ingredienser till mängder

Användarinfo

Datornätverk

1 Användarbild av Bilobaba Vladimir, Aviavlad; https://unsplash.com/@jjying
Introduktion till Python för utvecklare

Skapa en dictionary

# Creating a dictionary
recipe = 
Introduktion till Python för utvecklare

Skapa en dictionary

# Creating a dictionary
recipe = {
Introduktion till Python för utvecklare

Skapa en dictionary

# Creating a dictionary
recipe = {"pasta"
Introduktion till Python för utvecklare

Skapa en dictionary

# Creating a dictionary
recipe = {"pasta":
Introduktion till Python för utvecklare

Skapa en dictionary

# Creating a dictionary
recipe = {"pasta": 500
Introduktion till Python för utvecklare

Skapa en dictionary

# Creating a dictionary
recipe = {"pasta": 500,
Introduktion till Python för utvecklare

Skapa en dictionary

# Creating a dictionary
recipe = {"pasta": 500,
          "tomatoes": 400,
Introduktion till Python för utvecklare

Skapa en dictionary

# Creating a dictionary
recipe = {"pasta": 500,
          "tomatoes": 400, 
          "garlic": 15,
          "basil": 20, 
          "olive oil": 30,
          "salt": 5
Introduktion till Python för utvecklare

Skapa en dictionary

# Creating a dictionary
recipe = {"pasta": 500,
          "tomatoes": 400, 
          "garlic": 15,
          "basil": 20, 
          "olive oil": 30,
          "salt": 5}
Introduktion till Python för utvecklare

Hämta ett värde via nyckeln

  • Dictionaries är ordnade

    • Värden kan hämtas via indexering med nyckeln
  • Hur mycket pasta behöver vi?

# Find the ingredient's quantity
print(recipe["pasta"])
500
Introduktion till Python för utvecklare

Hämta alla värden

# Get all values from a dictionary
print(recipe.values())
dict_values([500, 400, 15, 20, 30, 5])
Introduktion till Python för utvecklare

Hämta alla nycklar

# Retrieve all keys in a dictionary
print(recipe.keys())
dict_keys(['pasta', 'tomatoes', 'garlic', 'basil', 'olive oil', 'salt'])
Introduktion till Python för utvecklare

Visa hela dictionaryn

# 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)])
Introduktion till Python för utvecklare

Lägga till ett nyckel-värde-par

# 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}
Introduktion till Python för utvecklare

Uppdatera ett värde

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}
Introduktion till Python för utvecklare

Duplicerade nycklar

# 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
Introduktion till Python för utvecklare

Nu kör vi en övning!

Introduktion till Python för utvecklare

Preparing Video For Download...