程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

The use of the sort () method in Python

編輯:Python

One 、 Basic form

sorted(iterable[, cmp[, key[, reverse]]])
iterable.sort(cmp[, key[, reverse]])

Parameter interpretation :

(1)iterable Specify the to sort list perhaps iterable, Needless to say ;

(2)cmp For the function , Specify the function to compare when sorting , You can specify a function or lambda function , Such as :

students For class objects list, No member has three domains , use sorted When comparing, you can decide by yourself cmp function , For example, here we want to sort by comparing the third data member , The code can be written like this :

students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
sorted(students, key=lambda student : student[2])

(3)key For the function , Specifies which item of the element to be sorted is to be sorted , The function is illustrated by the above example , The code is as follows :

sorted(students, key=lambda student : student[2])

key designated lambda The function function is to remove elements student The third domain of ( namely :student[2]), therefore sorted Sorting time , Will students The third field of all elements to sort .

Two 、 Common usage :

1. Order of original address

1) The list has its own sort Method , It sorts the list in its original location , Since it's in the original order , It's obviously impossible to have this method , Because tuples are immutable .

x = [4, 6, 2, 1, 7, 9]
x.sort()
print (x) # [1, 2, 4, 6, 7, 9] 

2. Replica sort

1)[:] Fragmentation method

x =[4, 6, 2, 1, 7, 9]
y = x[ : ]
y.sort()
print (y) #[1, 2, 4, 6, 7, 9]
print (x) #[4, 6, 2, 1, 7, 9]

Be careful :y = x[:] List by slicing x All the elements of are copied to y, If you simply put x Assign a value to y:y = x,y and x Or point to the same list , No new copy is generated .

2)sorted Method

sorted Return an ordered copy , And the type is always a list , as follows :


x =[4, 6, 2, 1, 7, 9]
y = sorted(x)
print (y) #[1, 2, 4, 6, 7, 9]
print (x) #[4, 6, 2, 1, 7, 9] 
print (sorted('Python')) #['P', 'h', 'n', 'o', 't', 'y']

3、 ... and 、 Advanced usage

1. Customize cmp Comparison function

def comp(x, y):
if x < y:
return 1
elif x > y:
return -1
else:
return 0
nums = [3, 2, 8 ,0 , 1]
nums.sort(comp)
print (nums) # null [8, 3, 2, 1, 0]
nums.sort(cmp) # Call built-in functions cmp , Ascending sort 
print (nums) # null [0, 1, 2, 3, 8]

2. Customize key and reverse

(1.reverse Implement descending sort , You need to provide a Boolean value , The default is False( Ascending order ).

(2.key When using, you must provide a function that is always called by the sorting process :

''' No one answers the problems encountered in learning ? Xiaobian created a Python Exchange of learning QQ Group :711312441 Looking for small partners who share the same aspiration , Help each other , There are also good video tutorials and PDF e-book ! '''
alist = [('2', '3', '10'), ('1', '2', '3'), ('5', '6', '7'), ('2', '5', '10'), ('2', '4', '10')]
# Multi level sorting , First, according to paragraph 3 Order of elements , Then according to the 2 Order of elements :
print (sorted(alist, cmp = None, key = lambda x:(int(x[2]), int(x[1])), reverse = False))
-------------------------------------------------------------------------------------------
[('1', '2', '3'), ('5', '6', '7'), ('2', '3', '10'), ('2', '4', '10'), ('2', '5', '10')]

Four 、operator.itemgetter function

operator Module provided itemgetter Function is used to get the data of which dimensions of an object , The parameter is some sequence number ( That is, the serial number of the data to be acquired in the object ), Let's take an example .

a = [1,2,3]
>>> b=operator.itemgetter(1) // Defined function b, Get the... Of the object 1 Values for domains
>>> b(a)
2
>>> b=operator.itemgetter(1,0) // Defined function b, Get the... Of the object 1 Domain and the 0 The value of
>>> b(a)
(2, 1)

it is to be noted that ,operator.itemgetter Function does not get value , It defines a function , This function is used to get the value of an object .

itemgetter stay sort In the middle of the day :

from operator import itemgetter
alist = [('2', '3', '10'), ('1', '2', '3'), ('5', '6', '7'), ('2', '5', '10'), ('2', '4', '10')]
# Multi level sorting , First, according to paragraph 3 Order of elements , Then according to the 2 Order of elements :
print (sorted(alist, cmp = None, key = itemgetter(2, 1), reverse = False))
print (sorted(alist, cmp = None, key = lambda x:itemgetter(2, 1)(x), reverse = False))
print (sorted(alist, cmp = None, key = lambda x:map(int, itemgetter(2, 1)(x)), reverse = False))
-------------------------------------------------------------------------------------------------
[('2', '3', '10'), ('2', '4', '10'), ('2', '5', '10'), ('1', '2', '3'), ('5', '6', '7')]
[('2', '3', '10'), ('2', '4', '10'), ('2', '5', '10'), ('1', '2', '3'), ('5', '6', '7')]
[('1', '2', '3'), ('5', '6', '7'), ('2', '3', '10'), ('2', '4', '10'), ('2', '5', '10')]

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved