[英]How to obtain the list of uncommon tuple present in two list of tuples
I have two list l1 and l2 which contains tuples inside, I need to filter out the tuple in l1 with the tuple present in l2.我有两个列表 l1 和 l2,其中包含元组,我需要用 l2 中存在的元组过滤掉 l1 中的元组。 How to do this?这该怎么做?
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
It would be more efficient to convert l2
into a list of sets first so that you can perform subset checks in linear time:首先将l2
转换为集合列表会更有效,以便您可以在线性时间内执行子集检查:
s2 = list(map(set, l2))
output = [l for l in l1 if not any(s.issubset(l) for s in s2)]
output
becomes: output
变为:
[('a', 'b', 'c', 'd', 'e'), ('t', 'y', 'u', 'i', 'o')]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.