Bruk av pop() Metoden: Slette Elementer med Returverdier
pop()
-metoden i Python-ordbøker lar deg fjerne et nøkkel-verdi-par basert på nøkkelen og returnerer den tilsvarende verdien. Denne metoden er spesielt nyttig når du trenger å hente og behandle en verdi samtidig som du fjerner den fra ordboken.
Syntaksen er:
pythondictionary.pop(key)
99
1
2
3
4
5
6
7
8
9
10
11
12
book = {
"title": "Pride and Prejudice",
"author": "Jane Austen",
"year": 1813,
"copies": 5
}
# Remove the 'copies' key and retrieve its value
removed_copies = book.pop("copies")
print("Updated dictionary:", book)
print("Removed value:", removed_copies)
123456789101112book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "copies": 5 } # Remove the 'copies' key and retrieve its value removed_copies = book.pop("copies") print("Updated dictionary:", book) print("Removed value:", removed_copies)
Hvis nøkkelen ikke finnes i ordboken, kaster Python et KeyError
-unntak.
9
1
2
3
4
5
6
7
8
book = {
"title": "Pride and Prejudice",
"author": "Jane Austen",
"year": 1813
}
# Attempting to remove a non-existent key
removed_genre = book.pop("genre")
12345678book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813 } # Attempting to remove a non-existent key removed_genre = book.pop("genre")
Oppgave
Swipe to start coding
Du har fått den samme ordboken authors_books
.
- Fjern bøkene til
"Stephen King"
fra ordboken og lagre dem i variabelenkings_books
. - Bruk
pop()
-metoden for å oppnå dette.
Løsning
99
1
2
3
4
5
6
7
8
9
10
11
12
13
authors_books = {
'William Shakespeare': ['Hamlet', 'Macbeth', 'Romeo and Juliet', 'Othello'],
'J.K. Rowling': ['Harry Potter and the Sorcerer\'s Stone', 'Harry Potter and the Chamber of Secrets', 'Harry Potter and the Prisoner of Azkaban', 'Harry Potter and the Goblet of Fire'],
'George Orwell': ['1984', 'Animal Farm', 'Coming Up for Air'],
'Stephen King': ['It', 'The Shining', 'Carrie', 'Misery'],
'Agatha Christie': ['Murder on the Orient Express', 'The Murder of Roger Ackroyd', 'And Then There Were None', 'Death on the Nile']
}
# Write your code here
kings_books = authors_books.pop('Stephen King')
# Testing
print(f"Updated dictionary: {authors_books}.\nStephen King\'s books: {kings_books}")
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 2. Kapittel 6
single
99
1
2
3
4
5
6
7
8
9
10
11
12
13
authors_books = {
'William Shakespeare': ['Hamlet', 'Macbeth', 'Romeo and Juliet', 'Othello'],
'J.K. Rowling': ['Harry Potter and the Sorcerer\'s Stone', 'Harry Potter and the Chamber of Secrets', 'Harry Potter and the Prisoner of Azkaban', 'Harry Potter and the Goblet of Fire'],
'George Orwell': ['1984', 'Animal Farm', 'Coming Up for Air'],
'Stephen King': ['It', 'The Shining', 'Carrie', 'Misery'],
'Agatha Christie': ['Murder on the Orient Express', 'The Murder of Roger Ackroyd', 'And Then There Were None', 'Death on the Nile']
}
# Write your code here
kings_books = ___
# Testing
print(f"Updated dictionary: {authors_books}.\nStephen King\'s books: {kings_books}")
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår