Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Tilgang til Dictionary Keys | Mastering Python Ordbøker
Python Datastrukturer

book
Tilgang til Dictionary Keys

For å få tilgang til nøklene i en ordbok i Python, kan du bruke keys()-metoden. Dette returnerer et visningsobjekt som viser alle nøklene i ordboken.

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

Iterere gjennom nøkler

Du kan iterere gjennom nøklene i en ordbok ved å bruke en for-løkke:

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

Sjekke om en nøkkel eksisterer

Bruk in-nøkkelordet for å sjekke om en spesifikk nøkkel finnes i ordboken:

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
Oppgave

Swipe to start coding

Du har gitt en ordbok authors_books, hvor nøkkelen er forfatteren og verdien er en liste over deres boktitler.

  • Initialiser variabelen keys som en liste over ordbokens nøkler.
  • Initialiser variabelen all_books som en liste over alle tilgjengelige boktitler.
  • Bruk en for-løkke for å få boklistene etter forfatter.
  • Bruk en nestet for-løkke og append()-metoden for å fylle all_books-listen med alle tilgjengelige bøker.

Løsning

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)
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 3
single

single

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)

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

some-alt