给出以下两个嵌套列表:
circuitBreaker.protect(task).runSyncMaybe
我正在尝试按列表比较两个列表,同时保持列表结构,仅保留匹配项:
alist = [[1,4,10,2], [4,1,12,10], [5,12,4,10], [5,10,2,4]]
blist = [[2,4,10,9,13,20], [12,9,20,33,Not,15], [3,1,2,7,4,8], [Not,9,8,3,4,5]]
预先感谢您的帮助!
答案 0 :(得分:1)
您可以使用set.intersection()
(doc):
Not = object()
alist = [[1,4,10,2], [4,1,12,10], [5,12,4,10], [5,10,2,4]]
blist = [[2,4,10,9,13,20], [12,9,20,33,Not,15], [3,1,2,7,4,8], [Not,9,8,3,4,5]]
new_list = [list(set(i).intersection(j)) for i, j in zip(alist, blist)]
print(new_list)
打印:
[[2, 10, 4], [12], [4], [4, 5]]