我有两个列表l1和l2,其中包含元组,我需要过滤掉l1中的元组和l2中的元组。如何做到这一点?
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')]