In Python, you can use a variety of methods to calculate the intersection of two lists. Here are a few common methods:
Method 1: Use Sets
Collections are data structures used in Python to store unique elements, and it provides many useful methods, including calculating intersections.
list1 = [1, 2, 3, 4, 5] list2 = [4, 5, 6, 7, 8] # Convert list to collectionset1 = set(list1) set2 = set(list2) # Calculate intersectionintersection = (set2) # If necessary, convert the results back to the listintersection_list = list(intersection) print(intersection_list) # Output: [4, 5]
Method 2: Use List Comprehension
If you don't want to use a collection, or want to keep the result in a list format, you can use a list comprehension to find the intersection of two lists.
list1 = [1, 2, 3, 4, 5] list2 = [4, 5, 6, 7, 8] # Use list comprehension to calculate intersectionintersection_list = [value for value in list1 if value in list2] print(intersection_list) # Output: [4, 5]
Method 3: Use (may be inefficient for larger lists)
Although this method is not as commonly used as the first two, it shows how to use ititertools
In the modulefilterfalse
function to calculate intersection (although in this case, usefilter
or simple list comprehensions may be more direct and efficient).
import itertools list1 = [1, 2, 3, 4, 5] list2 = [4, 5, 6, 7, 8] # Convert to a collection to improve search efficiency (otherwise the efficiency of this method will be very low)set2 = set(list2) # Use filter out elements not in set2intersection_list = list((lambda x: x not in set2, list1)) # But note that this will only give the elements where the intersection of list1 and list2 appear in list1.# To get a complete intersection (regardless of order), you should use:#intersection_list = list(set(list1) & set(list2)) # This is actually the same as method 1 # Since we have converted to a collection before, here we directly use the result of method 1 as an example# Actually, we won't use this method because it's inefficient and verbose # Suppose we just want to show how to use filterfalse (although this is not the best practice)# And we know that elements in list1 may also have duplicates in list2, we want to keep these duplicates (although the question does not require it)# Then we need to change the strategy slightly, but this is usually not the standard way to calculate intersections# ... (The detailed implementation of the non-standard method is omitted here, as it does not work for this scenario) # Is it correct and more efficient to use Method 1 or Method 2print(intersection_list) # If the correct method is used, the output should be [4, 5]
Notice: In Method 3, I showed how to use, but pointed out that this approach is not usually the best choice for calculating intersections, especially when the list is larger. In practical applications, you should use Method 1 (using collections) or Method 2 (using list comprehensions) because they are both simple and efficient.
This is the end of this article about python's implementation example of intersection of two lists. For more related python lists, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!