Re: Sum elements in a list with conditions

• To: mathgroup at smc.vnet.net
• Subject: [mg69843] Re: [mg69828] Sum elements in a list with conditions
• From: Bruce Colletti <vze269bv at verizon.net>
• Date: Mon, 25 Sep 2006 03:53:05 -0400 (EDT)

```There's undoubtedly a better way, but here goes:

X={{1,2},{4,5},{1,6},{2,9},{1,10},{4,3}};

X=Split[Sort@X,#1[[1]]==#2[[1]]&];

Map[{#[[1,1]],Total@#[[All,2]]}&,X]

Out[8]=
{{1,18},{2,9},{4,8}}

Bruce

=====================
From: Guillermo Sanchez <guillermo.sanchez at hotmail.com>
To: mathgroup at smc.vnet.net
Subject: [mg69843] [mg69828] Sum elements in a list with conditions

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: Sum elements in a list with conditions
• 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