“Python -Sortierliste” Code-Antworten

Liste der Liste der Liste

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

Python -Sort

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

Python -Sortierliste

>>> sorted([5, 2, 3, 1, 4])
[1, 2, 3, 4, 5]
Happy Herring

Python -Sortierliste

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

Python -Sortierliste

prime_numbers = [11, 3, 7, 5, 2]

# sort the list
prime_numbers.sort()
print(prime_numbers)

# Output: [2, 3, 5, 7, 11]
Sparkling Salmon

Python -Sortierliste

# 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")
vettiyankan

Ähnliche Antworten wie “Python -Sortierliste”

Fragen ähnlich wie “Python -Sortierliste”

Weitere verwandte Antworten zu “Python -Sortierliste” auf Python

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen