MathGroup Archive 2006

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

Search the Archive

Re: List Operations


On 15 Apr 2006, at 10:10, Andrzej Kozlowski wrote:

>
> 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:
>
>
> SetAttributes[plus,{Flat,OneIdentity}]
>
>
> Format[plus[x___]]:=HoldForm[Plus[x]]
>
>
> 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
>


Sorry, I copied and pasted not what I intended. The second example  
should have been:


SetAttributes[plus,{Flat,OneIdentity}];

Format[plus[x___]]:=HoldForm[Plus[x]];

Split[mylist /. {i_, x_, y_} -> {i, x*y}, #1[[1]] == #2[[1]] & ] //.  
{a___, {i_, x_}, {i_, y_}, b___} ->
    {a, {i, plus[x, y]}, b}


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





  • Prev by Date: problems with sum functions/ factoring the factorial
  • Next by Date: Re: List Operations
  • Previous by thread: Re: List Operations
  • Next by thread: Re: List Operations