Python equivalent of which() in R

前端 未结 3 940
清歌不尽
清歌不尽 2020-12-28 15:33

I am trying to take the following R statement and convert it to Python using NumPy:

1 + apply(tmp,1,function(x) length(which(x[1:k] < x[k+1])))

3条回答
  •  星月不相逢
    2020-12-28 15:39

    The Python code below answers my question:

    np.array([1 + np.sum(row[range(k)] < row[k]) for row in tmp])
    

    Here tmp is a 2d array, and k is a variable which was set for column comparison.

    Thanks to https://stackoverflow.com/users/601095/doboy for inspiring me with the answer!

提交回复
热议问题