获取数组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])之间的共同元素
时间: 2023-06-01 18:02:01 浏览: 75
使用np.intersect1d()函数可以获取两个数组之间的共同元素,代码如下:
```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])
common = np.intersect1d(a, b)
print(common)
```
输出结果为:
```
[2 4]
```
相关问题
获取数组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中元素相匹配的位置,然后将结果打印出来。
阅读全文