return sqrt(sum((a[i] - b[i])**2 for i in range(n))) n = len(data) m = 5 edges = [] for i in range(n): for j in range(i + 1, n): edges.append((dist(data[i], data[j]), i, j)) edges.sort() # print(edges) uni = UnionFind(n) for (cost, i, j) in edges: if uni.group_count() == m: break if uni.size(i) > 12 or uni.size(j) > 12: continue if (uni.union(i, j)): print(cost, i, j) print(uni.all_group_members()) import subprocess for root, group in uni.all_group_members().items(): dir_name = "img" + str(root) subprocess.run(["mkdir", dir_name]) for i in group: img_name = str(images[i]) subprocess.run(["cp", img_name, "./" + dir_name])
print(uf.same(0, 5)) # False print(uf.members(0)) # [0, 2] print(uf.members(5)) # [1, 3, 4, 5] print(uf.roots()) # [0, 1] print(uf.group_count()) # 2 print(uf.all_group_members()) # defaultdict(<class 'list'>, {0: [0, 2], 1: [1, 3, 4, 5]}) print(list(uf.all_group_members().values())) # [[0, 2], [1, 3, 4, 5]] l = ['A', 'B', 'C', 'D', 'E'] d = {x: i for i, x in enumerate(l)} print(d) # {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4} d_inv = {i: x for i, x in enumerate(l)} print(d_inv) # {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E'}