python - 列表 A 是否有任何不在列表 B 中的项目
问题描述
我试图弄清楚如果我的列表包含不在我的黑名单中的任何项目,我如何返回 true。听起来可能很奇怪,但如果列表完全由我的黑名单中的项目组成,我只想返回 false。
这就是我的意思...
blacklist = [one, two, three]
这是我希望发生在以下情况...
one two three four = true because four is not in the blacklist
one = false because one is in the blacklist
one two three = false because all are in the blacklist
five = true because five is not in the blacklist
希望这是有道理的。
解决方案
您可以通过减去它们来找到 2 个列表之间的差异set()
:
allowed = list(set(yourlist)-set(blacklist))
这将返回一个列表以查看您的列表和黑名单之间的区别,因此,您可以使用条件来查看列表是否为空,以返回 false 或 true。
推荐阅读
- swift - 将框架绑定到超级视图边界 UIView 扩展
- outlook - 想要在 C++ 中使用扩展 MAPI 填充“新消息”窗口
- visual-studio - 删除 localdb 后如何恢复 Azure Functions 调试功能?
- unix - 如何仅在夜间运行 unix 脚本?
- android - Android,一般应用程序需要 ndk 吗?构建文件在哪里?
- shopify - 最新的 Shopify API 不在 @shopify/koa-shopify-graphql-proxy (2021-01)
- asp.net-core - 使用 Redis + ASP.NET Core 3.1 + Ubuntu 18.04 重新启动应用程序后会话丢失
- scala - 视图边界已弃用;改用隐式参数
- c++ - 一般结束迭代器与容器`end()`的可递减要求
- javascript - 无法在谷歌脚本中运行 for 循环