Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Legge til Elementer i en Ordbok: Oppdatere Nøkkel-Verdi-Par | Mastering Python Ordbøker
Python Datastrukturer

book
Legge til Elementer i en Ordbok: Oppdatere Nøkkel-Verdi-Par

Ordbøker er dynamiske, noe som betyr at du kan legge til, oppdatere eller fjerne elementer etter at ordboken er opprettet. La oss utforske hvordan vi kan legge til nye elementer i en ordbok.

Start med å opprette en ordbok kalt book med noen innledende detaljer:

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

For å gjøre ordboken vår mer komplett, kan vi legge til et nytt nøkkel-verdi-par til den. For eksempel, vi ønsker kanskje å legge til genre av boken:

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

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

Etter å ha lagt til det nye nøkkel-verdi paret, inkluderer ordboken nå:

Oppdatere et eksisterende nøkkel-verdi-par

Hvis du trenger å oppdatere verdien til en eksisterende nøkkel, kan du gjøre det ved å tilordne en ny verdi til den nøkkelen. For eksempel, la oss anta at bokas utgivelsesår ble korrigert til 1815:

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

book["year"] = 1815
print(book)
1234
book = {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance"} book["year"] = 1815 print(book)
copy

Merk

Enkelte ' ' og doble " " anførselstegn i Python er utskiftbare og likeverdige.

Oppgave

Swipe to start coding

Du har gitt en ordbok authors_books og 2 variabler: author_to_add og fitzgeralds_books.

  • Legg til en ny forfatter og deres liste over bøker i ordboken authors_books.
  • Forfatteren skal være nøkkelen, og listen over bøker skal være verdien.

Løsning

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']
}

author_to_add = 'F. Scott Fitzgerald'
fitzgeralds_books = ['The Great Gatsby', 'Tender Is the Night', 'This Side of Paradise', 'The Beautiful and Damned', 'The Last Tycoon']

# Write your code here
authors_books[author_to_add] = fitzgeralds_books

# Testing
print("Updated dictionary:", authors_books)
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 4
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']
}

author_to_add = 'F. Scott Fitzgerald'
fitzgeralds_books = ['The Great Gatsby', 'Tender Is the Night', 'This Side of Paradise', 'The Beautiful and Damned', 'The Last Tycoon']

# Write your code here
___

# Testing
print("Updated dictionary:", authors_books)
toggle bottom row
some-alt