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.
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'])
Iterere gjennom nøkler
Du kan iterere gjennom nøklene i en ordbok ved å bruke en for
-løkke:
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)
Sjekke om en nøkkel eksisterer
Bruk in-nøkkelordet for å sjekke om en spesifikk nøkkel finnes i ordboken:
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.")
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 ogappend()
-metoden for å fylleall_books
-listen med alle tilgjengelige bøker.
Løsning
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)
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 2. Kapittel 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)
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår