Python 从一个列表中的任何项目到另一个列表

标签 python

如果匹配项目列表,返回列表的 T/F 列表的优雅方法是什么?

例如:

[1,3,5,4]
in
[4,3,7,5,8]

返回:

[True, True, False, True, False]

最佳答案

Testing membership在集合中的时间复杂度为 O(1),而在列表中测试成员资格的时间复杂度为 O(n)。因此(尤其是对于大列表)您应该在测试成员资格之前将 firSTList 转换为集合。

firstset = set(firstlist)
[(v in firstset) for v in otherlist]

关于Python 从一个列表中的任何项目到另一个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13101451/

相关文章:

python - 在图像python中找到对象的边界?

python - 使用 CSV : reading and writing data in the right order

javascript - 将数据从 django View 传递到 javascript

python - python如何计算元组的哈希值

python - 如何以 "mnist.pkl.gz"中使用的确切格式和数据结构将我的数据集放入 .pkl 文件中?

python - WebDriverException : Message: 'Can not connect to the ChromeDriver' . utils.is_connectable(self.port) 错误:

python - 重命名 PyCharm 项目不会更改窗口顶部的名称

python - Google App Engine 边缘缓存不工作

python - 如何在 Python 中处理数据中的 NaN 值?

python - 为某些列 pandas 创建新列