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

标签 python keyword

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

比如

''!=3

相同
''.__ne__(3)

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

filter( list1.__in__, list2 )

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

最佳答案

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]

关于python `in` 关键字作为过滤器中使用的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6766243/

相关文章:

python - Pandas Dataframe.to_html() 输出行的 HTML ID

design-patterns - 在 Clojure 中设计关键字

Python:看不到抛出的异常

python - BeautifulSoup - 在看似简单的情况下解析问题

python - 编译UWSGI Empire_mongodb插件

mysql - 在mysql中使用单个关键字从多个表中搜索

c++ - 我在哪里可以了解 C++ 实例关键字?

scala - 在Scala中是否可以使用 “type”单词作为变量名?

syntax - pyodbc 驱动关键字语法错误

python - 在 Jython 中使用 JavaCV