python - 如何获取两个元组列表中存在的不常见元组列表
问题描述
我有两个列表 l1 和 l2 ,其中包含里面的元组,我需要用 l2 中存在的元组过滤掉 l1 中的元组。这个怎么做?
l1=[('a','b','c','d','e'),('t','y','u','i','o'),('q','s','a','e','r'),('t','f','e','w','l')]
l2=[('a','r'),('l','f')]
output=[('a','b','c','d','e'),('t','y','u','i','o')]
k=0
p=0
filter=[]
for i in l1:
for j in l2:
if i[k]!=j[p]:
ss=i
filter.append(ss)
k+=1
p+=1
解决方案
l2
首先转换为集合列表会更有效,以便您可以在线性时间内执行子集检查:
s2 = list(map(set, l2))
output = [l for l in l1 if not any(s.issubset(l) for s in s2)]
output
变成:
[('a', 'b', 'c', 'd', 'e'), ('t', 'y', 'u', 'i', 'o')]
推荐阅读
- linux - 如何创建一个程序,将操作系统更改的更新日志实时提取到镜像文件
- python - 将项目添加到现有多处理池映射中的列表
- google-cloud-platform - 在 GCP 中,我在 Stack Driver Logging 中计费
- swift - 我们如何提取自定义形状的线点或曲线度数来馈送我们的自定义路径?
- spring-boot - OpenFaas:获取上游 HTTP 请求错误:发布 http://127.0.0.1:8082/:部署长时间运行的 springboot 服务时出现 EOF
- angular - 角度父组件属性更新不运行子组件中的方法
- html - 在 VueJS 的上下文中循环时创建了不必要的 div 标签
- reactjs - 如何缩短在抽屉导航器中添加标题的代码?
- solr - 什么将取代 Solr 9.0 中的 DataImportHandler?
- kdb - kdb:短语和性能