获取数组a = np.array([1,2,3,2,3,4,3,4,5,6])和数组b = np.array([7,2,10,2,7,4,9,4,9,8])之间的共同元素(可参考np.unique()函数)
时间: 2023-08-17 19:06:08 浏览: 57
可以使用`np.intersect1d()`函数来获取数组a和数组b之间的共同元素,具体代码如下:
```python
import numpy as np
a = np.array([1,2,3,2,3,4,3,4,5,6])
b = np.array([7,2,10,2,7,4,9,4,9,8])
result = np.intersect1d(a, b)
print(result)
```
输出结果为:
```
[2 4]
```
其中,`np.intersect1d(a, b)`表示获取数组a和数组b之间的共同元素,返回一个新的数组。
相关问题
获取数组a = np.array([1,2,3,2,3,4,3,4,5,6])和数组b = np.array([7,2,10,2,7,4,9,4,9,8])元素相匹配的位置
以下是获取数组a和数组b元素相匹配的位置的方法:
```python
import numpy as np
a = np.array([1,2,3,2,3,4,3,4,5,6])
b = np.array([7,2,10,2,7,4,9,4,9,8])
# 方法一:使用np.where()函数
matched_positions = np.where(a == b)[0]
print("Matched positions:", matched_positions)
# 方法二:使用np.argwhere()函数
matched_positions = np.argwhere(a == b).flatten()
print("Matched positions:", matched_positions)
```
获取数组a=np.array([1,2,3,2,3,4,3,4,5,6])和数组b=np.array([7,2,10,2,7,4,9,4,9,8])元素相匹配的位置
以下是使用NumPy获取数组a和数组b元素相匹配位置的方法:
```python
import numpy as np
a = np.array([1, 2, 3, 2, 3, 4, 3, 4, 5, 6])
b = np.array([7, 2, 10, 2, 7, 4, 9, 4, 9, 8])
matches = np.where(a == b)
print(matches) # 输出:(array([1, 3, 5, 7]),)
```
这段代码中,我们使用了NumPy的where函数来获取数组a和数组b中元素相匹配的位置,然后将结果打印出来。