Re: Summing list subsets
- To: mathgroup at smc.vnet.net
- Subject: [mg30754] Re: Summing list subsets
- From: "Orestis Vantzos" <atelesforos at hotmail.com>
- Date: Sun, 9 Sep 2001 03:26:56 -0400 (EDT)
- Organization: National Technical University of Athens, Greece
- References: <9ncfgn$prb$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
In[13]:= zzz[x_, y_, func_] := {#, func @@ Extract[y, Position[x, #]]} & /@ Union[x] In[15]:= zzz[{a, a, b, b, b, b, c, d, d, d, d, d}, {1, 1, 2, 2, 2, 2, 3, 4, 4, 4, 4, 4}, Plus] Out[15]= {{a, 2}, {b, 8}, {c, 3}, {d, 20}} The elements don't have to be consecutive. Orestis "Mark Coleman" <mcoleman at bondspace.com> wrote in message news:9ncfgn$prb$1 at smc.vnet.net... > 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 > >