比较两个不同大小的数组 - python numpy

时间:2022-02-27 08:40:42

So i have two arrays, they have the same dimension but different lenght.

所以我有两个数组,它们具有相同的维度但不同的长度。

Arr1 = np.array([[Ind1],[Ind2],[Ind3]])

Arr1 = np.array([[Ind1],[Ind2],[Ind3]])

Arr2 = np.array([[Ind7],[Ind3],[Ind3],[Ind4]])

Arr2 = np.array([[Ind7],[Ind3],[Ind3],[Ind4]])

I need to get the position and value of the elements that have the same position and are equal in both arrays.

我需要获得具有相同位置且在两个数组中相等的元素的位置和值。

In the example case the expected answer will be:

在示例中,预期答案将是:

Position = 2

位置= 2

Value = Ind3

值= Ind3

I'm using python with the numpy module.

我正在使用python和numpy模块。

2 个解决方案

#1


1  

With NumPy arrays, you might want to work in a vectorized manner for performance and also to make use of array-slicing. With that in mind, here's one approach for input arrays a and b -

使用NumPy数组,您可能希望以矢量化方式工作以提高性能并使用数组切片。考虑到这一点,这是输入数组a和b的一种方法 -

n = min(len(a), len(b))
out_idx = np.flatnonzero(a[:n] == b[:n])
out_val = a[out_idx] # or b[out_idx] both work

This takes care of multiple matches.

这需要处理多个匹配。

Sample run -

样品运行 -

In [224]: a = np.array([3, 8, 9, 2, 1, 7])

In [225]: b = np.array([1, 2, 9, 7, 5, 7, 0, 4])

In [226]: n = min(len(a), len(b))
     ...: out_idx = np.flatnonzero(a[:n] == b[:n])
     ...: out_val = a[out_idx]
     ...: 

In [227]: out_idx
Out[227]: array([2, 5])

In [228]: out_val
Out[228]: array([9, 7])

For a list of tuples as output for indices and their values -

对于作为索引及其值的输出的元组列表 -

In [229]: zip(out_idx, out_val)
Out[229]: [(2, 9), (5, 7)]

For a pretty dictionary output of indices and corresponding values -

对于索引和相应值的漂亮字典输出 -

In [230]: {i:j for i,j in zip(out_idx, out_val)}
Out[230]: {2: 9, 5: 7}

#2


1  

Assuming the lists are called lst_1 and lst_2, you could do something like

假设列表名为lst_1和lst_2,您可以执行类似的操作

for i in range(min(len(lst_1), len(lst_2)):
    if lst_1[i] == lst_2[i]:
        return lst_1[i], i

This will return a tuple containing the common element and its index. Note that if there are multiple matches, the first one will be returned; if no matches exist, None is returned.

这将返回包含公共元素及其索引的元组。请注意,如果有多个匹配项,则会返回第一个匹配项;如果不存在匹配项,则返回None。

#1


1  

With NumPy arrays, you might want to work in a vectorized manner for performance and also to make use of array-slicing. With that in mind, here's one approach for input arrays a and b -

使用NumPy数组,您可能希望以矢量化方式工作以提高性能并使用数组切片。考虑到这一点,这是输入数组a和b的一种方法 -

n = min(len(a), len(b))
out_idx = np.flatnonzero(a[:n] == b[:n])
out_val = a[out_idx] # or b[out_idx] both work

This takes care of multiple matches.

这需要处理多个匹配。

Sample run -

样品运行 -

In [224]: a = np.array([3, 8, 9, 2, 1, 7])

In [225]: b = np.array([1, 2, 9, 7, 5, 7, 0, 4])

In [226]: n = min(len(a), len(b))
     ...: out_idx = np.flatnonzero(a[:n] == b[:n])
     ...: out_val = a[out_idx]
     ...: 

In [227]: out_idx
Out[227]: array([2, 5])

In [228]: out_val
Out[228]: array([9, 7])

For a list of tuples as output for indices and their values -

对于作为索引及其值的输出的元组列表 -

In [229]: zip(out_idx, out_val)
Out[229]: [(2, 9), (5, 7)]

For a pretty dictionary output of indices and corresponding values -

对于索引和相应值的漂亮字典输出 -

In [230]: {i:j for i,j in zip(out_idx, out_val)}
Out[230]: {2: 9, 5: 7}

#2


1  

Assuming the lists are called lst_1 and lst_2, you could do something like

假设列表名为lst_1和lst_2,您可以执行类似的操作

for i in range(min(len(lst_1), len(lst_2)):
    if lst_1[i] == lst_2[i]:
        return lst_1[i], i

This will return a tuple containing the common element and its index. Note that if there are multiple matches, the first one will be returned; if no matches exist, None is returned.

这将返回包含公共元素及其索引的元组。请注意,如果有多个匹配项,则会返回第一个匹配项;如果不存在匹配项,则返回None。