Re: Summing list subsets

• To: mathgroup at smc.vnet.net
• Subject: [mg30804] Re: Summing list subsets
• From: Ignacio Rodriguez <ignacio at sgirmn.pluri.ucm.es>
• Date: Wed, 19 Sep 2001 00:16:45 -0400 (EDT)
• Organization: UCM
• References: <9ncfgn\$prb\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Try the following

Mixer[x_List,y_List]:=Table[{Part[x,i],{Part[y,i]}},{i,1,Length[x]}] /;
Length[x]==Length[y]
Compacter[x_]:=ReplaceRepeated[x,{a___,{b_,{c__}},d___,{b_,{e__}},f___}->{a,{b,{c,e}},d,f}];

Applier[f_,x_]:=Table[{Part[x,i,1],Apply[f,Part[x,i,2]]},{i,1,Length[x]}];
x={a,a,b,b,b,b,c,d,d,d,d,d}
y={1,1,2,2,2,2,3,4,4,4,4,4}
t1=Mixer[x,y]
t2=Compacter[t1]
z=Applier[Plus,t2]

Mark Coleman wrote:

> Greetings:
>
> Consider two lists:
>
> x = {a,a,b,b,b,b,c,d,d,d,d,d,} and  y = {1,1,2,2,2,2,3,4,4,4,4,4}
>
> I would like to have a function that returns the sum (or any other function)
> of each unique element of x, given the corresponding value in y. That is,
> for a 'Sum', the result would be
>
> z={{a,2},{b,8},{c,3},{d,20}}
>
> This is similar in spirit to a common database aggregation problem.
>
> Any ideas?
>
> Thanks.
>
> -Mark

--

Ignacio Rodriguez Ramirez de Arellano
Unidad de RMN
Universidad Complutense
Paseo Juan XXIII, 1
Madrid 28040, Spain

Tel. 34-91-394-3288
Fax  34-91-394-3245
e-mail: ignacio at sgirmn.pluri.ucm.es

```

• Prev by Date: Numbered equations
• Next by Date: Re: Combinations
• Previous by thread: Re: Summing list subsets
• Next by thread: RE: Re: Summing list subsets