Re: Summing list subsets

• To: mathgroup at smc.vnet.net
• Subject: [mg30740] Re: [mg30711] Summing list subsets
• From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
• Date: Sun, 9 Sep 2001 03:26:31 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```I admit I am not 100% sure if I have understood you, but it seems to me
that the following function mc does what you want:

mc[f_, x_, y_] := Transpose[{Union[x], f @@@ Split[x] /. Thread[Rule[x,
y]]}]

In your case:

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};

In[4]:=
mc[Plus,x,y]

Out[4]=
{{a,2},{b,8},{c,3},{d,20}}

In[5]:=
mc[Times,x,y]

Out[5]=
{{a,1},{b,16},{c,3},{d,1024}}

Andrzej Kozlowski
Toyama International University
JAPAN
http://platon.c.u-tokyo.ac.jp/andrzej/

On Saturday, September 8, 2001, at 03:55  PM, 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
>
>
>

```

• Prev by Date: Re: Changing the default font to arial
• Next by Date: Re: algebraic substitution rules
• Previous by thread: Re: Summing list subsets
• Next by thread: Re: Summing list subsets