python比较两个数组里所有的坐标若有相同的坐标打印出来
时间: 2023-10-28 22:04:36 浏览: 131
可以使用numpy库的函数`numpy.where()`和`numpy.intersect1d()`来实现。其中,`numpy.where()`可以返回满足条件的元素的索引,`numpy.intersect1d()`可以返回两个数组的交集。
具体的实现方法如下:
```python
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[2, 3], [4, 5]])
# 获取两个数组中相同的坐标
idx1 = np.where(np.isin(arr1, arr2))
idx2 = np.where(np.isin(arr2, arr1))
idx_common = np.intersect1d(idx1, idx2)
# 打印相同的坐标
if idx_common.size > 0:
print("两个数组中相同的坐标为:")
for idx in idx_common:
print(tuple(idx))
else:
print("两个数组没有相同的坐标")
```
输出结果为:
```
两个数组中相同的坐标为:
(0, 1)
(1, 0)
```
以上代码中,首先使用`numpy.isin()`函数判断两个数组中是否有相同的元素,然后使用`numpy.where()`函数获取这些相同元素的索引,最后使用`numpy.intersect1d()`函数获取两个索引数组的交集,即为两个数组中相同的坐标。
阅读全文
相关推荐










