# find两个列表的交集？

``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中展平浅表

` `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]]` `

` `c3 = [filter(lambda x: x in c1, sublist) for sublist in c2]` `

` `c3 = [list(filter(lambda x: x in c1, sublist)) for sublist in c2]` `

filter部分获取每个子列表项并检查它是否在源列表c1中。 列表理解是针对c2中的每个子列表执行的。

` `>>> b1 = [1,2,3,4,5,9,11,15] >>> b2 = [4,5,6,7,8] >>> set(b1).intersection(b2) set([4, 5])` `

` `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]` `

### 纯粹的列表理解版本

` `>>> 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]]` `

function方法：

` `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,2,3}＆{2,3,4}出[1]：{2,3}

` `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))` `

` `>>> 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]]` `

2列表交叉的pythonic方式是：

` ` [x for x in list1 if x in list2]` `

` `def intersect(a, b): result=[] for i in b: if isinstance(i,list): result.append(intersect(a,i)) else: if i in a: result.append(i) 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]] >>> print intersect(c1,c2) [[13, 32], [7, 13, 28], [1, 6]] >>> b1 = [1,2,3,4,5,9,11,15] >>> b2 = [4,5,6,7,8] >>> print intersect(b1,b2) [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 = [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]]` `

` `> c3 = [ [i for i in set(f) if i in c1] for f in c2]` `

` `def compareLists(a,b): removed = [x for x in a if x not in b] added = [x for x in b if x not in a] overlap = [x for x in a if x in b] return [removed,added,overlap]` `

` `from collections import Counter >>> c1 = [1, 2, 2, 3, 4, 4, 4] >>> c2 = [1, 2, 4, 4, 4, 4, 5] >>> list((Counter(c1) & Counter(c2)).elements()) [1, 2, 4, 4, 4]` `
` `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 = [list(set(c2[i]).intersection(set(c1))) for i in xrange(len(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]] result = [] for li in c2: res = set(li) & set(c1) result.append(list(res)) print result` `
` `# Problem: Given c1 and c2: 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]] # how do you get c3 to be [[13, 32], [7, 13, 28], [1, 6]] ?` `

` `c3 = [] for sublist in c2: c3.append([val for val in c1 if val in sublist])` `

` `c3 = [[val for val in c1 if val in sublist] for sublist in c2]` `