numpy取两个array的交集
时间: 2023-04-03 11:01:47 浏览: 178
可以使用numpy中的intersect1d函数来取两个array的交集,例如:
import numpy as np
a = np.array([1, 2, 3, 4, 5])
b = np.array([3, 4, 5, 6, 7])
intersect = np.intersect1d(a, b)
print(intersect)
输出结果为:
[3 4 5]
相关问题
numpy求两个数组交集
可以使用numpy库的intersect1d方法来求两个数组的交集。
例如,假设有两个数组a和b:
```python
import numpy as np
a = np.array([1, 2, 3, 4, 5])
b = np.array([3, 4, 5, 6, 7])
```
可以使用intersect1d方法来求a和b的交集:
```python
c = np.intersect1d(a, b)
print(c)
```
输出结果为:
```
[3 4 5]
```
其中,c即为a和b的交集数组。
提取两个numpy数组的交集
可以使用numpy中的intersect1d函数来提取两个numpy数组的交集。
示例代码:
```python
import numpy as np
a = np.array([1, 2, 3, 4, 5])
b = np.array([3, 4, 5, 6, 7])
intersect = np.intersect1d(a, b)
print(intersect) # 输出 [3 4 5]
```
在上面的代码中,我们首先定义了两个numpy数组a和b,然后使用intersect1d函数来获取它们的交集。最后,我们将交集打印出来。