Dictionaries: Unterschied zwischen den Versionen

Aus Micropython Referenz
Zur Navigation springen Zur Suche springen
Peter (Diskussion | Beiträge)
Die Seite wurde neu angelegt: „ == Links:== * https://realpython.com/python-dictionary-comprehension/“
 
Peter (Diskussion | Beiträge)
Keine Bearbeitungszusammenfassung
 
Zeile 1: Zeile 1:
== Dictionaries sortieren==
Quelle: https://aashishkumar12376.medium.com/6-python-dictionary-tricks-to-supercharge-your-application-ed5abe24d3bb <br>
=== 5. Sort Dictionaries by Keys or Values===
Need a sorted dictionary? Use sorted() with items():
<pre>
data = {"banana": 3, "apple": 5, "orange": 2}
sorted_by_key = dict(sorted(data.items())) 
sorted_by_value = dict(sorted(data.items(), key=lambda x: x[1]))
print(sorted_by_key)  # {'apple': 5, 'banana': 3, 'orange': 2}
print(sorted_by_value) # {'orange': 2, 'banana': 3, 'apple': 5}
</pre>




== Links:==
== Links:==
* https://realpython.com/python-dictionary-comprehension/
* https://realpython.com/python-dictionary-comprehension/

Aktuelle Version vom 15. März 2025, 18:44 Uhr


Dictionaries sortieren[Bearbeiten | Quelltext bearbeiten]

Quelle: https://aashishkumar12376.medium.com/6-python-dictionary-tricks-to-supercharge-your-application-ed5abe24d3bb

5. Sort Dictionaries by Keys or Values[Bearbeiten | Quelltext bearbeiten]

Need a sorted dictionary? Use sorted() with items():

data = {"banana": 3, "apple": 5, "orange": 2}

sorted_by_key = dict(sorted(data.items()))  
sorted_by_value = dict(sorted(data.items(), key=lambda x: x[1]))

print(sorted_by_key)   # {'apple': 5, 'banana': 3, 'orange': 2}
print(sorted_by_value) # {'orange': 2, 'banana': 3, 'apple': 5}


Links:[Bearbeiten | Quelltext bearbeiten]