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

前端 未结 5 971
囚心锁ツ
囚心锁ツ 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 00:57

    You could also use heapq to find the index of the smallest. Then you can chose to find multiple (for example index of the 2 smallest).

    import heapq
    
    x = np.array([1,2,3,4,0,1,2,3,4,11]) 
    
    heapq.nsmallest(2, (range(len(x))), x.take)
    

    Returns [4, 0]

提交回复
热议问题