I would like to make a function that takes in a list of integers and returns a partition of the indices of the list, based on the elements of the list.
For instance:
{1,3,3,7} --> { {1}, {2,3}, {4}}
{3,1,3} --> {{1,3}, {2}}
I can think of messy ways to do this, but is there a natural way to do this in Mathematica?
I would use:
This will be much faster than repeatedly using
Position, on a long lists with many unique elements. Example:TomDsuggested using the newerGatherByin place ofSowandReap. This is even faster on long lists with little repetition.Speed is more similar on lists with greater repetition:
If going for pure speed one must recognize that
MapIndexedis not optimized for packed arrays, thereforeRangeandTransposewill be considerably faster in that case: