python - 在另一个列表的嵌套列表中比较/查找列表
问题描述
我有几个名为find
. 我想知道这些find
是否是full_list
. 列表find 1-4
是一部分,full_list
而列表find 5-7
不是。
下面的示例返回"Yes"
.
find1 = [[1, 1]]
find2 = [[1, 1], [2, 2]]
find3 = [[1, 1], [3, 3]]
find4 = [[4, 4], [2, 2], [3, 3]]
find5 = [[1, 0]]
find6 = [[1, 1], [2, 0]]
find7 = [[1, 1], [3, 0]]
full_list = [[1, 1], [2, 2], [3, 3], [4, 4]]
if find2[0] in full_list and find2[1] in full_list:
print("Yes")
else:
print("No")
因为len(find2) != len(find4)
,当前的if
语句非常笨拙,几乎没有用处。
如何使这项工作以更普遍的方式工作?
解决方案
You could use all()
with a generator which returns a True
if everything is a truthy or False
:
if all(x in full_list for x in find2):
print("Yes")
else:
print("No")
# => prints Yes
This one is generic, just need to change find2
to any list you need to check with full_list
.
推荐阅读
- javascript - 如何修复倒计时时钟 JS HTML CSS
- angular - TypeError: Object(...) is not a function at Base64.encodeFile (vendor.js:)
- javascript - 如何使用鼠标滚轮同步两个滑块?
- node.js - 错误 node_modules/@types/node/globals.d.ts(196,5) errorTS2300: Duplicate identifier 'resolve' src/typings.d.ts(12,3) Duplicate identifier 'resolve'
- c++ - 函数返回值的位运算符提升
- scala - 异步处理 akka 流并写入文件接收器
- docker - 如何在运行 ASP.NET Core Web 应用程序之前更改 dockerfile 中的自定义入口点以将迁移应用到 sqlserver?
- spring-boot - Spring Boot数据JPA中的JSON数组问题
- java - 跨所有实例从数据库自动刷新
- c# - 任务
Xamarin.Forms 中的错误使程序停止