Re: Extract values and multilpicities from list
- To: mathgroup at smc.vnet.net
- Subject: [mg64872] Re: [mg64849] Extract values and multilpicities from list
- From: Pratik Desai <pdesai1 at umbc.edu>
- Date: Mon, 6 Mar 2006 05:01:26 -0500 (EST)
- References: <200603050819.DAA09793@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 > > > Here is one such attempt In[1]:= Clear[list1,CommonVals,data] CommonVals[list_?ListQ]:=Module[{list1=list},Table[{Union[list1][[r]],Count[list1,Union[list1][[r]]]},{r,1,Length[Union[list1]]}]] list1 = {2,3,4,4,2,1,1,5,4} CommonVals[list1]//Timing data=Table[Random[Integer, {1, 9}], {10^5}]; CommonVals[data]//Timing Out[3]= {2,3,4,4,2,1,1,5,4} Out[4]= {0. Second,{{1,2},{2,2},{3,1},{4,3},{5,1}}} Out[6]= {0.771 Second,{{1,11091},{2, 10991},{3,11107},{4,11175},{5,11022},{6,11133},{7,11098},{8,11194},{ 9,11189}}}
- References:
- Extract values and multilpicities from list
- From: "Dr. Wolfgang Hintze" <weh@snafu.de>
- Extract values and multilpicities from list