我有兩個串列 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
uj5u.com熱心網友回復:
首先轉換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')]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/336741.html
下一篇:如何在串列編號中存盤變數?
