如何获取两个元组列表中存在的不常见元组列表

How to obtain the list of uncommon tuple present in two list of tuples

我有两个列表 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')]