“Sortieren Sie die Schlüsselpython” Code-Antworten

Python -Sortierliste in umgekehrt

#1 Changes list
list.sort(reverse=True)
#2 Returns sorted list
sorted(list, reverse=True)
MitroGr

Sortieren Sie die Schlüsselpython

>>> student_tuples = [
...     ('john', 'A', 15),
...     ('jane', 'B', 12),
...     ('dave', 'B', 10),
... ]
>>> sorted(student_tuples, key=lambda student: student[2])   # sort by age
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
Puzzled Pheasant

Sortieren Sie die Schlüsselpython

>>> student_objects = [
...     Student('john', 'A', 15),
...     Student('jane', 'B', 12),
...     Student('dave', 'B', 10),
... ]
>>> sorted(student_objects, key=lambda student: student.age)   # sort by age
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
Nikoooo

Sortieren Sie die Schlüsselpython

>>> class Student:
...     def __init__(self, name, grade, age):
...         self.name = name
...         self.grade = grade
...         self.age = age
...     def __repr__(self):
...         return repr((self.name, self.grade, self.age))
Nikoooo

Ähnliche Antworten wie “Sortieren Sie die Schlüsselpython”

Fragen ähnlich wie “Sortieren Sie die Schlüsselpython”

Weitere verwandte Antworten zu “Sortieren Sie die Schlüsselpython” auf Python

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen