Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Verwendung des Del-Schlüsselworts: Entfernen von Wörterbucheinträgen | Wörterbuch
Python-Datenstrukturen

book
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.

python
del dict[key]
book = {
"title": "Pride and Prejudice",
"author": "Jane Austen",
"year": 1813,
"genre": "Romance",
"copies": 5
}

# Deleting a key value
del book["copies"]

print(book)
123456789101112
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance", "copies": 5 } # Deleting a key value del book["copies"] print(book)
copy

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.

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
1234567891011
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
copy
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

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?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 5
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

expand
ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

some-alt