MathGroup Archive 2006

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

Search the Archive

Re: Sum elements in a list with conditions


On 24 Sep 2006, at 12:45, Guillermo Sanchez wrote:

> Dear group
> I have a pair of elements list. The second elements of the list should
> be summed when the first element of the pairs are equals. Example
>
> Given
>
> {{a1, b1},{a2,b2},{a2,b3},{a2,b4},{a3, b5}, {a3, b6}}
>
> the output should be
>
> {{a1, b1},{a2, b2+b3+b4},{a3, b5+b6}}
>
> Thanks
>
> Guillermo
>


{{a1, b1}, {a2, b2}, {a2, b3}, {a2, b4}, {a3, b5},
    {a3, b6}} //. {a___, {x_, y_}, b___, {x_, z_},
     c___} :> {a, {x, y + z}, b, c}


{{a1, b1}, {a2, b2 + b3 + b4}, {a3, b5 + b6}}

Andrzej Kozlowski
Tokyo, Japan


  • Prev by Date: Re: Sum elements in a list with conditions
  • Next by Date: Re: cusp fold surface
  • Previous by thread: Re: Sum elements in a list with conditions
  • Next by thread: RE: Sum elements in a list with conditions