Список сортировки списка
>>> lis = [[1,4,7],[3,6,9],[2,59,8]]
>>> sorted(lis, key=lambda x: x[0])
[[1, 4, 7], [2, 59, 8], [3, 6, 9]]
Bloody Bug
>>> lis = [[1,4,7],[3,6,9],[2,59,8]]
>>> sorted(lis, key=lambda x: x[0])
[[1, 4, 7], [2, 59, 8], [3, 6, 9]]
>>> 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)]
>>> sorted([5, 2, 3, 1, 4])
[1, 2, 3, 4, 5]
prime_numbers = [11, 3, 7, 5, 2]
# sorting the list in ascending order
prime_numbers.sort()
print(prime_numbers)
# Output: [2, 3, 5, 7, 11]
prime_numbers = [11, 3, 7, 5, 2]
# sort the list
prime_numbers.sort()
print(prime_numbers)
# Output: [2, 3, 5, 7, 11]
# Sort with an inner object
# Here it will sort with "book_no"
# [
# {
# "key": "book-key-1",
# "book_no": 1,
# "name": "My Book Name 1"
# },
# {
# "key": "book-key-2",
# "book_no": 2,
# "name": "My Book Name 2"
# }
# ]
def sortOnNumber(e):
return e['book_no']
@app.get('/getBooks')
def getBooks():
res = next(booksDb.fetch())
res.sort(key=sortOnNumber)
if res:
return res
raise HTTPException(404,"Not found")