wedge
We know that the object is a set of python can take the intersection, union, difference, symmetric difference, but for numpy there is no way to do this? Sometimes we use the pandas at (底层基于numpy)
the time of processing data, we do not want to separate into collections and then processed, but also support our numpy to do so, look below.
Set Operations
set in the set operation
set of set operations, is relatively simple, we simply look at it
set1 = {1, 2, 3}
set2 = {2, 3, 4}
"""
&: 交集
|: 并集
-: 差集
^: 对称差集
"""
# 以下几种方式是等价的,但是一般我们都会使用操作符来进行处理,因为比较方便
print(set1 & set2) # {2, 3}
print(set1.intersection(set2)) # {2, 3}
print(set.intersection(set1, set2)) # {2, 3}
print(set1 | set2) # {1, 2, 3, 4}
print(set1.union(set2)) # {1, 2, 3, 4}
print(set.union(set1, set2)) # {1, 2, 3, 4}
print(set1 - set2, set2 - set1) # {1} {4}
print(set1.difference(set2), set2.difference(set1)) # {1} {4}
print(set.difference(set1, set2), set.difference(set2, set1)) # {1} {4}
print(set1 ^ set2) # {1, 4}
print(set1.symmetric_difference(set2)) # {1, 4}
print(set.symmetric_difference(set1, set2)) # {1, 4}
"""
另外,以上所有的操作都支持多个集合,不仅仅只是两个
"""
print({1, 2, 3} & {2, 3, 4} & {3, 4, 5}) # {3}
The set operation numpy
Although numpy arrays are also supported, etc. & operators, but they represent a collection of meaning and nothing to do.
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([2, 3, 4])
# 两个数组进行&,表示将数组里面对应元素分别进行"按位与"操作
print(arr1 & arr2) # [0 2 0]
So, we need to use api numpy provided by operations
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([2, 3, 4])
# 取交集
print(
np.intersect1d(arr1, arr2)
) # [2 3]
# 取并集
print(
np.union1d(arr1, arr2)
) # [1 2 3 4]
# 取差集
print(
np.setdiff1d(arr1, arr2),
np.setdiff1d(arr2, arr1)
) # [1] [4]
# 取对称差集
print(
np.setxor1d(arr1, arr2)
) # [1 4]
Two receiving array, a return array. But we see it and the collection of a difference is the set of requirements which the element is not repeated, but the array did not have this requirement.
import numpy as np
arr1 = np.array([1, 2, 2, 2, 3])
arr2 = np.array([2, 3, 4])
print(np.intersect1d(arr1, arr2)) # [2 3]
print(np.union1d(arr1, arr2)) # [1 2 3 4]
But we can only pass above two arrays, if there are multiple arrays of it?
from functools import reduce
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([2, 3, 4])
arr3 = np.array([3, 4, 5])
print(reduce(np.intersect1d, [arr1, arr2, arr3])) # [3]
Overall it is quite simple |