Cho hai danh sách, in tất cả các yếu tố phổ biến của hai danh sách. & NBSP; & nbsp;
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 1: Sử dụng Set & Thuộc tính
Chuyển đổi danh sách thành các bộ và sau đó in SET1 & SET2. SET1 & SET2 Trả về các phần tử phổ biến được đặt, trong đó SET1 là List1 và Set2 là List2. & NBSP; bên dưới là triển khai Python3 của cách tiếp cận trên: & nbsp; & nbsp;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]:
a_set
__ set
{5} No common elements0
{5} No common elements2
=
set
{5} No common elements5
{5} No common elements7
{5} No common elements8
{5} No common elements9
{5} No common elements0
{5} No common elements1
{5} No common elements3
{5} No common elements4
{5} No common elements9
{5} No common elements0
{5} No common elements7
{5} No common elements8
{5} No common elements9
The common elements in the two lists are: [5]0____8
The common elements in the two lists are: [5]2
The common elements in the two lists are: [5]3
The common elements in the two lists are: [5]4
The common elements in the two lists are: [5]5
The common elements in the two lists are: [5]4
The common elements in the two lists are: [5]7
The common elements in the two lists are: [5]4
The common elements in the two lists are: [5]9
The common elements in the two lists are: [5]4
def
1def
2def
3=
The common elements in the two lists are: [5]2
def
1The common elements in the two lists are: [5]4
def
8The common elements in the two lists are: [5]4
common_member[a, b]:
0The common elements in the two lists are: [5]4
common_member[a, b]:
222common_member[a, b]:
6
The common elements in the two lists are: [5]0____8
The common elements in the two lists are: [5]2
The common elements in the two lists are: [5]3
The common elements in the two lists are: [5]4
The common elements in the two lists are: [5]5
The common elements in the two lists are: [5]4
The common elements in the two lists are: [5]7
The common elements in the two lists are: [5]4
The common elements in the two lists are: [5]9
The common elements in the two lists are: [5]4
def
1def
2def
3=
The common elements in the two lists are: [5]2
def
1The common elements in the two lists are: [5]4
def
8The common elements in the two lists are: [5]4
common_member[a, b]:
0The common elements in the two lists are: [5]4
common_member[a, b]:
222common_member[a, b]:
6
Output:
{5} No common elements
def
3=
The common elements in the two lists are:
[5]
2def
8The common elements in the two lists are:
[5]
4common_member[a, b]:
0The common elements in the two lists are:
[5]
4common_member[a, b]:
2The common elements in the two lists are:
[5]
4common_member[a, b]:
4def
2
Phương pháp 2: Sử dụng thuộc tính của Set Set Intersection
Below is the Python3 implementation of the above approach:
Python3
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;
a_set
__ set
{5} No common elements0
{5} No common elements2
=
set
{5} No common elements5
{5} No common elements7
{5} No common elements8
{5} No common elements9
{5} No common elements01
{5} No common elements02
{5} No common elements3
{5} No common elements4
{5} No common elements9
{5} No common elements01
{5} No common elements7
{5} No common elements09
{5} No common elements9
The common elements in the two lists are: [5]0____8
The common elements in the two lists are: [5]2
The common elements in the two lists are: [5]3
The common elements in the two lists are: [5]4
The common elements in the two lists are: [5]5
The common elements in the two lists are: [5]4
The common elements in the two lists are: [5]7
The common elements in the two lists are: [5]4
The common elements in the two lists are: [5]9
The common elements in the two lists are: [5]4
def
1def
2def
3=
The common elements in the two lists are: [5]2
def
1The common elements in the two lists are: [5]4
def
8The common elements in the two lists are: [5]4
common_member[a, b]:
0The common elements in the two lists are: [5]4
common_member[a, b]:
222{5} No common elements0
{5} No common elements38
def
3=
The common elements in the two lists are: [5]2
def
8The common elements in the two lists are: [5]4
common_member[a, b]:
0The common elements in the two lists are: [5]4
common_member[a, b]:
2The common elements in the two lists are: [5]4
common_member[a, b]:
4def
2def
3=
The common elements in the two lists are: [5]2
def
8The common elements in the two lists are: [5]4
common_member[a, b]:
0The common elements in the two lists are: [5]4
common_member[a, b]:
2The common elements in the two lists are: [5]4
common_member[a, b]:
4def
2{5} No common elements0
{5} No common elements38
Output:
{5} No common elements
Phương pháp 2: Sử dụng thuộc tính của Set Set IntersectionUsing for loop
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;
def
common_member[a, b]:
a_set
__ set
{5} No common elements0
{5} No common elements2
=
set
{5} No common elements5
The common elements in the two lists are: [5]0____8
The common elements in the two lists are: [5]2
The common elements in the two lists are: [5]3
The common elements in the two lists are: [5]4
The common elements in the two lists are: [5]5
The common elements in the two lists are: [5]4
The common elements in the two lists are: [5]7
The common elements in the two lists are: [5]4
The common elements in the two lists are: [5]9
The common elements in the two lists are: [5]4
def
1def
2def
3=
The common elements in the two lists are: [5]2
def
1The common elements in the two lists are: [5]4
def
8The common elements in the two lists are: [5]4
common_member[a, b]:
0The common elements in the two lists are: [5]4
common_member[a, b]:
222{5} No common elements0
{5} No common elements7
{5} No common elements10
{5} No common elements9
{5} No common elements0
{5} No common elements38
Output:
The common elements in the two lists are: [5]