MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: List Operations

On 14 Apr 2006, at 17:32, 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

It is impossible in Mathematica to get  Mathematica to output  
something in the form a+a+a and the like instead of 3a , without  
using HoldForm. So, you should  either use something like this:

mylist = {{1, a, b}, {1, a, b}, {1, a,
b}, {2, b, z}, {2, b, z}, {2, b, z}, {2, b, z}};

mylist /. {i_, x_, y_} -> {i, x*y} //. {a___, {i_, x_}, {i_, y_},  
b___} -> {a, {i, x + y}, b}

{{1, 3*a*b}, {2, 4*b*z}}

or if you really want it to look as you wrote it:



mylist /. {i_, x_, y_} -> {i, x*y} //. {a___, {i_, x_}, {i_, y_},  
b___} -> {a, {i, HoldForm[x + y]}, b}

{{1,a*b+a*b+a*b},{2,b z+b*z+b*z+b*z}}

Andrzej Kozlowski

  • Prev by Date: Re: NDSolve
  • Next by Date: Re: NDSolve
  • Previous by thread: List Operations
  • Next by thread: Re: List Operations