python 列表排序

2021-08-29 05:34:00 字數 746 閱讀 6009

//列表排序

s = [1,2,3]

s.reverse()//倒序

s.sort()//正序

sorted(...)

sorted(iterable, cmp=none, key=none, reverse=false) --> new sorted list

iterable:是可迭代型別;

cmp:用於比較的函式,比較什麼由key決定,有預設值,迭代集合中的一項;

key:用列表元素的某個屬性和函式進行作為關鍵字,有預設值,迭代集合中的一項;

reverse:排序規則. reverse = true 或者 reverse = false,有預設值。

返回值:是乙個經過排序的可迭代型別,與iterable一樣

例如:s = [('b',2),('a',4),('c',3)]

print sorted(s,key=lambda x:x[0],reverse=true)

print sorted(s,cmp=lambda x,y:cmp(x[1],y[1]))

s =

sorted(s.keys(),reverse=true)

sorted(s,reverse=true)

sorted(s.items(),key=lambda x:x[0])

sorted(s.items(),key=lambda x:x[1][0])

sorted(s.values(),key=lambda x:x[0])

python 列表排序 python列表排序有哪些

python列表排序 1 氣泡排序,是一種簡單的排序演算法,它重複地遍歷要排序的數列,一次比較兩個元素,如果他們的順序錯誤就把他們交換過來 2 插入排序,通過構建有序序列,對於未排序資料,在已排序序列中從後向前掃瞄,找到相應位置並插入。1 氣泡排序 氣泡排序 bubble sort 是一種簡單的排序...

列表python排序

python題目 對列表 37,41.12,35,22,98,16,7,45,31 進行排序。這裡不考慮.sort 方法。usr bin env python coding utf 8 def merge left,right i,j 0,0result 左右列表元素對比大小,然後加1while i...

python列表排序

1.先來sort方法和sorted排序 sort方法不會生成新的列表,而sorted會 lst 3,2,9,4,34,43,22,11 lst.sort print lst print sorted lst 2.定義乙個列表,將原列表的最小值取出來,依次新增 lst1 def sort list t...