快速查找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