2013-02-04 7 views
2

부울 배열을 반복 가능한 인덱스로 변환하려면 어떻게해야합니까?부울 배열의 파이썬 numpy 인덱스 집합

예를 들면,

import numpy as np 
import itertools as it 
x = np.array([1,0,1,1,0,0]) 
y = x > 0 
retval = [i for i, y_i in enumerate(y) if y_i] 

은 더 좋은 방법이 있나요?

답변

3

np.where 또는 np.nonzero을 시도해보십시오.

x = np.array([1, 0, 1, 1, 0, 0]) 
np.where(x)[0] # returns a tuple hence the [0], see help(np.where) 
# array([0, 2, 3]) 
x.nonzero()[0] # in this case, the same as above. 

help(np.where)help(np.nonzero) 참조하십시오.

np.where 페이지에서 1D x에 해당하는 내용은 기본적으로 긴 형식과 동일한 의미입니다.

+0

나는 더 좋은 방법이 있다는 것을 알았다! 나는 "np.index *"를 보았지만 아무 것도 찾지 못했습니다. –