Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Zugriff auf Wörterbuchwerte | Wörterbuch
Python-Datenstrukturen

book
Zugriff auf Wörterbuchwerte

Lassen Sie uns ein Wörterbuch erstellen, um ein Buch in einer Bibliothek darzustellen:

book = {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813}
print(book)
12
book = {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813} print(book)
copy

Dieses Wörterbuch enthält drei Schlüssel-Wert-Paare:

In einem Wörterbuch können Sie ein Element abrufen, indem Sie auf seinen Schlüssel verweisen. Hier ist ein Beispiel für den Zugriff auf das Veröffentlichungsjahr des Buches:

book = {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813}

publication_year = book["year"]
print(publication_year) # Output: 1813
1234
book = {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813} publication_year = book["year"] print(publication_year) # Output: 1813
copy

Im bereitgestellten Code:

  • Python sucht nach dem Schlüssel "year" im Buchwörterbuch;
  • Es ruft den mit dem Schlüssel verknüpften Wert (1813) ab und weist ihn der Variablen publication_year zu.

Der Wert wird dann gedruckt, was zeigt, wie Schlüssel einen schnellen und spezifischen Zugriff auf Wörterbuchwerte ermöglichen.

Aufgabe

Swipe to start coding

Sie haben ein Wörterbuch authors_books, bei dem der Buchautor der Schlüssel und die Liste der Buchtitel der Wert ist.

  • Initialisieren Sie die Variable kings_books als Liste von Stephen Kings Büchern.
  • Initialisieren Sie die Variable shekspeares_books als Liste von Shakespeares Büchern.
  • Sie sollten die Variablen initialisieren, indem Sie die Daten aus dem Wörterbuch mit dem Schlüssel abrufen.

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
kings_books = authors_books['Stephen King']
shekspeares_books = authors_books['William Shakespeare']

# Testing
print(f'William Shakespeare\'s books: {shekspeares_books} \nStephen King\'s books: {kings_books}')
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 2
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 = ___
shekspeares_books = ___

# Testing
print(f'William Shakespeare\'s books: {shekspeares_books} \nStephen King\'s books: {kings_books}')
toggle bottom row
some-alt