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
 +</WRAP>
  
 +
 +===== 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 **''['' '']''** an.
 +  * Ist der Schlüssel bereits vorhanden, so wird der bisherige Wert überschrieben.
 +  * Andernfalls wird ein neuer Schlüssel und Wert eingetragen.
 +
 +<code python>
 +
 +colors = {'red': '#ff0000', 'green': '#00ff00', 'blue': '#0000ff'}
 +colors['indigo'] = '#4B0082'  # adds a new key/value
 +colors['blue'  = '#000080'  # replaces the value for 'blue'
 +print(colors)
 +</code>
 +
 +== Output ==
 +<code>
 +{'red': '#ff0000', 'green': '#00ff00', 'blue': '#000080', 'indigo': '#4B0082'}
 +</code>
 +
 +==== Elemente löschen ====
 +Der Befehl ''del'' löscht ein Element aus dem Dictionary.
 +
 +<code python>
 +colors = {'red': '#ff0000', 'green': '#00ff00', 'blue': '#0000ff'}
 +del colors['green'  # Removes the element with the key 'green'
 +print(colors)
 +</code>
 +
 +== Output ==
 +<code>
 +{'red': '#ff0000', 'blue': '#0000ff'}
 +</code>
 +
 +==== 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 ''key'' **''in''** ''dictionary''.
 +</WRAP>
 +
 +<code python>
 +colors = {'red': '#ff0000', 'green': '#00ff00', 'blue': '#0000ff'}
 +print(colors['green'])  # prints the value #00ff00
 +if 'pink' in colors:
 +    print ('Pink!!')
 +else:
 +    print ('No pink found')
 +</code>
 +
 +== Output ==
 +<code>
 +#00ff00
 +No pink found
 +</code>
 +
 +=== 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.
 +</WRAP>
 +
 +Die Funktion ''values()'' liest alle Werte eines Dictionaries und erzeugt ein View-Objekt als Returnwert. 
 +In Kombination mit ''list(...)'' können wir dieses View-Objekt in eine Liste umwandeln.
 +
 +Die Funktion ''keys()'' funktioniert ähnlich und liefert eine View-Objekt aller Schlüssel.
 +
 +<code python>
 +colors = {'red': '#ff0000', 'green': '#00ff00', 'blue': '#0000ff'}
 +hex_values = list(colors.values())  # make a list of all values
 +color_names = list(colors.keys())   # make a list of all keys
 +index = hex_values.index('#00ff00') # search the index of the value
 +print(color_names[index])           # print the key by using the index
 +</code>
 +
 +== Output ==
 +<code>
 +green
 +</code>
 +==== Alle Elemente verarbeiten ====
 +Mit einem **for**-Loop können wir alle Elemente eines Dictionarys verarbeiten.
 +<code python>
 +colors = {'red': '#ff0000', 'green': '#00ff00', 'blue': '#0000ff'}
 +for color_name in colors:    # loops through the keys
 +    print(color_name)
 +    
 +for key, value in colors.items():    # loops through the keys and values
 +    print(f'The hexcode for {key} is {value}')
 +print(colors)
 +</code>
 +
 +== Output ==
 +<code>
 +red
 +green
 +blue
 +The hexcode for red is #ff0000
 +The hexcode for green is #00ff00
 +The hexcode for blue is #0000ff
 +</code>
 +
 +==== Sortieren ====
 +Mit dem **sorted**-Befehl können wir die Elemente nach ihrem Schlüssel sortieren.
 +
 +<code python>
 +colors = {'red': '#ff0000', 'green': '#00ff00', 'blue': '#0000ff'}
 +for name in sorted(colors.items()):
 +    print(f'The hexcode for {name} is {colors[name]}')
 +</code>
 +
 +== Output ==
 +<code>
 +The hexcode for blue is #0000ff
 +The hexcode for green is #00ff00
 +The hexcode for red is #ff0000
 +</code>
 +
 +----
 +{{tag>M319-C1G M319-C1F M319-C1E}}
 +
 +[[https://creativecommons.org/licenses/by-nc-sa/4.0/|{{https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png}}]] Marcel Suter 
  • de/modul/m319/learningunits/lu11/verarbeitung.txt
  • Zuletzt geändert: 2025/06/23 07:45
  • von msuter