python `in` 关键字作为过滤器中使用的函数

发布于 2024-11-25 07:38:36 字数 313 浏览 0 评论 0原文

是否可以在过滤器中使用 python 关键字 in ?我知道二元、一元、赋值操作等价于函数调用。

例如,

''!=3

相同

''.__ne__(3)

in 函数是否有类似的东西 ? 我想做这样的事情。 ..

filter( list1.__in__, list2 )

我想这可以通过编写 in 函数来完成...但我只想知道它是否已经内置。

is it possible to use the python keyword in in a filter? I know that binary, unary, assignment operations are equivalent to a function call.

such as

''!=3

is the same as

''.__ne__(3)

is there an analogous thing for the in function?
I want to do something like this. ..

filter( list1.__in__, list2 )

I guess this can be accomplished with writing the in function... but i just want to know if it is already built in or not.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不气馁 2024-12-02 07:38:36
filter( list1.__contains__, list2 )

更简洁地写为:

[ v for v in list2 if v in list1 ]

并显示等价性:

>>> list1 = [2, 4, 6, 8, 10]
>>> list2 = [1, 2, 3, 4, 5]
>>> [ v for v in list2 if v in list1 ]
[2, 4]
filter( list1.__contains__, list2 )

is more cleanly written as:

[ v for v in list2 if v in list1 ]

and to show equivalence:

>>> list1 = [2, 4, 6, 8, 10]
>>> list2 = [1, 2, 3, 4, 5]
>>> [ v for v in list2 if v in list1 ]
[2, 4]
孤独陪着我 2024-12-02 07:38:36

您正在寻找 __contains__

>>> [1, 2, 3].__contains__(2)
True
>>> [1, 2, 3].__contains__(4)
False

对于你想做的事情:

>>> list1 = [2, 4, 6, 8, 10]
>>> filter(list1.__contains__, [1, 2, 3, 4, 5])
[2, 4]

You are looking for __contains__.

>>> [1, 2, 3].__contains__(2)
True
>>> [1, 2, 3].__contains__(4)
False

And for what you want to do:

>>> list1 = [2, 4, 6, 8, 10]
>>> filter(list1.__contains__, [1, 2, 3, 4, 5])
[2, 4]
只等公子 2024-12-02 07:38:36

一般来说,您应该使用 operator 模块中的函数,在本例中为 operator.contains

但有一种更有效的方法可以通过使用集合来做到这一点:

In [1]: list1 = [2, 4, 6, 8, 10]

In [2]: list2 = [1, 2, 3, 4, 5]

In [3]: list(set(list1) & set(list2))
Out[3]: [2, 4]

注意:& 运算符是交集。

In general you should use the functions from the operator module, in this case it would be operator.contains.

But there is much more efficient way to do this by using sets:

In [1]: list1 = [2, 4, 6, 8, 10]

In [2]: list2 = [1, 2, 3, 4, 5]

In [3]: list(set(list1) & set(list2))
Out[3]: [2, 4]

Note: The & operator is the intersection.

如梦初醒的夏天 2024-12-02 07:38:36

正如 Dan D. 的回答所示,列表理解绝对是做到这一点的最佳方法。不过,在更一般的情况下,如果您想在接受另一个函数作为参数的函数中使用类似 innot 的内容,则可以使用 lambda 函数:

>>> list1 = [2, 4, 6, 8, 10]
>>> list2 = [1, 2, 3, 4, 5]
>>> in_check = lambda item: item in list1
>>> filter(in_check, list2)
[2, 4]

再次强调,我提出这个只是为了让您了解一般知识;处理这种特定情况的最佳方法肯定是使用列表理解,如 Dan D. 的回答。有关 Python 中 lambda 的更多信息,请访问此处

A list comprehension, as in Dan D.'s answer, is definitely the best way to do this. In the more general case, though, where you want to use something like in or not in a function which takes another function as an argument, you can use a lambda function:

>>> list1 = [2, 4, 6, 8, 10]
>>> list2 = [1, 2, 3, 4, 5]
>>> in_check = lambda item: item in list1
>>> filter(in_check, list2)
[2, 4]

Again, I present this just for your general knowledge; the best way to handle this specific case is definitely with a list comprehension, as in Dan D.'s answer. More information on lambdas in Python is available here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文