Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
快速查找python numpy数组列表中的关节对_Python_Python 3.x_Performance_Set - Fatal编程技术网

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

快速查找python numpy数组列表中的关节对,python,python-3.x,performance,set,Python,Python 3.x,Performance,Set,我有一个numpy数组的列表,希望获得关节对的索引。基本上,如果我有以下列表: lst = [[1, 2, 3, 5], [1, 2, 9], [5, 6], [9]] 我希望: [[0, 1], [0, 2], [1, 3]] 以下是我目前掌握的情况,但进展缓慢: out = out for i, item in enumerate(lst): for j in range(i+1, len(lst)): if not set(item).isdisjoint(ls

我有一个
numpy
数组的列表,希望获得关节对的索引。基本上,如果我有以下列表:

lst = [[1, 2, 3, 5], [1, 2, 9], [5, 6], [9]]
我希望:

[[0, 1], [0, 2], [1, 3]]
以下是我目前掌握的情况,但进展缓慢:

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])

非常感谢您的帮助。

您可以尝试以下方法:

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])])
输出

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

在您的示例中,唯一不相交的一对是
[1,2],而[0,3]
这就是您所期望的答案吗?噢,对不起,我的错。我是说关节。更新了问题@Landar