Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Sanakirjan Arvojen Käsittely | Mastering Python Dictionaries
Pythonin Tietorakenteet

book
Sanakirjan Arvojen Käsittely

Rakennetaan sanakirja, joka edustaa kirjaa kirjastossa:

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

Tämä sanakirja sisältää kolme avain-arvoparia:

Sanakirjassa voit hakea kohteen viittaamalla sen avaimen. Tässä on esimerkki kirjan julkaisuvuoden hakemisesta:

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

Annetussa koodissa:

  • Python etsii avainta "year" sanakirjasta book;

  • Se hakee avainta vastaavan arvon (1813) ja määrittää sen muuttujalle publication_year.

Arvo tulostetaan sitten, mikä osoittaa, kuinka avaimet mahdollistavat nopean ja tarkan pääsyn sanakirjan arvoihin.

Tehtävä

Swipe to start coding

Sinulle annetaan sanakirja authors_books, jossa kirjan kirjoittaja on avain ja lista kirjan nimikkeistä on arvo.

  • Alusta muuttuja kings_books Stephen Kingin kirjojen listana.
  • Alusta muuttuja shekspeares_books Shakespearen kirjojen listana.
  • Sinun tulisi alustaa muuttujat hakemalla tiedot sanakirjasta avaimen avulla.

Ratkaisu

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}')
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 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}')

Kysy tekoälyä

expand
ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

some-alt