Hướng dẫn python intersection of multiple lists - python giao điểm của nhiều danh sách

Đối với 2.4, bạn chỉ có thể xác định hàm giao nhau.

def intersect(*d):
    sets = iter(map(set, d))
    result = sets.next()
    for s in sets:
        result = result.intersection(s)
    return result

Đối với các phiên bản mới hơn của Python:

Phương pháp giao nhau có một lượng đối số tùy ý

result = set(d[0]).intersection(*d[1:])

Ngoài ra, bạn có thể giao nhau tập hợp đầu tiên để tránh cắt danh sách và tạo một bản sao:

result = set(d[0]).intersection(*d)

Tôi không thực sự chắc chắn cái nào sẽ hiệu quả hơn và có cảm giác rằng nó sẽ phụ thuộc vào kích thước của

result = set(d[0]).intersection(*d[1:])
4 và kích thước của danh sách trừ khi Python có kiểm tra sẵn có như vậy

if s1 is s2:
    return s1

Trong phương pháp giao điểm.

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

Bạn có thể sử dụng giao điểm của danh sách không?

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

Giao lộ của hai danh sách có nghĩa là chúng ta cần lấy tất cả các yếu tố phổ biến cho cả hai danh sách ban đầu và lưu trữ chúng vào một danh sách khác. Bây giờ có nhiều cách khác nhau trong Python, qua đó chúng ta có thể thực hiện giao điểm của các danh sách.
Approach #1 : Naive(List comprehension)

.Interection làm gì trong Python?

result = set(d[0]).intersection(*d[1:])
5
result = set(d[0]).intersection(*d[1:])
6

result = set(d[0]).intersection(*d[1:])
7
result = set(d[0]).intersection(*d)
78
result = set(d[0]).intersection(*d)
9
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]
0
result = set(d[0]).intersection(*d)
81

result = set(d[0]).intersection(*d)
3
result = set(d[0]).intersection(*d)
9
if s1 is s2:
    return s1
0
if s1 is s2:
    return s1
1
if s1 is s2:
    return s1
2
if s1 is s2:
    return s1
3
if s1 is s2:
    return s1
4
if s1 is s2:
    return s1
5
if s1 is s2:
    return s1
2
if s1 is s2:
    return s1
7
if s1 is s2:
    return s1
8

if s1 is s2:
    return s1
9
result = set(d[0]).intersection(*d)
9
if s1 is s2:
    return s1
0
if s1 is s2:
    return s1
1
if s1 is s2:
    return s1
2
if s1 is s2:
    return s1
3
if s1 is s2:
    return s1
4
if s1 is s2:
    return s1
7
if s1 is s2:
    return s1
2
>>> 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])
>>> 
8
if s1 is s2:
    return s1
4
if s1 is s2:
    return s1
5____32__373738

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]]
4
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]]
5

Output:

[['a', 'c'], ['d', 'e']]

& NBSP; Cách tiếp cận #2: Sử dụng Set Intersection ()
Approach #2 : Using Set intersection()

Đây là một phương pháp hiệu quả so với phương pháp ngây thơ. Trước tiên, chúng tôi chuyển đổi cả hai danh sách danh sách thành danh sách các bộ dữ liệu bằng MAP () vì các bộ Python tương thích với các bộ dữ liệu, không phải danh sách. Sau đó, chúng tôi chỉ cần tìm Set Intersection () của cả hai danh sách.

result = set(d[0]).intersection(*d[1:])
5
result = set(d[0]).intersection(*d[1:])
6

result = set(d[0]).intersection(*d[1:])
7
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]]
9
result = set(d[0]).intersection(*d)
9
[['a', 'c'], ['d', 'e']]
1
[['a', 'c'], ['d', 'e']]
2
[['a', 'c'], ['d', 'e']]
3
[['a', 'c'], ['d', 'e']]
4

result = set(d[0]).intersection(*d[1:])
7
[['a', 'c'], ['d', 'e']]
6
result = set(d[0]).intersection(*d)
9
[['a', 'c'], ['d', 'e']]
1
[['a', 'c'], ['d', 'e']]
2
[['a', 'c'], ['d', 'e']]
3
[['d', 'e'], ['a', 'c']]
1

result = set(d[0]).intersection(*d[1:])
7
result = set(d[0]).intersection(*d[1:])
8
[['d', 'e'], ['a', 'c']]
4
[['a', 'c'], ['d', 'e']]
2
[['a', 'c'], ['d', 'e']]
1
[['a', 'c'], ['d', 'e']]
2
[['d', 'e'], ['a', 'c']]
4
if s1 is s2:
    return s1
2
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]
0
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]
1

result = set(d[0]).intersection(*d)
3
result = set(d[0]).intersection(*d)
9
if s1 is s2:
    return s1
0
if s1 is s2:
    return s1
1
if s1 is s2:
    return s1
2
if s1 is s2:
    return s1
3
if s1 is s2:
    return s1
4
if s1 is s2:
    return s1
5
if s1 is s2:
    return s1
2
if s1 is s2:
    return s1
7
if s1 is s2:
    return s1
8

if s1 is s2:
    return s1
9
result = set(d[0]).intersection(*d)
9
if s1 is s2:
    return s1
0
if s1 is s2:
    return s1
1
if s1 is s2:
    return s1
2
if s1 is s2:
    return s1
3
if s1 is s2:
    return s1
4
if s1 is s2:
    return s1
7
if s1 is s2:
    return s1
2
>>> 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])
>>> 
8
if s1 is s2:
    return s1
4
if s1 is s2:
    return s1
5____32__373738

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]]
4
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]]
5

Output:

[['d', 'e'], ['a', 'c']]


Giao lộ của hai danh sách có nghĩa là chúng ta cần lấy tất cả các yếu tố phổ biến cho cả hai danh sách ban đầu và lưu trữ chúng vào một danh sách khác. Bây giờ có nhiều cách khác nhau trong Python, thông qua đó chúng ta có thể thực hiện giao điểm của danh sách. & Nbsp; ví dụ: & nbsp; & nbsp;
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]

Phương pháp 1: & nbsp; Đây là phương pháp đơn giản nhất trong đó chúng tôi đã sử dụng bất kỳ chức năng tích hợp nào. & NBSP; & nbsp; 
This is the simplest method where we haven’t used any built-in functions. 
 

Python3

result = set(d[0]).intersection(*d[1:])
5
result = set(d[0]).intersection(*d[1:])
6

result = set(d[0]).intersection(*d[1:])
7
result = set(d[0]).intersection(*d[1:])
13
result = set(d[0]).intersection(*d)
9
result = set(d[0]).intersection(*d[1:])
15
result = set(d[0]).intersection(*d)
0
result = set(d[0]).intersection(*d[1:])
17
result = set(d[0]).intersection(*d)
2

result = set(d[0]).intersection(*d[1:])
7
result = set(d[0]).intersection(*d[1:])
8
result = set(d[0]).intersection(*d[1:])
26

E

E

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]]
4
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]]
5

Output:  
 

[9, 11, 26, 28]

Phương pháp 2: & nbsp; Phương thức này bao gồm việc sử dụng phương thức set (). & Nbsp; & nbsp; 
This method includes the use of set() method
 

Python3

result = set(d[0]).intersection(*d[1:])
5
result = set(d[0]).intersection(*d[1:])
6

result = set(d[0]).intersection(*d[1:])
7
result = set(d[0]).intersection(*d[1:])
8
[['d', 'e'], ['a', 'c']]
4
[['a', 'c'], ['d', 'e']]
2
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]
0
result = set(d[0]).intersection(*d[1:])
78
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]
0
result = set(d[0]).intersection(*d[1:])
80

E

E

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]]
4
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]]
5

Output:  
 

result = set(d[0]).intersection(*d[1:])
0

Phương pháp 3: & nbsp; Trong phương thức này, chúng tôi đã đặt () danh sách lớn hơn và sau đó sử dụng hàm tích hợp gọi là Intersection () để tính toán danh sách giao nhau. Intersection () là một phần hạng nhất của SET. & NBSP; & NBSP; 
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

result = set(d[0]).intersection(*d[1:])
5
result = set(d[0]).intersection(*d)
26

result = set(d[0]).intersection(*d[1:])
7
result = set(d[0]).intersection(*d[1:])
8
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]
0
result = set(d[0]).intersection(*d)
30

Các

Các

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]]
4
result = set(d[0]).intersection(*d)
74

Output:  
 

result = set(d[0]).intersection(*d[1:])
1

Phương pháp 4: & nbsp; bằng cách sử dụng phương pháp lai này, độ phức tạp của chương trình rơi xuống O (n). Đây là một cách hiệu quả để thực hiện chương trình sau. & Nbsp; & nbsp; 
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

result = set(d[0]).intersection(*d[1:])
5
result = set(d[0]).intersection(*d[1:])
6

result = set(d[0]).intersection(*d[1:])
7
result = set(d[0]).intersection(*d[1:])
8
[['d', 'e'], ['a', 'c']]
4
[['a', 'c'], ['d', 'e']]
2
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]
0
result = set(d[0]).intersection(*d[1:])
78
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]
0
result = set(d[0]).intersection(*d[1:])
80

E

result = set(d[0]).intersection(*d[1:])
7
result = set(d[0]).intersection(*d[1:])
8
result = set(d[0]).intersection(*d[1:])
26

E

E

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]]
4
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]]
5

Output:  
 

result = set(d[0]).intersection(*d[1:])
2

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]]
4
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]]
5
 
This is the where the intersection is performed over sub-lists inside other lists. Here we have used the concept of filter(). 
 

Python3

result = set(d[0]).intersection(*d[1:])
5
result = set(d[0]).intersection(*d[1:])
6

result = set(d[0]).intersection(*d[1:])
7
result = set(d[0]).intersection(*d[1:])
8
[['d', 'e'], ['a', 'c']]
4
[['a', 'c'], ['d', 'e']]
2
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]
0
result = set(d[0]).intersection(*d[1:])
78
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]
0
result = set(d[0]).intersection(*d[1:])
80

result = set(d[0]).intersection(*d[1:])
7
result = set(d[0]).intersection(*d[1:])
8
result = set(d[0]).intersection(*d[1:])
26

E

E

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]]
4
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]]
5

Hoạt động: Phần bộ lọc lấy từng mục của nhóm phụ và kiểm tra xem nó có nằm trong danh sách nguồn không. Danh sách hiểu được thực hiện cho mỗi tualle trong danh sách2. & Nbsp; đầu ra: & nbsp; & nbsp;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: 
 

result = set(d[0]).intersection(*d[1:])
3

Làm thế nào để bạn tìm thấy giao điểm của nhiều danh sách trong Python?

Thực hiện theo các bước dưới đây ...
Chuyển đổi hai mục danh sách thành các bộ dữ liệu bằng bản đồ ..
Giao nhau hai bộ bằng phương pháp giao lộ và bản đồ ..
Chuyển đổi kết quả thành danh sách ..
In kết quả ..

Làm thế nào để bạn tìm thấy giao điểm của ba danh sách trong Python?

Ví dụ 1: Danh sách giao nhau thông qua phương thức SET () Danh sách thứ ba được tạo bằng cách chuyển đổi List1 và List 2 thành tập thông qua phương thức SET () và áp dụng giao lộ bằng cách sử dụng & toán tử.Tiếp theo, các giá trị của ba danh sách được in.Đầu ra sau sẽ xuất hiện sau khi chạy tập lệnh.converting list1 and list 2 into the set via the set() method and applying intersection by using the & operator. Next, the values of the three lists are printed. The following output will appear after running the script.

Bạn có thể sử dụng giao điểm của danh sách không?

Giao lộ của hai danh sách có nghĩa là chúng ta cần lấy tất cả các yếu tố phổ biến cho cả hai danh sách ban đầu và lưu trữ chúng vào một danh sách khác.Bây giờ có nhiều cách khác nhau trong Python, qua đó chúng ta có thể thực hiện giao điểm của các danh sách.. Now there are various ways in Python, through which we can perform the Intersection of the lists.

.Interection làm gì trong Python?

Phương thức Python Set Intersection () Phương thức giao nhau () Phương thức trả về một tập hợp chứa sự tương đồng giữa hai hoặc nhiều bộ.Ý nghĩa: Bộ được trả về chỉ chứa các mục tồn tại trong cả hai bộ hoặc trong tất cả các bộ nếu so sánh được thực hiện với nhiều hơn hai bộ.returns a set that contains the similarity between two or more sets. Meaning: The returned set contains only items that exist in both sets, or in all sets if the comparison is done with more than two sets.