“cara menggunakan sort dalam python” Kode Jawaban

Daftar Sortir Python secara terbalik

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

cara menggunakan sort dalam python

words = ["apple","pear","red"]
numbers = [4,2,7,5]
#Parameters:
#key: changes how it sorts the list, For Example: letters.sort(key=len) Sorts by length
#Reverse: Default: sorts in ascending order, if Reverse is True: sorts descending order
words.sort(key=len)
>["red","pear","apple"]
numbers.sort(reverse=True)
>[7,5,4,2]
#sort: changes the list so it is sorted
#sorted: returns the sorted list
Blue Cloud: Weird Coder

Sortir Python

>>> 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 sort () dan disortir ()

a=[2,2,4,1]
b=a
a.sort()
// a now points to object [1,2,2,4]
c=sorted(b)
//c and b also points to [1,2,2,4] 
// sort works on array only but sorted also on strings but return array of char
s="sjndk"
print(sorted(s))
// prints ['d', 'j', 'k', 'n', 's']
// sorted also works on list of strings(sorts alphabetically)
ap_Cooperative_dev

Bagaimana cara menyortir Python

a=[1,6,10,2,50,69,3]
print(sorted(a))
Thiêm KTH

Sortir daftar dalam Python

data_list = [-5, -23, 5, 0, 23, -6, 23, 67]
new_list = []

while data_list:
    minimum = data_list[0]  # arbitrary number in list 
    for x in data_list: 
        if x < minimum:
            minimum = x
    new_list.append(minimum)
    data_list.remove(minimum)    

print new_list
NATURAL MORONS

Jawaban yang mirip dengan “cara menggunakan sort dalam python”

Pertanyaan yang mirip dengan “cara menggunakan sort dalam python”

Lebih banyak jawaban terkait untuk “cara menggunakan sort dalam python” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya