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
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
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.
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...
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
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
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
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
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
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
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
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
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
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
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
>
> >
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
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
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
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
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
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
> 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
"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]
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
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
> >
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
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
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
28 matches
Mail list logo