1. 程式人生 > >python sort() sorted() 與argsort()函式的區別

python sort() sorted() 與argsort()函式的區別

1、python的內建排序函式有 sort、sorted兩個

sort函式只定義在list中,sorted函式對於所有的可迭代序列都可以定義.

for example:

ls = list([5, 2, 3, 1, 4])

new_ls = sorted(ls)

/*或者使用ls.sort()即可,直接將ls改變*/

print(new_ls)

2、argsort()函式,是numpy庫中的函式,返回的是陣列值從小到大的索引值

for example:

One dimensional array:一維陣列

>>> x = np.array([3, 1, 2])
>>> np.argsort(x)
array([1, 2, 0])