在两个numpy数组中查找所有匹配集

2024-10-03 04:26:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有2个numpy阵列,如下所示:

#[ 3  5  6  8  8  9  9  9 10 10 10 11 11 12 13 14] #rows
#[11  7 11  4  7  2  4  7  2  4  7  4  7  7 11 11] #cols

我要查找所有匹配集,例如:

3 6 13 14行与第11行匹配

5 8 9 10 11 12从行匹配2 4 7在cols中

有没有直接的方法可以做到这一点?没有空白值,行和列大小将相同

我尝试过的(循环,但不是最有效的):

#first get array of indices, sorted by unique element
idx_sort = np.argsort(cols)

# sorts records array so all unique elements are together 
sorted_records_array = cols[idx_sort]

# returns the unique values, the index of the first occurrence of a value, and the count for each element
vals, idx_start, count = np.unique(sorted_records_array, return_counts=True, return_index=True)

# splits the indices into separate arrays
res = np.split(idx_sort, idx_start[1:])

#Using looping I use intersections and concatenate to group sets:
for cntr,itm in enumerate(res):
    idx = rows[itm]
    for cntr2,itm2 in enumerate(res):
        if cntr != cntr2:
            intersectItems = np.intersect1d(rows[itm], rows[itm2])
            if intersectItems.size > 0:
                #print('intersectItems',intersectItems)
                res[cntr] = np.unique(np.concatenate((res[cntr], res[cntr2]), axis=0))

我还需要找到并删除重复项,因为我在这里的输出是[3 6 13 14]、[11 11 11]


Tags: ofthefornpressortarrayrows
2条回答

IIUC,你可以这样做:

import numpy as np

rows = np.array([3, 5, 6, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 12, 13, 14])  # rows
cols = np.array([11, 7, 11, 4, 7, 2, 4, 7, 2, 4, 7, 4, 7, 7, 11, 11])  # cols

matches = {row: col for col, row in zip(cols[::-1], rows[::-1])}
print(matches)

输出

{14: 11, 13: 11, 12: 7, 11: 4, 10: 2, 9: 2, 8: 4, 6: 11, 5: 7, 3: 11}

也许通过查看反向字典更容易理解:

from collections import defaultdict
d = defaultdict(list)
for key, value in matches.items():
    d[value].append(key)
d = dict(d)
print(d)

输出反向字典

{11: [14, 13, 6, 3], 7: [12, 5], 4: [11, 8], 2: [10, 9]}

正如您可以从上面看到的14,13,6,3匹配到1112,5,11,8,10,9匹配到7,4,2

您可以使用字典理解(嵌入列表理解):

rows = [3, 5, 6, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 12, 13, 14] 
cols = [11, 7, 11, 4, 7, 2, 4, 7, 2, 4, 7, 4, 7, 7, 11, 11]

>>> {c: [r for i, r in enumerate(rows) if cols[i]==c] for c in cols}
{11: [3, 6, 13, 14], 7: [5, 8, 9, 10, 11, 12], 4: [8, 9, 10, 11], 2: [9, 10]}

相关问题 更多 >