Sanakirjan Avaimien Käsittely
Päästäksesi käsiksi sanakirjan avaimiin Pythonissa, voit käyttää keys()
-metodia. Tämä palauttaa näkymäobjektin, joka näyttää kaikki sanakirjan avaimet.
9
1
2
3
4
5
6
7
8
book = {
"title": "Pride and Prejudice",
"author": "Jane Austen",
"year": 1813,
"genre": "Romance"
}
keys = book.keys()
print(keys) # Output: dict_keys(['title', 'author', 'year', 'genre'])
12345678book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } keys = book.keys() print(keys) # Output: dict_keys(['title', 'author', 'year', 'genre'])
Avainten läpikäynti
Voit käydä läpi sanakirjan avaimet käyttämällä for
-silmukkaa:
9
1
2
3
4
5
6
7
8
9
book = {
"title": "Pride and Prejudice",
"author": "Jane Austen",
"year": 1813,
"genre": "Romance"
}
for key in book.keys():
print(key)
123456789book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } for key in book.keys(): print(key)
Tarkistetaan avaimen olemassaolo
Käytä in-avainsanaa tarkistaaksesi, onko tietty avain olemassa sanakirjassa:
9
1
2
3
4
5
6
7
8
9
book = {
"title": "Pride and Prejudice",
"author": "Jane Austen",
"year": 1813,
"genre": "Romance"
}
if "author" in book:
print("The 'author' key exists in the dictionary.")
123456789book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } if "author" in book: print("The 'author' key exists in the dictionary.")
Tehtävä
Swipe to start coding
Sinulle annetaan sanakirja authors_books
, jossa avain on kirjailija ja arvo on luettelo heidän kirjojensa nimikkeistä.
- Alusta muuttuja
keys
sanakirjan avaimien luetteloksi. - Alusta muuttuja
all_books
luetteloksi kaikista saatavilla olevista kirjan nimikkeistä. - Käytä
for
-silmukkaa saadaksesi kirjaluettelot kirjailijan mukaan. - Käytä sisäkkäistä
for
-silmukkaa jaappend()
-menetelmää täyttääksesiall_books
-luettelon kaikilla saatavilla olevilla kirjoilla.
Ratkaisu
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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']
}
keys = authors_books.keys()
all_books = []
for key in keys:
for book in authors_books[key]:
all_books.append(book)
# Testing
print("The list of all books in the library:", all_books)
Oliko kaikki selvää?
Kiitos palautteestasi!
Osio 2. Luku 3
single
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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']
}
keys = ___
all_books = []
for ___ in keys:
for book in ___[key]:
___
# Testing
print("The list of all books in the library:", all_books)
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme