MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Extract values and multilpicities from list


Dr. Wolfgang Hintze wrote:
> Given a list of integers which may repeat, e.g.
> 
> lstIn = {2,3,4,4,2,1,1,5,4}
> 
> provide a list of different values and their respective multiplicities,
> in the example,
> 
> LstOut= {{1,2},{2,2},{3,1},{4,3},{5,1}}
> 
> Who finds the shortest function doing this task in general?
> 
> Thanks.
> 
> Best regards,
> Wolfgang
> 
Hi Wolfgang,

What about the following one liner?

In[1]:=
lstIn = {2, 3, 4, 4, 2, 1, 1, 5, 4};

In[2]:=
Flatten /@ Thread[
    {Flatten /@ ({Union[#1], Length[#1]} & ) /@
       Split[Sort[lstIn]]}]

Out[2]=
{{1, 2}, {2, 2}, {3, 1}, {4, 3}, {5, 1}}

Best regards,
/J.M.


  • Prev by Date: Compile Fourier (2)
  • Next by Date: Re: MatrixForm to JPG
  • Previous by thread: Re: Extract values and multilpicities from list
  • Next by thread: Re: Extract values and multilpicities from list