Python method to get two sets of arrays to intersect union and difference
- 2020-04-02 14:45:57
- OfStack
This article shows an example of python getting the house part of two arrays' intersection, union, and difference sets. Share with you for your reference. The details are as follows:
1. Get the intersection of the two lists
# Methods a :
a=[2,3,4,5]
b=[2,5,8]
tmp = [val for val in a if val in b]
print tmp
#[2, 5]
# Method 2
print list(set(a).intersection(set(b)))
2. Get the union of two lists
print list(set(a).union(set(b)))
3. Get the difference set of the two lists
print list(set(b).difference(set(a))) # b There is, a Not found in
In this way, you can handle the intersection, union, and difference sets of python lists.
I hope this article has helped you with your Python programming.