Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Accesso alle Chiavi del Dizionario | Padroneggiare i Dizionari Python
Strutture Dati Python

book
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.

book = {
"title": "Pride and Prejudice",
"author": "Jane Austen",
"year": 1813,
"genre": "Romance"
}
keys = book.keys()
print(keys) # Output: dict_keys(['title', 'author', 'year', 'genre'])
12345678
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } keys = book.keys() print(keys) # Output: dict_keys(['title', 'author', 'year', 'genre'])
copy

Iterare attraverso le chiavi

Puoi iterare attraverso le chiavi in un dizionario usando un ciclo for:

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

for key in book.keys():
print(key)
123456789
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } for key in book.keys(): print(key)
copy

Verifica dell'Esistenza di una Chiave

Usa la parola chiave in per verificare se una chiave specifica esiste nel dizionario:

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

if "author" in book:
print("The 'author' key exists in the dictionary.")
123456789
book = { "title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance" } if "author" in book: print("The 'author' key exists in the dictionary.")
copy
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 metodo append() per riempire la lista all_books con tutti i libri disponibili.

Soluzione

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?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 3
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

expand
ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

some-alt