Verwendung des Del-Schlüsselworts: Entfernen von Wörterbucheinträgen
In Python können Sie Schlüssel aus einem Wörterbuch mit dem del
-Schlüsselwort entfernen. Dies ermöglicht es Ihnen, bestimmte Einträge zu löschen oder sogar das gesamte Wörterbuch zu leeren. Lassen Sie uns anhand eines bibliotheksbezogenen Beispiels erkunden, wie dies funktioniert.
pythondel dict[key]
99
1
2
3
4
5
6
7
8
9
10
11
12
book = {
"title": "Pride and Prejudice",
"author": "Jane Austen",
"year": 1813,
"genre": "Romance",
"copies": 5
}
# Deleting a key value
del book["copies"]
print(book)
123456789101112book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance", "copies": 5 } # Deleting a key value del book["copies"] print(book)
Löschen des gesamten Wörterbuchs
Wenn Sie das gesamte Wörterbuch löschen möchten, können Sie dies mit del
tun. Seien Sie jedoch vorsichtig—diese Aktion entfernt das Wörterbuch vollständig, und jeder weitere Verweis darauf wird einen Fehler auslösen.
99
1
2
3
4
5
6
7
8
9
10
11
book = {
"title": "Pride and Prejudice",
"author": "Jane Austen",
"year": 1813,
"genre": "Romance"
}
# Deleting the entire dictionary
del book
# Attempting to print the dictionary after deletion will cause an error
print(book) # This line will raise a NameError
1234567891011book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } # Deleting the entire dictionary del book # Attempting to print the dictionary after deletion will cause an error print(book) # This line will raise a NameError
Aufgabe
Swipe to start coding
Wir arbeiten weiterhin mit dem Wörterbuch authors_books
.
- Entfernen Sie das Element mit dem Schlüssel
"J.K. Rowling"
aus dem Wörterbuch. - Verwenden Sie das
del
Schlüsselwort, um dies zu tun.
Lösung
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
del authors_books['J.K. Rowling']
# Testing
print("Updated dictionary:", authors_books)
War alles klar?
Danke für Ihr Feedback!
Abschnitt 2. Kapitel 5
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
___
# Testing
print("Updated dictionary:", authors_books)
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen