Python Dictionaries: Schlüssel-Wert-Paare

PythonPythonBeginner
Jetzt üben

This tutorial is from open-source community. Access the source code

💡 Dieser Artikel wurde von AI-Assistenten übersetzt. Um die englische Version anzuzeigen, können Sie hier klicken

Einführung

Dictionaries sind eine grundlegende Datenstruktur in Python. Sie ermöglichen es Ihnen, Schlüssel-Wert-Paare zu speichern und werden häufig verwendet, um reale Objekte oder Konzepte darzustellen. In dieser Herausforderung werden Sie mit Dictionaries und ihren Werten arbeiten.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/DataStructuresGroup(["Data Structures"]) python(("Python")) -.-> python/FunctionsGroup(["Functions"]) python(("Python")) -.-> python/PythonStandardLibraryGroup(["Python Standard Library"]) python/BasicConceptsGroup -.-> python/variables_data_types("Variables and Data Types") python/BasicConceptsGroup -.-> python/comments("Comments") python/DataStructuresGroup -.-> python/lists("Lists") python/DataStructuresGroup -.-> python/dictionaries("Dictionaries") python/FunctionsGroup -.-> python/function_definition("Function Definition") python/FunctionsGroup -.-> python/build_in_functions("Build-in Functions") python/PythonStandardLibraryGroup -.-> python/data_collections("Data Collections") subgraph Lab Skills python/variables_data_types -.-> lab-13740{{"Python Dictionaries: Schlüssel-Wert-Paare"}} python/comments -.-> lab-13740{{"Python Dictionaries: Schlüssel-Wert-Paare"}} python/lists -.-> lab-13740{{"Python Dictionaries: Schlüssel-Wert-Paare"}} python/dictionaries -.-> lab-13740{{"Python Dictionaries: Schlüssel-Wert-Paare"}} python/function_definition -.-> lab-13740{{"Python Dictionaries: Schlüssel-Wert-Paare"}} python/build_in_functions -.-> lab-13740{{"Python Dictionaries: Schlüssel-Wert-Paare"}} python/data_collections -.-> lab-13740{{"Python Dictionaries: Schlüssel-Wert-Paare"}} end

Dictionary-Werte

Es wird Ihnen ein flaches Dictionary gegeben, und Sie müssen eine Funktion erstellen, die eine flache Liste aller Werte im Dictionary zurückgibt. Ihre Aufgabe besteht darin, die Funktion values_only(flat_dict) zu implementieren, die ein flaches Dictionary als Argument nimmt und eine Liste aller Werte im Dictionary zurückgibt.

Um dieses Problem zu lösen, können Sie die dict.values()-Methode verwenden, um die Werte im gegebenen Dictionary zurückzugeben. Anschließend können Sie das Ergebnis mithilfe der list()-Funktion in eine Liste umwandeln.

def values_only(flat_dict):
  return list(flat_dict.values())
ages = {
  'Peter': 10,
  'Isabel': 11,
  'Anna': 9,
}
values_only(ages) ## [10, 11, 9]

Zusammenfassung

In dieser Herausforderung haben Sie gelernt, wie Sie alle Werte aus einem flachen Dictionary extrahieren und als Liste zurückgeben. Sie haben die dict.values()-Methode verwendet, um die Werte zu erhalten und anschließend das Ergebnis mithilfe der list()-Funktion in eine Liste umgewandelt. Dies ist eine nützliche Technik, wenn Sie mit Dictionaries in Python arbeiten.