Python-Python中R的函数“ which”

use*_*573 1 python r function which

我正在寻找python中“ R”中的等效功能。有人知道如何适应它吗?

例如:

set_false_over <- length(datapoints[which(labels==FALSE & datapoints>=unique_values[i])])
Run Code Online (Sandbox Code Playgroud)

zha*_*hen 5

您可以使用numpy.where,但在用例中是不必要的:

In [8]: import numpy as np

In [9]: x = np.arange(9.).reshape(3, 3)

In [10]: x
Out[10]: 
array([[ 0.,  1.,  2.],
       [ 3.,  4.,  5.],
       [ 6.,  7.,  8.]])

In [11]: x[np.where(x>5)]
Out[11]: array([ 6.,  7.,  8.])

In [12]: x[x>5]
Out[12]: array([ 6.,  7.,  8.])
Run Code Online (Sandbox Code Playgroud)

>运算首先返回你的bool的矩阵:

In [16]: x>5
Out[16]: 
array([[False, False, False],
       [False, False, False],
       [ True,  True,  True]], dtype=bool)
Run Code Online (Sandbox Code Playgroud)

while np.where返回X和Y的元组,其中某些条件匹配:

In [15]: np.where(x>5)
Out[15]: (array([2, 2, 2], dtype=int64), array([0, 1, 2], dtype=int64))
Run Code Online (Sandbox Code Playgroud)