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

前端 未结 5 969
囚心锁ツ
囚心锁ツ 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:09

    A simple loop will do:

    res = []
    x = [1,2,3,4,0,1,2,3,4,11] 
    for i in range(len(x)):
        if check_condition(x[i]):
            res.append(i)
    

    One liner with comprehension:

    res = [i for i, v in enumerate(x) if check_condition(v)]
    

    Here you have a live example

提交回复
热议问题