MathGroup Archive 2006

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

Search the Archive

RE: Sum elements in a list with conditions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg69835] RE: [mg69828] Sum elements in a list with conditions
  • From: "David Park" <djmp at earthlink.net>
  • Date: Mon, 25 Sep 2006 03:52:46 -0400 (EDT)

Guillermo,

testlist = {{a1, b1}, {a2, b3}, {a2, b4}, {a3, b5}, {a3, b6}, {a2, b2}};

Print["The starting list"]
testlist
Print["Sort the list"]
Sort[testlist]
Print["Split the list on equality in the first element"]
Split[%%, First[#1] === First[#2] &]
Print["Sum the second elements in each group using the following function:
", \
{Part[#, 1, 1], Last@Total[#]} &]
{Part[#, 1, 1], Last@Total[#]} & /@ %%

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/


From: Guillermo Sanchez [mailto:guillermo.sanchez at hotmail.com]
To: mathgroup at smc.vnet.net


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



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