1. 程式人生 > >獲取array中陣列元素的索引

獲取array中陣列元素的索引

1. 函式原型

argwhere(array):找到非空陣列array在滿足某些條件下的索引,返回索引陣列。

2. 應用

2.1 一維陣列

返回一個一維陣列,代表當前滿足條件的元素出現的位置。

# -*- coding: utf-8 -*-
import numpy as np

arr = np.random.randint(0,10, (5,))
index = np.argwhere(arr < 5)

2. 2 二維陣列

返回二維陣列,代表當前滿足條件的元素出現的位置。

# -*- coding: utf-8 -*-
import numpy as np

"""
arr = 
    9 3 7 0 
    3 4 2 4 
    3 6 4 4 
    
index = 
    0	1
    0	3
    1	0
    1	1
    1	2
    1	3
    2	0
    2	2
    2	3
"""

arr = np.random.randint(0,10, (3,4))
index = np.argwhere(arr < 5)

參考文獻

http://blog.csdn.net/vernice/article/details/50990919