Using reverse iteration to clean up a list
tkpmep at hotmail.com
tkpmep at hotmail.com
Sat Mar 12 23:00:04 EST 2005
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
Here's how I have done it:
L.sort()
for i in range(len(L),0,-1):
if L[i-1][0]=L[i][0]:
L[i-1][2] += L[i][2]
del L[i]
Is there a simpler way to do this using the new reverse iteration
facility in Python 2.4?
Thomas Philips
More information about the Python-list
mailing list