I have a list
of distinct expressions. We can represent a partitioning of list
in two ways:
As a list of sublists
partitions = {{a, b, ...}, {x, y, ...}, ...}
.As a
vector
of integer partition indices, corresponding to each element oflist
.
Example:
list = {a, b, c, d, e}
partitions = {{a,e}, {}, {c,d,b}}
vector = {1, 3, 3, 3, 1}
Here {a,e}
has index 1
, {}
has index 2
and {c,d,b}
has index 3
.
What is the fastest way to convert from the partitions
representation to the vector
representation?
list
may contain any expression, including lists. The conversion must be as fast as possible, with special attention given to the situation where list
contains only integers.
A possible implementation is
partitionToVector[list_, partitions_] :=
list /. Dispatch[Join @@ Thread /@ Thread[partitions -> Range@Length[partitions]]]
{a,e}
receives the index 1. Then botha
ande
will be replaced with 1 inlist
. Another way to put it: There's a direct correspondence between elements oflist
andvector
(that are in the same position). The number invector
indicates which partition the corresponding element oflist
belongs to. $\endgroup$list
is given. Thenvector
andpartitions
are two different but equivalent representations for a partitioning oflist
. I want to state the problem better, but I don't really understand why you find it misleading. BTW this is for use with community detection algorithms and igraph. igraph likes thevector
representation and Mathematica likes thepartitions
representation. $\endgroup$list
contains no duplicates? Do you guarantee that every element oflist
appears exactly once inpartitions
? $\endgroup$