R's which() and which.min() Equivalent in Python

前端 未结 5 972
囚心锁ツ
囚心锁ツ 2020-12-11 00:33

I read the similar topic here. I think the question is different or at least .index() couldnot solve my problem.

This is a simple code in R and its answ

5条回答
  •  渐次进展
    2020-12-11 01:04

    Numpy does have built-in functions for it

    x = [1,2,3,4,0,1,2,3,4,11] 
    x=np.array(x)
    np.where(x == 2)
    np.min(np.where(x==2))
    np.argmin(x)
    
    np.where(x == 2)
    Out[9]: (array([1, 6], dtype=int64),)
    
    np.min(np.where(x==2))
    Out[10]: 1
    
    np.argmin(x)
    Out[11]: 4
    

提交回复
热议问题