Here is the python, presumably it is correct, I haven't tested it recently:
def metric_mbr(metric,x,thresh,data): for elt in data: if metric(x,elt) <= thresh: -- depending on if 0 or 1 means exact match, you may want to swap to: >= thresh return True return False def categorize_list(data,metric,thresh): out_list = [] for x in data: n = 0 del_list = [] for i in range(len(out_list)): if metric_mbr(metric,x,thresh,out_list[i]): if n == 0: out_list[i].append(x) idx = i n = 1 else: out_list[idx] += out_list[i] del_list.append(i) if n == 0: out_list.append([x]) else: out_list = [x for index, x in enumerate(out_list) if index not in del_list] return out_listAnd that's it. BTW, yeah O(n^3) I think!
No comments:
Post a Comment