Re: Summing/combining tuples

2016-05-20 Thread Dan Sommers
On Sat, 21 May 2016 03:19:49 +, Dan Sommers wrote: >> Is there something shorter and sweeter for the summation? > > from itertools import groupby > from operator import itemgetter > > result = [(k, >sum(map(itemgetter(2), v)), >sum(map(itemgetter(3

Re: Summing/combining tuples

2016-05-20 Thread Dan Sommers
On Wed, 18 May 2016 20:59:55 -0400, DFS wrote: > Have aList = [ > ('x','Name1', 1, 85), > ('x','Name2', 3, 219), > ('x','Name2', 1, 21), > ('x','Name3', 6, 169) > ] > > want > > aList = [ > ('Name1', 1, 85), > ('Name2', 4, 240), > ('Name3', 6, 169) > ] [snip] > Is there something shorter and

Re: Summing/combining tuples

2016-05-19 Thread Larry Hudson via Python-list
On 05/18/2016 09:53 PM, DFS wrote: On 5/18/2016 10:58 PM, Larry Hudson wrote: [snip...] Why two loops? Put both summations in a single loop. Then you're only scanning the alist once instead of twice. groups1 = defaultdict(int) groups2 = defaultdict(int) for nm, matches, words in alist: g

Re: Summing/combining tuples

2016-05-18 Thread Larry Hudson via Python-list
On 05/18/2016 05:59 PM, DFS wrote: Have aList = [ ('x','Name1', 1, 85), ('x','Name2', 3, 219), ('x','Name2', 1, 21), ('x','Name3', 6, 169) ] want aList = [ ('Name1', 1, 85), ('Name2', 4, 240), ('Name3', 6, 169) ] This drops the first element in each tuple: alist = [(b,c,d) for a,b,c,d in ali