“cara mengurutkan daftar daftar dalam python” Kode Jawaban

Urutkan daftar daftar

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

cara mengurutkan daftar daftar dalam python

from operator import itemgetter
A = [[10, 8], [90, 2], [45, 6]]
print("Sorted List A based on index 0: % s" % (sorted(A, key=itemgetter(0))))
B = [[50, 'Yes'], [20, 'No'], [100, 'Maybe']]
print("Sorted List B based on index 1: % s" % (sorted(B, key=itemgetter(1))))
Tough Tarantula

cara mengurutkan daftar dalam python

l=[1,3,2,5]
l= sorted(l)
print(l)
#output=[1, 2, 3, 5]
#or reverse the order:
l=[1,3,2,5]
l= sorted(l,reverse=True)
print(l)
#output=[5, 3, 2, 1]
SimTheGreat

cara mengurutkan daftar daftar dalam python

# You can use a lambda function, but you'll have to specify the index of the sorting key.

A = [[100, 'Yes'], [40, 'Maybe'], [60, 'No']]
print("Sorted List A based on index 0: % s" % (sorted(A, key=lambda x:x[0])))
B = [[2, 'Dog'], [0, 'Bird'], [7, 'Cat']]
print("Sorted List A based on index 1: % s" % (sorted(B, key=lambda x:x[1])))

# Also, you can use .sort() if you want to sort just by the first index

A = [[55, 90], [45, 89], [90, 70]]
A.sort()
print("New sorted list A is % s" % (A))
A.sort(reverse=True)
print("New reverse sorted list A is % s" % (A))

# You can even change the key sort, if you want to sort by length for example:

A = [[5, 90, 'Hi', 66], [80, 99], [56, 32, 80]]
A.sort(key=len) # <-
print("New sorted list A is % s" % (A))
Blyxyas

Daftar Sortir Python

thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
print(thislist)
SAMER SAEID

cara menyortir daftar

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

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

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

Jawaban yang mirip dengan “cara mengurutkan daftar daftar dalam python”

Pertanyaan yang mirip dengan “cara mengurutkan daftar daftar dalam python”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya