我知道如何获得两个平面列表的交集:
b1 = [1,2,3,4,5,9,11,15] b2 = [4,5,6,7,8] b3 = [val for val in b1 if val in b2]
要么
def intersect(a, b): return list(set(a) & set(b)) print intersect(b1, b2)
但是当我必须找到嵌套列表的交集时,我的问题就开始了:
c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63] c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]
最后我想收到:
c3 = [[13,32],[7,13,28],[1,6]]
你能帮我个忙吗?
在python中展平浅表
S.Lott.. 894
您不需要定义交集.它已经是集合的一流部分.
>>> b1 = [1,2,3,4,5,9,11,15] >>> b2 = [4,5,6,7,8] >>> set(b1).intersection(b2) set([4, 5])
@ S.Lott,`set(b1)&set(b2)`有什么问题?IMO清洁使用操作员. (30认同)
所以...这个答案绝不会回答这个问题,对吧?因为现在可以使用**嵌套**列表. (5认同)
另外,使用`set`将导致代码的速度提高了几个数量级.这是一个示例基准测试®:https://gist.github.com/andersonvom/4d7e551b4c0418de3160 (4认同)
仅在不必订购结果时才有效. (4认同)
由于转换为set,这会比lambda慢吗? (3认同)
Brian R. Bon.. 176
如果你想:
c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63] c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]] c3 = [[13, 32], [7, 13, 28], [1,6]]
那么这是你的Python 2解决方案:
c3 = [filter(lambda x: x in c1, sublist) for sublist in c2]
在Python 3中filter
返回一个iterable而不是list
,所以你需要用以下方法包装filter
调用list()
:
c3 = [list(filter(lambda x: x in c1, sublist)) for sublist in c2]
说明:
过滤器部分获取每个子列表的项目并检查它是否在源列表c1中.对c2中的每个子列表执行列表推导.
您不需要定义交集.它已经是集合的一流部分.
>>> b1 = [1,2,3,4,5,9,11,15] >>> b2 = [4,5,6,7,8] >>> set(b1).intersection(b2) set([4, 5])
如果你想:
c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63] c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]] c3 = [[13, 32], [7, 13, 28], [1,6]]
那么这是你的Python 2解决方案:
c3 = [filter(lambda x: x in c1, sublist) for sublist in c2]
在Python 3中filter
返回一个iterable而不是list
,所以你需要用以下方法包装filter
调用list()
:
c3 = [list(filter(lambda x: x in c1, sublist)) for sublist in c2]
说明:
过滤器部分获取每个子列表的项目并检查它是否在源列表c1中.对c2中的每个子列表执行列表推导.
对于那些只想找到两个列表的交集的人来说,Asker提供了两种方法:
b1 = [1,2,3,4,5,9,11,15] b2 = [4,5,6,7,8] b3 = [val for val in b1 if val in b2]和
def intersect(a, b): return list(set(a) & set(b)) print intersect(b1, b2)
但是有一种混合方法更有效,因为你只需要在列表/集合之间进行一次转换,而不是三种:
b1 = [1,2,3,4,5] b2 = [3,4,5,6] s2 = set(b2) b3 = [val for val in b1 if val in s2]
这将在O(n)中运行,而他的原始方法涉及列表理解将在O(n ^ 2)中运行
功能方法:
input_list = [[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]] result = reduce(set.intersection, map(set, input_list))
它可以应用于1+列表的更一般情况
>>> c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63] >>> c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]] >>> c1set = frozenset(c1)
扁平变体:
>>> [n for lst in c2 for n in lst if n in c1set] [13, 32, 7, 13, 28, 1, 6]
嵌套变体:
>>> [[n for n in lst if n in c1set] for lst in c2] [[13, 32], [7, 13, 28], [1, 6]]
&运算符取两组的交集.
{1,2,3}&{2,3,4} Out [1]:{2,3}
采用2个列表的pythonic方式是:
[x for x in list1 if x in list2]
您应该使用此代码(取自http://kogs-www.informatik.uni-hamburg.de/~meine/python_tricks)展平,代码未经测试,但我确信它有效:
def flatten(x): """flatten(sequence) -> list Returns a single, flat list which contains all elements retrieved from the sequence and all recursively contained sub-sequences (iterables). Examples: >>> [1, 2, [3,4], (5,6)] [1, 2, [3, 4], (5, 6)] >>> flatten([[[1,2,3], (42,None)], [4,5], [6], 7, MyVector(8,9,10)]) [1, 2, 3, 42, None, 4, 5, 6, 7, 8, 9, 10]""" result = [] for el in x: #if isinstance(el, (list, tuple)): if hasattr(el, "__iter__") and not isinstance(el, basestring): result.extend(flatten(el)) else: result.append(el) return result
在展平列表后,以通常的方式执行交集:
c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63] c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]] def intersect(a, b): return list(set(a) & set(b)) print intersect(flatten(c1), flatten(c2))
自intersect
定义以来,基本的列表理解就足够了:
>>> c3 = [intersect(c1, i) for i in c2] >>> c3 [[32, 13], [28, 13, 7], [1, 6]]
感谢S. Lott的评论和TM.的相关评论:
>>> c3 = [list(set(c1).intersection(i)) for i in c2] >>> c3 [[32, 13], [28, 13, 7], [1, 6]]
鉴于:
> c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63] > c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]
我发现以下代码运行良好,如果使用set操作可能更简洁:
> c3 = [list(set(f)&set(c1)) for f in c2]
它得到了:
> [[32, 13], [28, 13, 7], [1, 6]]
如果需要订购:
> c3 = [sorted(list(set(f)&set(c1))) for f in c2]
我们有:
> [[13, 32], [7, 13, 28], [1, 6]]
顺便说一句,对于更多的python风格,这个也很好:
> c3 = [ [i for i in set(f) if i in c1] for f in c2]