Accesso alle Chiavi del Dizionario
Per accedere alle chiavi di un dizionario in Python, puoi usare il metodo keys()
. Questo restituisce un oggetto vista che visualizza tutte le chiavi nel dizionario.
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'])
Iterare attraverso le chiavi
Puoi iterare attraverso le chiavi in un dizionario usando un ciclo for
:
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)
Verifica dell'Esistenza di una Chiave
Usa la parola chiave in per verificare se una chiave specifica esiste nel dizionario:
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.")
Compito
Swipe to start coding
Ti viene dato un dizionario authors_books
, dove la chiave è l'autore e il valore è una lista dei titoli dei loro libri.
- Inizializza la variabile
keys
come una lista delle chiavi del dizionario. - Inizializza la variabile
all_books
come una lista di tutti i titoli di libri disponibili. - Usa un ciclo
for
per ottenere le liste di libri per autore. - Usa un ciclo
for
annidato e il metodoappend()
per riempire la listaall_books
con tutti i libri disponibili.
Soluzione
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)
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 2. Capitolo 3
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)
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione