Re: List Operations
- To: mathgroup at smc.vnet.net
- Subject: [mg65700] Re: List Operations
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Sun, 16 Apr 2006 01:45:02 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <e1nn6s$lmm$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
LectorZ wrote: > Hi guys, > > My question: > > mylist={{1,a, b},{1,a, b},{1,a, > b},{2,b,z},{2,b,z},{2,b,z},{2,b,z},....,{n,x,y},{n,x,y},{n,x,y},...{n,x,y}} > > The sublists are of different length. > > I need to calculate the product between the 2nd and 3rd element of > every sublist (e.g. a*b) and then add them up according to the 1st > element: sum of all products where the 1st element is 1, 2, ...n. > > The result should be a list like that: > {{1,a*b+a*b+a*b},{2,b*z+b*z+b*z+b*z}, ...,{n, x*y+x*y+x*y+...+x*y}} > > Thank you for your help. > > LZ > Hi Lector, I have come up with the following expression: In[1]:= mylist = {{1, a, b}, {1, a, b}, {1, a, b}, {2, b, z}, {2, b, z}, {2, b, z}, {2, b, z}, {3, x, y}, {3, x, y}}; In[2]:= ({#1[[1,1]], Plus @@ #1[[2]]} & ) /@ Transpose /@ Split[mylist /. {n_, x_, y_} -> {n, x*y}, #1[[1]] == #2[[1]] & ] Out[2]= {{1, 3*a*b}, {2, 4*b*z}, {3, 2*x*y}} Hope this helps, Jean-Marc