Say I have a finite iterable X
and an equivalence relation ~
on X
. We can define a function my_relation(x1, x2)
that returns True
if x1~x2
and returns False
otherwise. I want to write a function that partitions X
into equivalence classes. That is, my_function(X, my_relation)
should return a list of the equivalence classes of ~
.
Is there a standard way to do this in python? Better yet, is there a module designed to deal with equivalence relations?
itertools.groupby
is useful if you can compute a canonical element of each equivalence class from an arbitrary value. – Magalimagallanesgroupby
requires them to be sorted - another difficulty with the current formulation. – Graduationsorted
function to get the order required. – Magalimagallanes