Sanakirjan Arvojen Käsittely
Rakennetaan sanakirja, joka edustaa kirjaa kirjastossa:
9
1
2
book = {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813}
print(book)
12book = {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813} print(book)
Tämä sanakirja sisältää kolme avain-arvoparia:
Sanakirjassa voit hakea kohteen viittaamalla sen avaimen. Tässä on esimerkki kirjan julkaisuvuoden hakemisesta:
9
1
2
3
4
book = {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813}
publication_year = book["year"]
print(publication_year) # Output: 1813
1234book = {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813} publication_year = book["year"] print(publication_year) # Output: 1813
Annetussa koodissa:
Python etsii avainta
"year"
sanakirjasta book;Se hakee avainta vastaavan arvon (
1813
) ja määrittää sen muuttujallepublication_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
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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ää?
Kiitos palautteestasi!
Osio 2. Luku 2
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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ä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme