Python - 两个列表列表的交集
Python - Intersection of two lists of lists
这是我的两个列表;
k = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4], [5,9]]
kDash = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4], [5,6], [1,2]]
我的输出应该如下;
[[1, 2], [4], [5, 6, 2], [1, 2], [3], [4]]
我怎样才能得到这个输出?
提前致谢
您必须将列表转换为元组列表,然后使用交集。请注意,下面的解决方案可能包含不同顺序的元素,并且显然不会出现重复项,因为我使用的是 set.
In [1]: l1 = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4], [5,9]]
In [2]: l2 = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4], [5,6], [1,2]]
In [3]: [list(x) for x in set(tuple(x) for x in l1).intersection(set(tuple(x) for x in l2))]
Out[3]: [[1, 2], [5, 6, 2], [3], [4]]
您也可以将交集保存在变量中并获得最终列表,如果顺序,则需要重复:
In [4]: intersection = set(tuple(x) for x in l1).intersection(set(tuple(x) for x in l2))
In [5]: [x for x in l1 if tuple(x) in intersection]
Out[5]: [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4]]
还有路口,有兴趣的可以看看。
In [6]: print intersection
set([(1, 2), (5, 6, 2), (3,), (4,)])
这对于大型列表非常有效,但如果列表很小,请探索@timegb 的其他解决方案(对于较长的列表,其解决方案将非常不理想)
因为你的输出列表有重复的元素,你似乎真的不需要经典的交集。基本的列表理解将完成一切。
>>> k = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4], [5,9]]
>>> kDash = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4], [5,6], [1,2]]
>>> [x for x in k if x in kDash]
[[1, 2], [4], [5, 6, 2], [1, 2], [3], [4]]
对于大型列表,我们希望获得调用 __contains__
所需的时间为 O(1) 而不是 O(n):
>>> stuff_in_kDash = set(map(tuple, kDash))
>>> [x for x in k if tuple(x) in stuff_in_kDash]
[[1, 2], [4], [5, 6, 2], [1, 2], [3], [4]]
更简洁的交集写法是
{tuple(x) for x in l1} & {tuple(x) for x in l2}
一个好的选择是
{tuple(x) for x in l1}.intersection(map(tuple, l2))
尽管这里写的是更优雅的解决方案,但这里还有另一个
def foo(L1,L2):
res=[]
for lst in L1:
if lst in L2:
res.append(lst)
return res
这是我的两个列表;
k = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4], [5,9]]
kDash = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4], [5,6], [1,2]]
我的输出应该如下;
[[1, 2], [4], [5, 6, 2], [1, 2], [3], [4]]
我怎样才能得到这个输出?
提前致谢
您必须将列表转换为元组列表,然后使用交集。请注意,下面的解决方案可能包含不同顺序的元素,并且显然不会出现重复项,因为我使用的是 set.
In [1]: l1 = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4], [5,9]]
In [2]: l2 = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4], [5,6], [1,2]]
In [3]: [list(x) for x in set(tuple(x) for x in l1).intersection(set(tuple(x) for x in l2))]
Out[3]: [[1, 2], [5, 6, 2], [3], [4]]
您也可以将交集保存在变量中并获得最终列表,如果顺序,则需要重复:
In [4]: intersection = set(tuple(x) for x in l1).intersection(set(tuple(x) for x in l2))
In [5]: [x for x in l1 if tuple(x) in intersection]
Out[5]: [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4]]
还有路口,有兴趣的可以看看。
In [6]: print intersection
set([(1, 2), (5, 6, 2), (3,), (4,)])
这对于大型列表非常有效,但如果列表很小,请探索@timegb 的其他解决方案(对于较长的列表,其解决方案将非常不理想)
因为你的输出列表有重复的元素,你似乎真的不需要经典的交集。基本的列表理解将完成一切。
>>> k = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4], [5,9]]
>>> kDash = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4], [5,6], [1,2]]
>>> [x for x in k if x in kDash]
[[1, 2], [4], [5, 6, 2], [1, 2], [3], [4]]
对于大型列表,我们希望获得调用 __contains__
所需的时间为 O(1) 而不是 O(n):
>>> stuff_in_kDash = set(map(tuple, kDash))
>>> [x for x in k if tuple(x) in stuff_in_kDash]
[[1, 2], [4], [5, 6, 2], [1, 2], [3], [4]]
更简洁的交集写法是
{tuple(x) for x in l1} & {tuple(x) for x in l2}
一个好的选择是
{tuple(x) for x in l1}.intersection(map(tuple, l2))
尽管这里写的是更优雅的解决方案,但这里还有另一个
def foo(L1,L2):
res=[]
for lst in L1:
if lst in L2:
res.append(lst)
return res