no way to compare when less than two revisions
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| — | de:modul:m319:learningunits:lu11:verarbeitung [2025/06/23 07:45] (aktuell) – ↷ Seite von modul:m319:learningunits:lu11:verarbeitung nach de:modul:m319:learningunits:lu11:verarbeitung verschoben msuter | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| + | ====== LU11b - Verarbeitungen mit Dictionary ====== | ||
| + | <WRAP center round info 60%> | ||
| + | Dieses Kapitel zeigt Ihnen die Verarbeitungen (Lesen, Schreiben, Löschen) von Dictionaries | ||
| + | </ | ||
| + | |||
| + | ===== Elemente einfügen / ändern ===== | ||
| + | Nachdem der Dictionary erstellt ist, können wir Elemente einfügen bzw. ändern. | ||
| + | Dazu geben wir den Schlüssel in eckigen Klammern **'' | ||
| + | * Ist der Schlüssel bereits vorhanden, so wird der bisherige Wert überschrieben. | ||
| + | * Andernfalls wird ein neuer Schlüssel und Wert eingetragen. | ||
| + | |||
| + | <code python> | ||
| + | |||
| + | colors = {' | ||
| + | colors[' | ||
| + | colors[' | ||
| + | print(colors) | ||
| + | </ | ||
| + | |||
| + | == Output == | ||
| + | < | ||
| + | {' | ||
| + | </ | ||
| + | |||
| + | ==== Elemente löschen ==== | ||
| + | Der Befehl '' | ||
| + | |||
| + | <code python> | ||
| + | colors = {' | ||
| + | del colors[' | ||
| + | print(colors) | ||
| + | </ | ||
| + | |||
| + | == Output == | ||
| + | < | ||
| + | {' | ||
| + | </ | ||
| + | |||
| + | ==== Einzelne Elemente lesen ==== | ||
| + | <WRAP center round info 60%> | ||
| + | Wir können die Element über ihren Schlüssel in den eckigen Klammern lesen. | ||
| + | |||
| + | Wollen wir wissen, ob ein bestimmter Schlüssel exisitert, so verwenden wir '' | ||
| + | </ | ||
| + | |||
| + | <code python> | ||
| + | colors = {' | ||
| + | print(colors[' | ||
| + | if ' | ||
| + | print (' | ||
| + | else: | ||
| + | print ('No pink found' | ||
| + | </ | ||
| + | |||
| + | == Output == | ||
| + | < | ||
| + | #00ff00 | ||
| + | No pink found | ||
| + | </ | ||
| + | |||
| + | === Lesen anhand des Werts === | ||
| + | <WRAP center round info 60%> | ||
| + | Es existiert keine Funktion um ein Element anhand seines Werts zu lesen. | ||
| + | Wir können uns behelfen, indem wir die Schlüssel und Werte in separate Listen umwandeln. | ||
| + | </ | ||
| + | |||
| + | Die Funktion '' | ||
| + | In Kombination mit '' | ||
| + | |||
| + | Die Funktion '' | ||
| + | |||
| + | <code python> | ||
| + | colors = {' | ||
| + | hex_values = list(colors.values()) | ||
| + | color_names = list(colors.keys()) | ||
| + | index = hex_values.index('# | ||
| + | print(color_names[index]) | ||
| + | </ | ||
| + | |||
| + | == Output == | ||
| + | < | ||
| + | green | ||
| + | </ | ||
| + | ==== Alle Elemente verarbeiten ==== | ||
| + | Mit einem **for**-Loop können wir alle Elemente eines Dictionarys verarbeiten. | ||
| + | <code python> | ||
| + | colors = {' | ||
| + | for color_name in colors: | ||
| + | print(color_name) | ||
| + | | ||
| + | for key, value in colors.items(): | ||
| + | print(f' | ||
| + | print(colors) | ||
| + | </ | ||
| + | |||
| + | == Output == | ||
| + | < | ||
| + | red | ||
| + | green | ||
| + | blue | ||
| + | The hexcode for red is #ff0000 | ||
| + | The hexcode for green is #00ff00 | ||
| + | The hexcode for blue is #0000ff | ||
| + | </ | ||
| + | |||
| + | ==== Sortieren ==== | ||
| + | Mit dem **sorted**-Befehl können wir die Elemente nach ihrem Schlüssel sortieren. | ||
| + | |||
| + | <code python> | ||
| + | colors = {' | ||
| + | for name in sorted(colors.items()): | ||
| + | print(f' | ||
| + | </ | ||
| + | |||
| + | == Output == | ||
| + | < | ||
| + | The hexcode for blue is #0000ff | ||
| + | The hexcode for green is #00ff00 | ||
| + | The hexcode for red is #ff0000 | ||
| + | </ | ||
| + | |||
| + | ---- | ||
| + | {{tag> | ||
| + | |||
| + | [[https:// | ||