Re: Using reverse iteration to clean up a list

2005-03-13 Thread Michael Hoffman
[EMAIL PROTECTED] wrote: Thank you all so much for the generous dollop of help: the dictionary suggestion is particularly helpful. The problem arises as follows: A software application stores the securities held in a portfolio in a .csv file, one row per security, with three colulmns. The first has

Re: Using reverse iteration to clean up a list

2005-03-13 Thread tkpmep
Thank you all so much for the generous dollop of help: the dictionary suggestion is particularly helpful. The problem arises as follows: A software application stores the securities held in a portfolio in a .csv file, one row per security, with three colulmns. The first has a security identifier o

Re: Using reverse iteration to clean up a list

2005-03-13 Thread Michael Hoffman
[EMAIL PROTECTED] wrote: L=[['A', 100], ['B', 300], ['A', 400], ['B', -100]] I want to aggregate these lists, i.e. to reduce L to L=[['A', 500], ['B', 200]] #500 = 100+400, 200=300-100 """ from itertools import groupby from operator import itemgetter [(key, sum(item[1] for item in sublist)) for ke

Re: Using reverse iteration to clean up a list

2005-03-12 Thread Luis M. Gonzalez
Well, I'm not sure if this is what you want, but you could use a dictionary: >>> d={} >>> for i,e in L: if d.has_key(i): d[i] += e else: d[i] = e >>> d {'A': 500, 'B': 200} >>> -- http://mail.python.org/mailman/listinfo/python-lis

Re: Using reverse iteration to clean up a list

2005-03-12 Thread John Machin
Paul Rubin wrote: > [EMAIL PROTECTED] writes: > > I have list of lists of the following form > > > > L=[['A', 100], ['B', 300], ['A', 400], ['B', -100]] > > > > I want to aggregate these lists, i.e. to reduce L to > > L=[['A', 500], ['B', 200]] #500 = 100+400, 200=300-100 > > How about: > > v

Re: Using reverse iteration to clean up a list

2005-03-12 Thread Paul Rubin
[EMAIL PROTECTED] writes: > I have list of lists of the following form > > L=[['A', 100], ['B', 300], ['A', 400], ['B', -100]] > > I want to aggregate these lists, i.e. to reduce L to > L=[['A', 500], ['B', 200]] #500 = 100+400, 200=300-100 How about: v = {} for name,val in L: v[n