python - 在列表中查找所有键簇

标签 python python-3.x

我有一个“组合”问题,要找到一组不同的键,我试图为其找到优化的解决方案:

我有这个列表“l”的列表:

l = [[1, 5],
 [5, 7],
 [4, 9],
 [7, 9],
 [50, 90],
 [100, 200],
 [90, 100],
 [2, 90],
 [7, 50],
 [9, 21],
 [5, 10],
 [8, 17],
 [11, 15],
 [3, 11]]

每个 ID 都链接到另一个 ID,但也可能链接到另一个键 - 通过另一个键 -(见下图)。目标是以优化的方式找到属于同一集群的所有键

enter image description here

想要的结果是:

[{1, 2, 4, 5, 7, 9, 10, 21, 50, 90, 100, 200}, {8, 17}, {3, 11, 15}]

我目前的代码是:

out = []

while len(l)>0:
    first, *rest = l
    first = set(first)

    lf = -1
    while len(first)>lf:
        lf = len(first)
        print(lf)
        rest2 = []
        for r in rest:
            if len(first.intersection(set(r)))>0:
                first |= set(r)
            else:
                rest2.append(r)     
        rest = rest2

    out.append(first)
    l = rest

我得到了之前显示的结果。当在需要很长时间才能运行的 200 万行上使用它时,问题就来了。

有没有其他方法可以优化解决这个问题?

最佳答案

您可以将其视为寻找 connected components 的问题在图表中:

l = [[1, 5], [5, 7], [4, 9], [7, 9], [50, 90], [100, 200], [90, 100],
     [2, 90], [7, 50], [9, 21], [5, 10], [8, 17], [11, 15], [3, 11]]
# Make graph-like dict
graph = {}
for i1, i2 in l:
    graph.setdefault(i1, set()).add(i2)
    graph.setdefault(i2, set()).add(i1)
# Find clusters
clusters = []
for start, ends in graph.items():
    # If vertex is already in a cluster skip
    if any(start in cluster for cluster in clusters):
        continue
    # Cluster set
    cluster = {start}
    # Process neighbors transitively
    queue = list(ends)
    while queue:
        v = queue.pop()
        # If vertex is new
        if v not in cluster:
            # Add it to cluster and put neighbors in queue
            cluster.add(v)
            queue.extend(graph[v])
    # Save cluster
    clusters.append(cluster)
print(*clusters)
# {1, 2, 100, 5, 4, 7, 200, 9, 10, 50, 21, 90} {8, 17} {3, 11, 15}

关于python - 在列表中查找所有键簇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54944847/

相关文章:

python-3.x - 将盈透证券 API 引入 Pandas

python - 从 fasta 中提取多个序列,但名称不同

python - 用字典中的新值替换键值对中的空白值(python 3.x)

python - 使用关键字查找项目

python - 为什么 Pickle 协议(protocol) 4 中的 Pickle 文件是协议(protocol) 3 中的文件的两倍大,而速度却没有任何提升?

python-3.x - 如何使Selenium线程运行(每个线程都有自己的驱动程序)

python - 从列表中删除重复项并删除另一个列表中相同索引处的元素

python - 使用 Lambda 函数时出现语法错误

Python 相当于 Excel 的 PERCENTILE.EXC

python - 在 for 循环中将小矩形打印到屏幕上。 (pygame)