Hướng dẫn how do you find the similar elements in two lists in python? - làm thế nào để bạn tìm thấy các yếu tố tương tự trong hai danh sách trong python?

Tôi đã tìm ra một giải pháp đầy đủ cho giao lộ sâu

def common_items_dict(d1, d2, use_set_for_list_commons=True, use_set_for_dict_key_commons=True, append_empty=False):
    result = {}
    if use_set_for_dict_key_commons:
        shared_keys=list(set(d1.keys()).intersection(d2.keys())) # faster, order not preserved
    else:
        shared_keys=common_items_list(d1.keys(), d2.keys(), use_set_for_list_commons=False)

    for k in  shared_keys:
        v1 = d1[k]
        v2 = d2[k]
        if isinstance(v1, dict) and isinstance(v2, dict):
            result_dict=common_items_dict(v1, v2, use_set_for_list_commons, use_set_for_dict_key_commons, append_empty)
            if len(result_dict)>0 or append_empty:
                result[k] = result_dict 
        elif isinstance(v1, list) and isinstance(v2, list):
            result_list=common_items_list(v1, v2, use_set_for_list_commons, use_set_for_dict_key_commons, append_empty)
            if len(result_list)>0 or append_empty:
                result[k] = result_list 
        elif v1 == v2:
            result[k] = v1
    return result

def common_items_list(d1, d2, use_set_for_list_commons=True, use_set_for_dict_key_commons=True, append_empty=False):
    if use_set_for_list_commons: 
        result_list= list(set(d2).intersection(d1)) # faster, order not preserved, support only simple data types in list values
        return result_list

    result = []
    for v1 in d1: 
        for v2 in d2:
            if isinstance(v1, dict) and isinstance(v2, dict):
                result_dict=common_items_dict(v1, v2, use_set_for_list_commons, use_set_for_dict_key_commons, append_empty)
                if len(result_dict)>0 or append_empty:
                    result.append(result_dict)
            elif isinstance(v1, list) and isinstance(v2, list):
                result_list=common_items_list(v1, v2, use_set_for_list_commons, use_set_for_dict_key_commons, append_empty)
                if len(result_list)>0 or append_empty:
                    result.append(result_list)
            elif v1 == v2:
                result.append(v1)
    return result


def deep_commons(v1,v2, use_set_for_list_commons=True, use_set_for_dict_key_commons=True, append_empty=False):
    """
    deep_commons
     returns intersection of items of dict and list combinations recursively

    this function is a starter function, 
    i.e. if you know that the initial input is always dict then you can use common_items_dict directly
    or if it is a list you can use common_items_list directly

    v1 - dict/list/simple_value
    v2 - dict/list/simple_value
    use_set_for_dict_key_commons - bool - using set is faster, dict key order is not preserved 
    use_set_for_list_commons - bool - using set is faster, list values order not preserved, support only simple data types in list values
    append_empty - bool - if there is a common key, but no common items in value of key , if True it keeps the key with an empty list of dict

    """

    if isinstance(v1, dict) and isinstance(v2, dict):
        return common_items_dict(v1, v2, use_set_for_list_commons, use_set_for_dict_key_commons, append_empty)
    elif isinstance(v1, list) and isinstance(v2, list):
        return common_items_list(v1, v2, use_set_for_list_commons, use_set_for_dict_key_commons, append_empty)
    elif v1 == v2:
        return v1
    else:
        return None


needed_services={'group1':['item1','item2'],'group3':['item1','item2']}
needed_services2={'group1':['item1','item2'],'group3':['item1','item2']}

result=deep_commons(needed_services,needed_services2)

print(result)

Các
 

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 3: Sử dụng cho vòng lặp

Pythonset1&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 =

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

    

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
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
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
8
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
9

{5}
No common elements
0
{5}
No common elements
1
{5}
No common elements
2

    

{5}
No common elements
4
{5}
No common elements
5

{5}
No common elements
0
{5}
No common elements
1
{5}
No common elements
8
{5}
No common elements
9
{5}
No common elements
0

{5}
No common elements
1=
{5}
No common elements
3
{5}
No common elements
4
{5}
No common elements
5
{5}
No common elements
6
{5}
No common elements
5
{5}
No common elements
8
{5}
No common elements
5
The common elements in the two lists are: 
[5]
0
{5}
No common elements
5
The common elements in the two lists are: 
[5]
2

The common elements in the two lists are: 
[5]
4=
{5}
No common elements
3
The common elements in the two lists are: 
[5]
2
{5}
No common elements
5
The common elements in the two lists are: 
[5]
9
{5}
No common elements
5def1
{5}
No common elements
5def3
{5}
No common elements
5def55543

def7

{5}
No common elements
1=
{5}
No common elements
3
{5}
No common elements
4
{5}
No common elements
5
{5}
No common elements
6
{5}
No common elements
5
{5}
No common elements
8
{5}
No common elements
5
The common elements in the two lists are: 
[5]
0
{5}
No common elements
5
The common elements in the two lists are: 
[5]
2

The common elements in the two lists are: 
[5]
4=
{5}
No common elements
3
The common elements in the two lists are: 
[5]
2
{5}
No common elements
5
The common elements in the two lists are: 
[5]
9
{5}
No common elements
5def1
{5}
No common elements
5def3
{5}
No common elements
5def55543

def7

Output:  

{5}
No common elements

The common elements in the two lists are: [5]4= {5} No common elements3The common elements in the two lists are: [5]9{5} No common elements5def1{5} No common elements5def3{5} No common elements5def55543

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 =

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

    

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
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
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
8
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
9

{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
02
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
03

    

{5}
No common elements
4
{5}
No common elements
5

{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
02
{5}
No common elements
8
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
10
{5}
No common elements
0

{5}
No common elements
1=
{5}
No common elements
3
{5}
No common elements
4
{5}
No common elements
5
{5}
No common elements
6
{5}
No common elements
5
{5}
No common elements
8
{5}
No common elements
5
The common elements in the two lists are: 
[5]
0
{5}
No common elements
5
The common elements in the two lists are: 
[5]
2

The common elements in the two lists are: 
[5]
4=
{5}
No common elements
3
The common elements in the two lists are: 
[5]
2
{5}
No common elements
5
The common elements in the two lists are: 
[5]
9
{5}
No common elements
5def1
{5}
No common elements
5def3
{5}
No common elements
5def55543

{5}
No common elements
1
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
39

The common elements in the two lists are: 
[5]
4=
{5}
No common elements
3
The common elements in the two lists are: 
[5]
9
{5}
No common elements
5def1
{5}
No common elements
5def3
{5}
No common elements
5def55543

The common elements in the two lists are: 
[5]
4=
{5}
No common elements
3
The common elements in the two lists are: 
[5]
9
{5}
No common elements
5def1
{5}
No common elements
5def3
{5}
No common elements
5def5
The common elements in the two lists are: 
[5]
3

{5}
No common elements
1
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
39

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

def a_set 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
8 =7=8=9
{5}
No common elements
5

{5}
No common elements
1=
{5}
No common elements
3
{5}
No common elements
4
{5}
No common elements
5
{5}
No common elements
6
{5}
No common elements
5
{5}
No common elements
8
{5}
No common elements
5
The common elements in the two lists are: 
[5]
0
{5}
No common elements
5
The common elements in the two lists are: 
[5]
2

The common elements in the two lists are: 
[5]
4=
{5}
No common elements
3
The common elements in the two lists are: 
[5]
2
{5}
No common elements
5
The common elements in the two lists are: 
[5]
9
{5}
No common elements
5def1
{5}
No common elements
5def3
{5}
No common elements
5def55543

{5}
No common elements
1
{5}
No common elements
8
{5}
No common elements
11
{5}
No common elements
0

{5}
No common elements
1
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
39

Output:

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