Re: Extract values and multilpicities from list
- To: mathgroup at smc.vnet.net
- Subject: [mg64859] Re: Extract values and multilpicities from list
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Mon, 6 Mar 2006 05:01:01 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- References: <due96k$ach$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
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.