简体   繁体   English

在 numpy 数组的 python 列表中快速找到关节对

[英]Quickly find joint pairs in a python list of numpy arrays

I have a list of numpy arrays and want to get the indices of the joint pairs.我有一个numpy数组列表,想要获取关节对的索引。 Basically, if I have the following list of lists:基本上,如果我有以下列表:

lst = [[1, 2, 3, 5], [1, 2, 9], [5, 6], [9]]

I want the following:我想要以下内容:

[[0, 1], [0, 2], [1, 3]]

Here's what I have so far, but it's slow:到目前为止,这是我所拥有的,但速度很慢:

out = out
for i, item in enumerate(lst):
    for j in range(i+1, len(lst)):
        if not set(item).isdisjoint(lst[j]):
            out.append([i, j])

I appreciate your help.我感谢您的帮助。

You could try something like this:你可以尝试这样的事情:

from itertools import combinations

lst = [[1, 2, 3, 5], [1, 2, 9], [5, 6], [9]]
lst_set = [set(e) for e in lst]

print([[x, y] for x, y in combinations(range(len(lst)), 2) if not lst_set[x].isdisjoint(lst_set[y])])

Output输出

[[0, 1], [0, 2], [1, 3]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM