Python 是否有一个'stdlib'或'pandas'等同于以下简单的'Is_in'函数?

Python 是否有一个'stdlib'或'pandas'等同于以下简单的'Is_in'函数?,python,list,Python,List,是否有stdlib或pandas等效于以下简单Is_in功能?(看起来很简单,但我还没找到) 同样的事情: def subset(x, idx): ''' :param x: a list of values of length n :param idx: a list of Boolean (True, False) values of length n :return: a list of all x values for which the corresponding Boolean v

是否有
stdlib
pandas
等效于以下简单
Is_in
功能?(看起来很简单,但我还没找到)

同样的事情:

def subset(x, idx):
'''
:param x: a list of values of length n
:param idx: a list of Boolean (True, False) values of length n
:return: a list of all x values for which the corresponding Boolean
 value (x[i] <--> idx[i]) was True

Example:
import numpy as np
idx = [True, False, np.nan, True]
x = [1,2,3,4]
subset(x, idx)
# returns: [1,4]

'''
xx = [x[i] for i in range(len(x)) if idx[i]==True]
return xx
def子集(x,idx):
'''
:param x:长度为n的值列表
:param idx:长度为n的布尔值(真、假)列表
:return:对应布尔值的所有x值的列表
值(x[i]idx[i])为真
例子:
将numpy作为np导入
idx=[True,False,np.nan,True]
x=[1,2,3,4]
子集(x,idx)
#返回:[1,4]
'''
xx=[x[i]表示范围内的i(len(x)),如果idx[i]==True]
返回xx
可能是最快的:

map(y.__contains__, x)

问题的第二部分是
itertools.compress

[i in y代表i in x]
?是的-谢谢-看起来更好/更快。-->更新最后一个元素真的应该是
true
<代码>{'a':[4]}={'a':[3]}另外,如果第二个的i[1]]可能更像python.@Phylogenesis oops yes:)应该是错误的-谢谢
map(y.__contains__, x)