for 2.4, you can just define an intersection function.
def intersect[*d]:
sets = iter[map[set, d]]
result = sets.next[]
for s in sets:
result = result.intersection[s]
return result
for newer versions of python:
the intersection method takes an arbitrary amount of arguments
result = set[d[0]].intersection[*d[1:]]
alternatively, you can intersect the first set with itself to avoid slicing the list and making a copy:
result = set[d[0]].intersection[*d]
I'm not really sure which would be more efficient and have a feeling that it would depend on the size of the d[0]
and the size of the list unless python has an inbuilt
check for it like
if s1 is s2:
return s1
in the intersection method.
>>> d = [[1,2,3,4], [2,3,4], [3,4,5,6,7]]
>>> set[d[0]].intersection[*d]
set[[3, 4]]
>>> set[d[0]].intersection[*d[1:]]
set[[3, 4]]
>>>
Given two list of lists, write a Python program to find the intersection between the given two lists.
Examples:
Input : lst1 = [['a', 'c'], ['d', 'e']] lst2 = [['a', 'c'], ['e', 'f'], ['d', 'e']] Output : [['a', 'c'], ['d', 'e']] Input : lst1 = [[1, 5, 7], [2, 3], [6, 9], [4, 8]] lst2 = [[9, 3], [2, 3], [6, 9]] Output : [[2, 3], [6, 9]]
Approach #1 : Naive[List comprehension]
The brute-force or naive approach to find the intersection of list of lists is to use List comprehension or simply a for loop.
def
intersection[lst1, lst2]:
return
[item
for
item
in
lst1
if
item
in
lst2]
lst1
=
[[
'a'
,
'c'
], [
'd'
,
'e'
]]
lst2
=
[[
'a'
,
'c'
], [
'e'
,
'f'
], [
'd'
,
'e'
]]
print
[intersection[lst1, lst2]]
Output:
[['a', 'c'], ['d', 'e']]
Approach #2 : Using Set intersection[]
This is an efficient method in comparison to the naive approach. We first convert both list of lists into list of tuples using map[] because Python sets are compatible with tuples, not lists. Then we simply find Set intersection[] of both the lists.
def
intersection[lst1, lst2]:
tup1
=
map
[
tuple
, lst1]
tup2
=
map
[
tuple
, lst2]
return
list
[
map
[
list
,
set
[tup1].intersection[tup2]]]
lst1
=
[[
'a'
,
'c'
], [
'd'
,
'e'
]]
lst2
=
[[
'a'
,
'c'
], [
'e'
,
'f'
], [
'd'
,
'e'
]]
print
[intersection[lst1, lst2]]
Output:
[['d', 'e'], ['a', 'c']]
Intersection of two list means we need to take all those elements which are common to both of the initial lists and store them into another list. Now there are various ways in Python, through which we can perform the Intersection of the lists.
Examples:
Input : lst1 = [15, 9, 10, 56, 23, 78, 5, 4, 9] lst2 = [9, 4, 5, 36, 47, 26, 10, 45, 87] Output : [9, 10, 4, 5] Input : lst1 = [4, 9, 1, 17, 11, 26, 28, 54, 69] lst2 = [9, 9, 74, 21, 45, 11, 63, 28, 26] Output : [9, 11, 26, 28]
Method 1:
This is the simplest method where we haven’t used any built-in functions.
Python3
def
intersection[lst1, lst2]:
lst3
=
[value
for
value
in
lst1
if
value
in
lst2]
return
lst3
lst1
=
[
4
,
9
,
1
,
17
,
11
,
26
,
28
,
54
,
69
]
lst2
=
[
9
,
9
,
74
,
21
,
45
,
11
,
63
,
28
,
26
]
print
[intersection[lst1, lst2]]
Output:
[9, 11, 26, 28]
Method 2:
This method includes the use of set[] method.
Python3
def
intersection[lst1, lst2]:
return
list
[
set
[lst1] &
set
[lst2]]
lst1
=
[
15
,
9
,
10
,
56
,
23
,
78
,
5
,
4
,
9
]
lst2
=
[
9
,
4
,
5
,
36
,
47
,
26
,
10
,
45
,
87
]
print
[intersection[lst1, lst2]]
Output:
[9, 10, 4, 5]
Method 3:
In this method we set[] the larger list and then use the built-in function called intersection[] to compute the intersected list. intersection[] is a first-class part of set.
Python3
def
Intersection[lst1, lst2]:
return
set
[lst1].intersection[lst2]
lst1
=
[
4
,
9
,
1
,
17
,
11
,
26
,
28
,
28
,
26
,
66
,
91
]
lst2
=
[
9
,
9
,
74
,
21
,
45
,
11
,
63
]
print
[Intersection[lst1, lst2]]
Output:
{9, 11}
Method 4:
By the use of this hybrid method the complexity of the program falls to O[n]. This is an efficient way of doing the following program.
Python3
def
intersection[lst1, lst2]:
temp
=
set
[lst2]
lst3
=
[value
for
value
in
lst1
if
value
in
temp]
return
lst3
lst1
=
[
9
,
9
,
74
,
21
,
45
,
11
,
63
]
lst2
=
[
4
,
9
,
1
,
17
,
11
,
26
,
28
,
28
,
26
,
66
,
91
]
print
[intersection[lst1, lst2]]
Output:
[9, 9, 11]
Method 5:
This is the where the
intersection is performed over sub-lists inside other lists. Here we have used the concept of filter[].
Python3
def
intersection[lst1, lst2]:
lst3
=
[
list
[
filter
[
lambda
x: x
in
lst1, sublist]]
for
sublist
in
lst2]
return
lst3
lst1
=
[
1
,
6
,
7
,
10
,
13
,
28
,
32
,
41
,
58
,
63
]
lst2
=
[[
13
,
17
,
18
,
21
,
32
], [
7
,
11
,
13
,
14
,
28
], [
1
,
5
,
6
,
8
,
15
,
16
]]
print
[intersection[lst1, lst2]]
Working: The filter part takes each sublist’s item and checks to see if it is in the source list. The list comprehension is executed for each sublist in list2.
Output:
[[13, 32], [7, 13, 28], [1, 6]]