Skip to content

Instantly share code, notes, and snippets.

@LouiS0616 LouiS0616/partition.py
Last active Jan 17, 2018

Embed
What would you like to do?
from itertools import product, groupby
import operator
from UnionFind import UnionFind
def _renumber(id_list):
id_set = set(id_list)
replace_dict = dict(zip(
sorted(list(id_set)),
[i for i, _ in enumerate(id_set)]
))
return [replace_dict[elem] for elem in id_list]
def partition(src_list, predicate_func):
src_len = len(src_list)
uf = UnionFind(src_len)
loop_obj = product(src_list, src_list)
for ij, (ei, ej) in enumerate(loop_obj):
i, j = divmod(ij, src_len)
if ei is ej:
continue
if not predicate_func(ei, ej):
continue
uf.union(i, j)
return _renumber(uf._id)
def gen_group(src_list, cluster_list):
groups = groupby(
sorted(
zip(src_list, cluster_list),
key=operator.itemgetter(-1)
),
key=operator.itemgetter(-1)
)
return (
[e[0] for e in elems] for _, elems in groups
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.