NumPy排序的實(shí)現(xiàn)
numpy.sort()函數(shù)
該函數(shù)提供了多種排序功能,支持歸并排序,堆排序,快速排序等多種排序算法
使用numpy.sort()方法的格式為:
numpy.sort(a,axis,kind,order)
- a:要排序的數(shù)組
- axis:沿著排序的軸,axis=0按照列排序,axis=1按照行排序。
- kind:排序所用的算法,默認(rèn)使用快速排序。常用的排序方法還有
- quicksort:快速排序,速度最快,算法不具有穩(wěn)定性
- mergesort:歸并排序,優(yōu)點(diǎn)是具有穩(wěn)定性,空間復(fù)雜度較高,一般外部排序時(shí)才會(huì)考慮
- heapsort:堆排序,優(yōu)點(diǎn)是堆排序在最壞的情況下,其時(shí)間復(fù)雜度也為O(nlogn),是一個(gè)既最高效率又最節(jié)省空間的排序方法
- order:如果包含字段,則表示要排序的字段(比如按照數(shù)組中的某個(gè)元素項(xiàng)進(jìn)行排序)
下面通過(guò)一個(gè)實(shí)例來(lái)具體了解numpy.sort()函數(shù)的用法
假設(shè)我們有一組用戶(hù)信息,包含用戶(hù)的用戶(hù)名以及用戶(hù)的年齡,我們按照用戶(hù)的年齡來(lái)進(jìn)行排序
dt=np.dtype([('name','S20'),('age','i4')])
a=np.array([('adm','19'),('wan','23'),('ade','23')],dtype=dt)
s=np.sort(a,order='age',kind='quicksort')
print(s)
運(yùn)行結(jié)果:
[(b'adm', 19) (b'ade', 23) (b'wan', 23)]
Process finished with exit code 0
numpy.argsort()函數(shù)
numpy.argsort()函數(shù)返回的時(shí)從小到大的元素的索引
可以通過(guò)以下的實(shí)例更好的理解
使用argsort()方法返回索引并重構(gòu)數(shù)組
x=np.array([3,8,11,2,5])
print('返回從小到大的索引')
y=np.argsort(x)
print(y)
print('以索引對(duì)原數(shù)組排序')
print(x[y])
print('重構(gòu)原數(shù)組')
for i in y:
print(x[i],end=",")
運(yùn)行結(jié)果:
返回從小到大的索引
[3 0 4 1 2]
以索引對(duì)原數(shù)組排序
[ 2 3 5 8 11]
重構(gòu)原數(shù)組
2,3,5,8,11,
Process finished with exit code 0
numpy.lexsort()函數(shù)
numpy.sort()函數(shù)可對(duì)于多個(gè)序列進(jìn)行排序,例如我們?cè)诒容^成績(jī)的時(shí)候先比較總成績(jī),由后列到前列的優(yōu)先順序進(jìn)行比較,這時(shí)就用到了lexsort()方法
nm = ('raju','anil','ravi','amar')
dv = ('f.y.', 's.y.', 's.y.', 'f.y.')
ind = np.lexsort((dv,nm))
print ('調(diào)用 lexsort() 函數(shù):')
print (ind)
print ('\n')
print ('使用這個(gè)索引來(lái)獲取排序后的數(shù)據(jù):')
print ([nm[i] + ", " + dv[i] for i in ind])
運(yùn)行結(jié)果:
使用這個(gè)索引來(lái)獲取排序后的數(shù)據(jù):
['amar, f.y.', 'anil, s.y.', 'raju, f.y.', 'ravi, s.y.']Process finished with exit code 0
numpy.partition()函數(shù)
numpy.partition()叫做分區(qū)排序,可以制定一個(gè)數(shù)來(lái)對(duì)數(shù)組進(jìn)行分區(qū)。
格式如下:
partition(a,kth[,axis,kind,order])
實(shí)例:實(shí)現(xiàn)將數(shù)組中比7小的元素放到前面,比7大的放后面
# partition分區(qū)排序 a=np.array([2,3,9,1,0,7,23,13]) print(np.partition(a,7))
運(yùn)行結(jié)果:
[ 0 1 2 3 7 9 13 23]
Process finished with exit code 0
實(shí)例:實(shí)現(xiàn)將數(shù)組中比7小的元素放到前面,比10大的放后面,7-10之間的元素放中間
partition分區(qū)排序
a = np.array([2, 3, 9, 1, 6, 5, 0, 12, 10, 7, 23, 13, 27]) print(np.partition(a, (7, 10))) print(np.partition(a, (2, 7)))
運(yùn)行結(jié)果
[ 1 0 2 3 5 6 7 9 10 12 13 23 27]
[ 0 1 2 6 5 3 7 9 10 12 23 13 27]Process finished with exit code 0
注意:(7,10)中10的位置,數(shù)值不能超過(guò)數(shù)組長(zhǎng)度。
numpy.nonzero()函數(shù)
返回輸入數(shù)組中非零元素的索引
a = np.array([[30,40,0],[0,20,10],[50,0,60]])
print ('我們的數(shù)組是:')
print (a)
print ('\n')
print ('調(diào)用 nonzero() 函數(shù):')
print (np.nonzero (a))
運(yùn)行結(jié)果:
我們的數(shù)組是:
[[30 40 0]
[ 0 20 10]
[50 0 60]]
調(diào)用 nonzero() 函數(shù):
(array([0, 0, 1, 1, 2, 2]), array([0, 1, 1, 2, 0, 2]))
Process finished with exit code 0
numpy.where()函數(shù)
返回滿(mǎn)足輸入條件的索引
where()函數(shù)的使用
b = np.array([2, 1, 3, 0, 4, 7, 23, 13, 27])
y = np.where(b > 10)
print(y)
print('利用索引得到數(shù)組中的元素')
print(b[y])
運(yùn)行結(jié)果:
(array([6, 7, 8], dtype=int64),)
利用索引得到數(shù)組中的元素
[23 13 27]Process finished with exit code 0
numpy.extract()函數(shù)
numpy.extract()函數(shù)實(shí)現(xiàn)的是返回自定義條件的元素
# extract()自定義元素篩選 b = np.array([2, 1, 3, 0, 4, 7, 23, 13, 27]) con = np.mod(b, 2) == 0 y = np.extract(con, b) print(a[y])
運(yùn)行結(jié)果:
[9 2 6]
Process finished with exit code 0
其它排序函數(shù)
numpy.argmax() 和 numpy.argmin()函數(shù)分別沿給定軸返回最大和最小元素的索引。numpy.sort_complex(a)函數(shù)實(shí)現(xiàn)對(duì)復(fù)數(shù)按照先實(shí)部后虛部的順序進(jìn)行排序。numpy.argpartition(a, kth[, axis, kind, order])函數(shù)實(shí)現(xiàn)通過(guò)指定關(guān)鍵字沿著指定的軸對(duì)數(shù)組進(jìn)行分區(qū)。
下面舉一個(gè)復(fù)數(shù)排序的例子:
t = np.array([ 1.+2.j, 2.-1.j, 3.-3.j, 3.-2.j, 3.+5.j]) res = np.sort_complex([1 + 2j, 2 - 1j, 3 - 2j, 3 - 3j, 3 + 5j]) print(res)
運(yùn)行結(jié)果:
[1.+2.j 2.-1.j 3.-3.j 3.-2.j 3.+5.j]
Process finished with exit code 0
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python利用pdfplumber庫(kù)提取pdf中表格數(shù)據(jù)
pdfplumber是一個(gè)用于從PDF文檔中提取文本和表格數(shù)據(jù)的Python庫(kù),它可以幫助用戶(hù)輕松地從PDF文件中提取有用的信息,例如表格、文本、元數(shù)據(jù)等,本文介紹了如何通過(guò)Python的pdfplumber庫(kù)提取pdf中表格數(shù)據(jù),感興趣的同學(xué)可以參考一下2023-05-05
Python依賴(lài)庫(kù)的幾種離線安裝方法總結(jié)
這篇文章主要介紹了如何在Python中使用pip工具進(jìn)行依賴(lài)庫(kù)的安裝和管理,包括如何導(dǎo)出和導(dǎo)入依賴(lài)包列表、如何下載和安裝單個(gè)或多個(gè)庫(kù)包及其依賴(lài),以及如何指定不同的Python源進(jìn)行安裝,需要的朋友可以參考下2025-03-03
Python爬取視頻時(shí)長(zhǎng)場(chǎng)景實(shí)踐示例
這篇文章主要為大家介紹了Python獲取視頻時(shí)長(zhǎng)場(chǎng)景實(shí)踐示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
8個(gè)Python中可復(fù)用函數(shù)的最佳實(shí)踐分享
在Python編程中,編寫(xiě)可復(fù)用的函數(shù)是提高代碼質(zhì)量和開(kāi)發(fā)效率的關(guān)鍵,本文將介紹8種最佳實(shí)踐,并提供豐富的示例代碼,希望可以幫助大家編寫(xiě)高質(zhì)量的可復(fù)用函數(shù)2023-12-12
Python一行代碼實(shí)現(xiàn)快速排序的方法
排序算法是在高考或中考中出現(xiàn)頻率最多的點(diǎn),所以大家要掌握,今天小編給大家?guī)?lái)了通過(guò)Python一行代碼實(shí)現(xiàn)快速排序的方法,感興趣的朋友跟隨小編一起看看吧2019-04-04

