記一些常用到的python中的函數

來源:互聯網
上載者:User

標籤:max   back   二維   res   border   abc   數組   情況   下標   

1. zip()函數

它的作用是從參數中按順序一一抽出子參數組出一個新的tuple.  直接看例子:

>>> mean = np.array([2, 5, 4])>>> out = zip(‘RGB‘, mean, ‘ABC‘)>>> out[(‘R‘, 2, ‘A‘), (‘G‘, 5, ‘B‘), (‘B‘, 4, ‘C‘)]

注意:當輸入的參數的長度不同時, zip()函數會截取最短長度作為輸出長度;

 

另外:在參數上加 * 時,表示它的逆操作:

>>> zip(*out)[(‘R‘, ‘G‘, ‘B‘), (2, 5, 4), (‘A‘, ‘B‘, ‘C‘)]


 

 

2. numpy.argsort()函數:

它的作用用於給指定的axis進行排序,並且返回它們從小到大排序以後的值對應的下標。

numpy.argsort(a, axis=-1, kind=‘quicksort‘, order=None) 其中的 axis = –1 表示最外層的 座標軸;

例如:

In [1]: import numpy as npIn [2]: a = np.array([4,2,7,4,8,3])In [3]: np.argsort(a, axis = 0)Out[3]: array([1, 5, 0, 3, 2, 4])

二維數組:

In [17]: b = np.arange(12).reshape(3,4)In [18]: bOut[18]: array([[ 0,  1,  2,  3],       [ 4,  5,  6,  7],       [ 8,  9, 10, 11]])In [19]: np.argsort(b, axis = 0)Out[19]: array([[0, 0, 0, 0],       [1, 1, 1, 1],       [2, 2, 2, 2]])In [20]: np.argsort(b, axis = 1)Out[20]: array([[0, 1, 2, 3],       [0, 1, 2, 3],       [0, 1, 2, 3]])

 

另外:在一維情況下,我們可以對數組 a 通過 a[a.argsort(axis = 0) ] 進行從小到大的排序;

 

3. argmax()函數:

numpy.argmax(a, axis=None, out=None)

它的作用是返回指定座標軸上的最大值的 index.

例子不舉了;

 

4.

記一些常用到的python中的函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.