Re: Summing a 2D list

2008-06-14 Thread MRAB
On Jun 14, 4:05 pm, sturlamolden <[EMAIL PROTECTED]> wrote: > On Jun 12, 3:48 pm, Mark <[EMAIL PROTECTED]> 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

Re: Summing a 2D list

2008-06-14 Thread sturlamolden
On Jun 12, 3:48 pm, Mark <[EMAIL PROTECTED]> 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

Re: Summing a 2D list

2008-06-14 Thread Karsten Heymann
Maric Michaud <[EMAIL PROTECTED]> writes: > Le Friday 13 June 2008 17:55:44 Karsten Heymann, vous avez écrit : >> Maric Michaud <[EMAIL PROTECTED]> writes: >> > So, writing C in python, which has dictionnary as builtin type, >> > should be considered "more elegant" ? >> >> IMO that's a bit harsh.

Re: Summing a 2D list

2008-06-13 Thread Maric Michaud
Le Friday 13 June 2008 18:55:24 Maric Michaud, vous avez écrit : > > approximately the double amount of memory compared to the other. > > I don't see how you came to this conclusion. Are you sure the extra list > take twice more memory than the extra dictionary ? twice less, I meant, of course...

Re: Summing a 2D list

2008-06-13 Thread Maric Michaud
Hello, Le Friday 13 June 2008 17:55:44 Karsten Heymann, vous avez écrit : > Maric Michaud <[EMAIL PROTECTED]> writes: > > So, writing C in python, which has dictionnary as builtin type, > > should be considered "more elegant" ? > > IMO that's a bit harsh. > harsh ? Sorry, I'm not sure to understa

Re: Summing a 2D list

2008-06-13 Thread Karsten Heymann
Hi Maric, Maric Michaud <[EMAIL PROTECTED]> writes: > So, writing C in python, which has dictionnary as builtin type, > should be considered "more elegant" ? IMO that's a bit harsh. > You are comparing apples with lemons, there is no such a difference > between list index access and dictionnary

Re: Summing a 2D list

2008-06-13 Thread Karsten Heymann
Paddy <[EMAIL PROTECTED]> writes: > How does your solution fare against the defaultdict solution of: > > d = collections.defaultdict(int) > for u,s in zip(users,score): d[u] += s list: 0.931s dict + "in": 1.495s defaultdict : 1.991s dict + "if": ~2s dict + "try": ~4s I've posted the (ve

Re: Summing a 2D list

2008-06-13 Thread Maric Michaud
Le Friday 13 June 2008 14:12:40 Karsten Heymann, vous avez écrit : > Hi Mark, > > Mark <[EMAIL PROTECTED]> writes: > > I have a scenario where I have a list like this: > > > > User            Score > > 1                 0 > > 1                 1 > > 1                 5 > > 2                 3 > > 2

Re: Summing a 2D list

2008-06-13 Thread Paddy
On Jun 13, 1:12 pm, Karsten Heymann <[EMAIL PROTECTED]> wrote: > Hi Mark, > > > > Mark <[EMAIL PROTECTED]> writes: > > I have a scenario where I have a list like this: > > > UserScore > > 1 0 > > 1 1 > > 1 5 > > 2 3 > > 2

Re: Summing a 2D list

2008-06-13 Thread Karsten Heymann
Hi Björn, "BJörn Lindqvist" <[EMAIL PROTECTED]> writes: > On Fri, Jun 13, 2008 at 2:12 PM, Karsten Heymann > <[EMAIL PROTECTED]> wrote: >> summed_up={} >> for user,vote in pairs: >> if summed_up.has_key(user): >>summed_up[user]+=vote >> else: >>summed_up[user]=vote > > You'll save even m

Re: Summing a 2D list

2008-06-13 Thread Gerhard Häring
BJörn Lindqvist wrote: [...] Here is another solution: from itertools import groupby from operator import itemgetter users = [1, 1, 1, 2, 2, 3, 4, 4, 4] scores = [0, 1, 5, 3, 1, 2, 3, 3, 2] for u, s in groupby(zip(users, scores), itemgetter(0)): print u, sum(y for x, y in s) Except that

Re: Summing a 2D list

2008-06-13 Thread BJörn Lindqvist
On Thu, Jun 12, 2008 at 3:48 PM, Mark <[EMAIL PROTECTED]> wrote: > Hi all, > > I have a scenario where I have a list like this: > > UserScore > 1 0 > 1 1 > 1 5 > 2 3 > 2 1 > 3 2 > 4

Re: Summing a 2D list

2008-06-13 Thread BJörn Lindqvist
On Fri, Jun 13, 2008 at 2:12 PM, Karsten Heymann <[EMAIL PROTECTED]> wrote: > Although your problem has already been solved, I'd like to present a > different approach which can be quite a bit faster. The most common > approach seems to be using a dictionary: > > summed_up={} > for user,vote in pai

Re: Summing a 2D list

2008-06-13 Thread Karsten Heymann
Hi Mark, Mark <[EMAIL PROTECTED]> writes: > I have a scenario where I have a list like this: > > UserScore > 1 0 > 1 1 > 1 5 > 2 3 > 2 1 > 3 2 > 4 3 > 4 3 > 4

Re: Counting things fast - was Re: Summing a 2D list

2008-06-12 Thread Paddy
On Jun 12, 4:14 pm, Gerhard Häring <[EMAIL PROTECTED]> wrote: > Aidan wrote: > > does this work for you? > > > users = [1,1,1,2,2,3,4,4,4] > > score = [0,1,5,3,1,2,3,3,2] > > > d = dict() > > > for u,s in zip(users,score): > > if d.has_key(u): > > d[u] += s > > else: > > d[u] = s > > >

Re: Summing a 2D list

2008-06-12 Thread Mark
On Jun 12, 3:45 pm, Aidan <[EMAIL PROTECTED]> wrote: > Aidan wrote: > > Mark wrote: > >> John, it's a QuerySet coming from a database in Django. I don't know > >> enough about the structure of this object to go into detail I'm > >> afraid. > > >> Aidan, I got an error trying your suggestion: 'zip a

Counting things fast - was Re: Summing a 2D list

2008-06-12 Thread Gerhard Häring
Aidan wrote: does this work for you? users = [1,1,1,2,2,3,4,4,4] score = [0,1,5,3,1,2,3,3,2] d = dict() for u,s in zip(users,score): if d.has_key(u): d[u] += s else: d[u] = s for key in d.keys(): print 'user: %d\nscore: %d\n' % (key,d[key]) I've recently had the very same prob

Re: Summing a 2D list

2008-06-12 Thread Gerhard Häring
Mark wrote: John, it's a QuerySet coming from a database in Django. I don't know enough about the structure of this object to go into detail I'm afraid. [...] Then let the database do the summing up. That's what it's there for :-) select user, sum(score) from score_table group by user or some

Re: Summing a 2D list

2008-06-12 Thread Aidan
Aidan wrote: Mark wrote: John, it's a QuerySet coming from a database in Django. I don't know enough about the structure of this object to go into detail I'm afraid. Aidan, I got an error trying your suggestion: 'zip argument #2 must support iteration', I don't know what this means! well, if

Re: Summing a 2D list

2008-06-12 Thread Aidan
Mark wrote: John, it's a QuerySet coming from a database in Django. I don't know enough about the structure of this object to go into detail I'm afraid. Aidan, I got an error trying your suggestion: 'zip argument #2 must support iteration', I don't know what this means! well, if we can create

Re: Summing a 2D list

2008-06-12 Thread Mark
John, it's a QuerySet coming from a database in Django. I don't know enough about the structure of this object to go into detail I'm afraid. Aidan, I got an error trying your suggestion: 'zip argument #2 must support iteration', I don't know what this means! Thanks to all who have answered! Sorry

Re: Summing a 2D list

2008-06-12 Thread Diez B. Roggisch
> To be honest I'm relatively new to Python, so I don't know too much > about how all the loop constructs work and how they differ to other > languages. I'm building an app in Django and this data is coming out > of a database and it looks like what I put up there! > > This was my (failed) attempt

Re: Summing a 2D list

2008-06-12 Thread John Salerno
"Mark" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] On Jun 12, 3:02 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > Mark wrote: --- This was my (failed) attempt: predictions = Prediction.objects.all() scores = [] for prediction in predictions: i = [prediction.predictor.id, 0]

Re: Summing a 2D list

2008-06-12 Thread Aidan
Mark wrote: Hi all, I have a scenario where I have a list like this: UserScore 1 0 1 1 1 5 2 3 2 1 3 2 4 3 4 3 4 2 And I need to add up th

Re: Summing a 2D list

2008-06-12 Thread Mark
On Jun 12, 3:02 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > Mark wrote: > > Hi all, > > > I have a scenario where I have a list like this: > > > User            Score > > 1                 0 > > 1                 1 > > 1                 5 > > 2                 3 > > 2                 1 > >

Re: Summing a 2D list

2008-06-12 Thread Chris
On Jun 12, 3:48 pm, Mark <[EMAIL PROTECTED]> wrote: > Hi all, > > I have a scenario where I have a list like this: > > User            Score > 1                 0 > 1                 1 > 1                 5 > 2                 3 > 2                 1 > 3                 2 > 4                 3 > 4

Re: Summing a 2D list

2008-06-12 Thread Benjamin Kaplan
On Thu, Jun 12, 2008 at 9:48 AM, Mark <[EMAIL PROTECTED]> wrote: > Hi all, > > I have a scenario where I have a list like this: > > UserScore > 1 0 > 1 1 > 1 5 > 2 3 > 2 1 > 3 2 > 4

Re: Summing a 2D list

2008-06-12 Thread Diez B. Roggisch
Mark wrote: > Hi all, > > I have a scenario where I have a list like this: > > UserScore > 1 0 > 1 1 > 1 5 > 2 3 > 2 1 > 3 2 > 4 3 > 4 3 > 4