Hướng dẫn python find common values between two lists - python tìm các giá trị chung giữa hai danh sách

Một bài kiểm tra hiệu suất nhanh hiển thị giải pháp của Lutz là tốt nhất:

import time

def speed_test[func]:
    def wrapper[*args, **kwargs]:
        t1 = time.time[]
        for x in xrange[5000]:
            results = func[*args, **kwargs]
        t2 = time.time[]
        print '%s took %0.3f ms' % [func.func_name, [t2-t1]*1000.0]
        return results
    return wrapper

@speed_test
def compare_bitwise[x, y]:
    set_x = frozenset[x]
    set_y = frozenset[y]
    return set_x & set_y

@speed_test
def compare_listcomp[x, y]:
    return [i for i, j in zip[x, y] if i == j]

@speed_test
def compare_intersect[x, y]:
    return frozenset[x].intersection[y]

# Comparing short lists
a = [1, 2, 3, 4, 5]
b = [9, 8, 7, 6, 5]
compare_bitwise[a, b]
compare_listcomp[a, b]
compare_intersect[a, b]

# Comparing longer lists
import random
a = random.sample[xrange[100000], 10000]
b = random.sample[xrange[100000], 10000]
compare_bitwise[a, b]
compare_listcomp[a, b]
compare_intersect[a, b]

Đây là những kết quả trên máy của tôi:

# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms

Rõ ràng, bất kỳ thử nghiệm hiệu suất nhân tạo nào cũng nên được thực hiện với một hạt muối, nhưng vì câu trả lời set[].intersection[] ít nhất là nhanh như các giải pháp khác, và cũng dễ đọc nhất, nó nên là giải pháp tiêu chuẩn cho vấn đề phổ biến này.

Làm thế nào để bạn tìm thấy các giá trị chung giữa hai danh sách trong Python?
 

Examples:

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [5, 6, 7, 8, 9]
Output : {5}
Explanation: The common element of the lists is 5. 

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [6, 7, 8, 9]
Output : No common elements 
Explanation: They do not have any 
elements in common in between them

Phương pháp 2: Sử dụng thuộc tính Giao lộ của Set Chuyển đổi danh sách thành được đặt bằng cách chuyển đổi. Sử dụng chức năng giao nhau để kiểm tra xem cả hai bộ có bất kỳ yếu tố chung nào không. Nếu chúng có nhiều yếu tố chung, thì hãy in giao điểm của cả hai bộ.

Làm thế nào để bạn tìm thấy các yếu tố phổ biến của hai danh sách?set1&set2. set1&set2 returns the common elements set, where set1 is the list1 and set2 is the list2. 
Below is the Python3 implementation of the above approach: 
 

Python3

def common_member[a, b]:

    

# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
0____11
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
2
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
3

    

# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
5
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
1
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
2.

    

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [5, 6, 7, 8, 9]
Output : {5}
Explanation: The common element of the lists is 5. 

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [6, 7, 8, 9]
Output : No common elements 
Explanation: They do not have any 
elements in common in between them
0     9
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
00
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
01
Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [5, 6, 7, 8, 9]
Output : {5}
Explanation: The common element of the lists is 5. 

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [6, 7, 8, 9]
Output : No common elements 
Explanation: They do not have any 
elements in common in between them
7

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [5, 6, 7, 8, 9]
Output : {5}
Explanation: The common element of the lists is 5. 

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [6, 7, 8, 9]
Output : No common elements 
Explanation: They do not have any 
elements in common in between them
2
Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [5, 6, 7, 8, 9]
Output : {5}
Explanation: The common element of the lists is 5. 

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [6, 7, 8, 9]
Output : No common elements 
Explanation: They do not have any 
elements in common in between them
3
Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [5, 6, 7, 8, 9]
Output : {5}
Explanation: The common element of the lists is 5. 

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [6, 7, 8, 9]
Output : No common elements 
Explanation: They do not have any 
elements in common in between them
4

    

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [5, 6, 7, 8, 9]
Output : {5}
Explanation: The common element of the lists is 5. 

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [6, 7, 8, 9]
Output : No common elements 
Explanation: They do not have any 
elements in common in between them
6
Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [5, 6, 7, 8, 9]
Output : {5}
Explanation: The common element of the lists is 5. 

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [6, 7, 8, 9]
Output : No common elements 
Explanation: They do not have any 
elements in common in between them
7

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [5, 6, 7, 8, 9]
Output : {5}
Explanation: The common element of the lists is 5. 

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [6, 7, 8, 9]
Output : No common elements 
Explanation: They do not have any 
elements in common in between them
2
Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [5, 6, 7, 8, 9]
Output : {5}
Explanation: The common element of the lists is 5. 

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [6, 7, 8, 9]
Output : No common elements 
Explanation: They do not have any 
elements in common in between them
3
{5}
No common elements
0
{5}
No common elements
1
{5}
No common elements
2

{5}
No common elements
3
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
1
{5}
No common elements
5
{5}
No common elements
6
{5}
No common elements
7
{5}
No common elements
8
{5}
No common elements
7
{5}
No common elements
0
{5}
No common elements
7__42

{5}
No common elements
6
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
1
{5}
No common elements
5
{5}
No common elements
4
{5}
No common elements
7
The common elements in the two lists are: 
[5]
1
{5}
No common elements
7
The common elements in the two lists are: 
[5]
3
{5}
No common elements
7__555553737

The common elements in the two lists are: 
[5]
9

{5}
No common elements
3
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
1
{5}
No common elements
5
{5}
No common elements
6
{5}
No common elements
7
{5}
No common elements
8
{5}
No common elements
7
{5}
No common elements
0
{5}
No common elements
7__42

{5}
No common elements
6
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
1
{5}
No common elements
5
{5}
No common elements
4
{5}
No common elements
7
The common elements in the two lists are: 
[5]
1
{5}
No common elements
7
The common elements in the two lists are: 
[5]
3
{5}
No common elements
7__555553737

The common elements in the two lists are: 
[5]
9

Output:  

{5}
No common elements

Phương pháp 2: Sử dụng thuộc tính của Set Set Intersection

Chuyển đổi danh sách thành được đặt bằng cách chuyển đổi. Sử dụng chức năng giao nhau để kiểm tra xem cả hai bộ có bất kỳ yếu tố chung nào không. Nếu chúng có nhiều yếu tố chung, thì hãy in giao điểm của cả hai bộ. & Nbsp; bên dưới là triển khai Python3 của cách tiếp cận trên: & nbsp; & nbsp;
Below is the Python3 implementation of the above approach: 
 

Python3

def common_member[a, b]:6

    

# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
0____11
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
2
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
3

    

# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
5
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
1
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
2.

    

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [5, 6, 7, 8, 9]
Output : {5}
Explanation: The common element of the lists is 5. 

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [6, 7, 8, 9]
Output : No common elements 
Explanation: They do not have any 
elements in common in between them
0     9
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
00
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
01
Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [5, 6, 7, 8, 9]
Output : {5}
Explanation: The common element of the lists is 5. 

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [6, 7, 8, 9]
Output : No common elements 
Explanation: They do not have any 
elements in common in between them
7

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [5, 6, 7, 8, 9]
Output : {5}
Explanation: The common element of the lists is 5. 

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [6, 7, 8, 9]
Output : No common elements 
Explanation: They do not have any 
elements in common in between them
2
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
04
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
05

    

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [5, 6, 7, 8, 9]
Output : {5}
Explanation: The common element of the lists is 5. 

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [6, 7, 8, 9]
Output : No common elements 
Explanation: They do not have any 
elements in common in between them
6
Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [5, 6, 7, 8, 9]
Output : {5}
Explanation: The common element of the lists is 5. 

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [6, 7, 8, 9]
Output : No common elements 
Explanation: They do not have any 
elements in common in between them
7

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [5, 6, 7, 8, 9]
Output : {5}
Explanation: The common element of the lists is 5. 

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [6, 7, 8, 9]
Output : No common elements 
Explanation: They do not have any 
elements in common in between them
2
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
04
{5}
No common elements
0
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
12
{5}
No common elements
2

{5}
No common elements
3
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
1
{5}
No common elements
5
{5}
No common elements
6
{5}
No common elements
7
{5}
No common elements
8
{5}
No common elements
7
{5}
No common elements
0
{5}
No common elements
7__42

{5}
No common elements
6
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
1
{5}
No common elements
5
{5}
No common elements
4
{5}
No common elements
7
The common elements in the two lists are: 
[5]
1
{5}
No common elements
7
The common elements in the two lists are: 
[5]
3
{5}
No common elements
7__555553737

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [5, 6, 7, 8, 9]
Output : {5}
Explanation: The common element of the lists is 5. 

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [6, 7, 8, 9]
Output : No common elements 
Explanation: They do not have any 
elements in common in between them
3
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
41

Các

{5}
No common elements
6
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
1
{5}
No common elements
5
The common elements in the two lists are: 
[5]
1
{5}
No common elements
7
The common elements in the two lists are: 
[5]
3
{5}
No common elements
7
The common elements in the two lists are: 
[5]
5
{5}
No common elements
7
The common elements in the two lists are: 
[5]
7
{5}
No common elements
5

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [5, 6, 7, 8, 9]
Output : {5}
Explanation: The common element of the lists is 5. 

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [6, 7, 8, 9]
Output : No common elements 
Explanation: They do not have any 
elements in common in between them
3
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
41

Output:  

{5}
No common elements

Phương pháp 3: Sử dụng cho vòng lặpUsing for loop

Python

def common_member[a, b]:

    

# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
71
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
1
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
73__

    

# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
04
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
84

{5}
No common elements
3
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
1
{5}
No common elements
5
{5}
No common elements
6
{5}
No common elements
7
{5}
No common elements
8
{5}
No common elements
7
{5}
No common elements
0
{5}
No common elements
7__42

{5}
No common elements
6
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
1
{5}
No common elements
5
{5}
No common elements
4
{5}
No common elements
7
The common elements in the two lists are: 
[5]
1
{5}
No common elements
7
The common elements in the two lists are: 
[5]
3
{5}
No common elements
7__555553737

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [5, 6, 7, 8, 9]
Output : {5}
Explanation: The common element of the lists is 5. 

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [6, 7, 8, 9]
Output : No common elements 
Explanation: They do not have any 
elements in common in between them
3
{5}
No common elements
0
Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [5, 6, 7, 8, 9]
Output : {5}
Explanation: The common element of the lists is 5. 

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [6, 7, 8, 9]
Output : No common elements 
Explanation: They do not have any 
elements in common in between them
13
{5}
No common elements
2

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [5, 6, 7, 8, 9]
Output : {5}
Explanation: The common element of the lists is 5. 

Input : list1 = [1, 2, 3, 4, 5] 
        list2 = [6, 7, 8, 9]
Output : No common elements 
Explanation: They do not have any 
elements in common in between them
3
# Short list:
compare_bitwise took 10.145 ms
compare_listcomp took 11.157 ms
compare_intersect took 7.461 ms

# Long list:
compare_bitwise took 11203.709 ms
compare_listcomp took 17361.736 ms
compare_intersect took 6833.768 ms
41

Output:

The common elements in the two lists are: 
[5]

Làm thế nào để bạn tìm thấy các giá trị chung giữa hai danh sách trong Python?

Phương pháp 2: Sử dụng thuộc tính Giao lộ của Set Chuyển đổi danh sách thành được đặt bằng cách chuyển đổi.Sử dụng chức năng giao nhau để kiểm tra xem cả hai bộ có bất kỳ yếu tố chung nào không.Nếu chúng có nhiều yếu tố chung, thì hãy in giao điểm của cả hai bộ.Using Set's intersection property Convert the list to set by conversion. Use the intersection function to check if both sets have any elements in common. If they have many elements in common, then print the intersection of both sets.

Làm thế nào để bạn tìm thấy các yếu tố phổ biến của hai danh sách?

Sử dụng & Toán tử Bộ & Toán tử cũng là một cách để tìm các yếu tố phổ biến trong hai danh sách.Trong phương pháp này, cả hai danh sách được chuyển đổi thành các tập hợp trước và sau đó các yếu tố phổ biến được tìm thấy bằng &.Đầu tiên cả hai danh sách được chuyển đổi thành hàm SET []. The set's & operator is also one way of finding common elements in two lists. In this method, both the lists are converted into sets first and then the common elements are found using & . First both the lists are converted into set using set[] function.

Làm thế nào để bạn đếm các yếu tố phổ biến trong danh sách Python?

Phương thức số 3: Sử dụng Set [] + Len [] Tác vụ được thực hiện cũng có thể được thực hiện bằng cách sử dụng hàm SET và LEN.Chúng ta có thể nhận được một phần tử chung bằng cách sử dụng giao nhau được đặt và đếm tổng phần tử chung bằng hàm LEN.Using set[] + len[] The task performed can also be perform using the set and len function. We can get a common element using set intersection and count the total common element using len function.

Bài Viết Liên Quan

Chủ Đề