Summing a 2D list

sturlamolden sturlamolden at yahoo.no
Sat Jun 14 17:05:38 CEST 2008


On Jun 12, 3:48 pm, Mark <markjtur... at gmail.com> wrote:

> Is this possible?

def foobar(user,score):
   sums = {}
   for u,s in zip(user,score):
      try:
         sums[u] += s
      except KeyError:
         sums[u] = s
   return [(u, sums[u]) for u in sums].sort()


usersum = foobar(user,score)
for u,s in usersum:
  print "%d   %d" % (u,s)









More information about the Python-list mailing list