“Urutkan Python Kunci” Kode Jawaban

Daftar Sortir Python secara terbalik

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

Urutkan Python Kunci

>>> 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

Urutkan Python Kunci

>>> 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

Urutkan Python Kunci

>>> 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

Jawaban yang mirip dengan “Urutkan Python Kunci”

Pertanyaan yang mirip dengan “Urutkan Python Kunci”

Lebih banyak jawaban terkait untuk “Urutkan Python Kunci” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya