Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

building a dict

1 view
Skip to first unread message

vsoler

unread,
Mar 13, 2010, 10:05:06 AM3/13/10
to
Say that "m" is a tuple of 2-tuples

m=(('as',3), ('ab',5), (None, 1), ('as',None), ('as',6))

and I need to build a "d" dict where each key has an associated list
whose first element is the count, and the second is the sum. If a 2-
tuple contains a None value, it should be discarded.

The expected result is:
d={'as':[2, 9], 'ab': [1,5]}

How should I proceed? So far I have been unsuccessful. I have tried
with a "for" loop.

Thank you for your help

Jon Clements

unread,
Mar 13, 2010, 10:25:45 AM3/13/10
to

Something like:

d = defaultdict( lambda: [0,0] )
for key, val in filter(lambda L: not any(i is None for i in L), m):
d[key][0] += 1
d[key][1] += val

hth

Jon

Patrick Maupin

unread,
Mar 13, 2010, 10:28:19 AM3/13/10
to

Post your first try at a for loop, and people might be willing to
point out problems, but this is such a basic for loop that it is
unlikely that anybody is going to write your ENTIRE homework for you.

Regards,
Pat

Steve Holden

unread,
Mar 13, 2010, 10:36:46 AM3/13/10
to pytho...@python.org

Here's a fairly simple-minded approach using a defaultdict, which calls
the dflt() function to create a value when the key is absent.

>>> from collections import defaultdict
>>> def dflt():
... return [0, 0]
...
>>> m = (('as',3), ('ab',5), (None, 1), ('as',None), ('as',6))
>>> d = defaultdict(dflt)
>>> for key, n in m:
... if key is not None and n is not None:
... c, t = d[key]
... d[key] = [c+1, t+n]
...
>>> d
defaultdict(<function dflt at 0x7f0bcb1b0ed8>,
{'as': [2, 9], 'ab': [1, 5]})
>>>

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
See PyCon Talks from Atlanta 2010 http://pycon.blip.tv/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/

Jon Clements

unread,
Mar 13, 2010, 10:58:02 AM3/13/10
to

I was thinking it's possibly homework, but looking at previous posts
it's fairly unlikely.

(If it is, then mea culpa, but as Steve has replied, I think I'll
manage to sleep tonight not worrying about the influx of uneducated,
incompetent and otherwise useless developers to the market).

However, they're receiving some 'elegant' solutions which no professor
(unless they're a star pupil - in which case they wouldn't be asking)
would take as having been done by their selves.
(Or at least I hope not)

But yes, I would certainly be interested in the 'unsuccessful
attempt'.
(To the OP, do post your attempts, it does give more validity).


Cheers,

Jon.

ru...@yahoo.com

unread,
Mar 13, 2010, 11:13:51 AM3/13/10
to

This is probably what you (OP) were trying to come up with?
[untested]

d = {}
for item in m:
key = m[0]; value = m[1]
if key is None or value is None: continue
if key not in dict:
d[key] = [value]
else:
d[key].append (value)

You can replace the
for item in m:
key = m[0]; value = m[1]
above with
for key, value in m:
which is a little nicer.

However, as other responses point out, when you want
to "accumulate" results in a dict, collections.defaultdict
should pop into your mind first.

ru...@yahoo.com

unread,
Mar 13, 2010, 11:26:59 AM3/13/10
to

Oops, didn't read very carefully, did I?

That should be:
d = {}


for item in m:
key = m[0]; value = m[1]
if key is None or value is None: continue
if key not in dict:

d[key] = [1, value]
else:


d[key][0] += 1

d[key][1] += value

ru...@yahoo.com

unread,
Mar 13, 2010, 12:16:16 PM3/13/10
to
On Mar 13, 9:26 am, ru...@yahoo.com wrote:
> That should be:
> d = {}
> for item in m:
    key = item[0];  value = item[1]

>     if key is None or value is None: continue
>     if key not in dict:
>         d[key] = [1, value]
>     else:
>         d[key][0] += 1
>         d[key][1] += value

That's it. Any other mistakes, you find 'em.

vsoler

unread,
Mar 13, 2010, 4:42:12 PM3/13/10
to

Thank you all. Your answers are more than valuable to me. I'll study
them carefully, but no doubt, my post has been answered.

By the way, I suppose I am the OP. Since I am not an native English
speaking person, I do not know what it stands for. Perhaps you can
tell me.

From what I see from your posts, you would have preferred that I
included in my original post my "for loop", so that the post is not so
abstract. I have taken note and I'll make it better next time.

Thank you for your help.

Vicente Soler

ru...@yahoo.com

unread,
Mar 13, 2010, 5:04:54 PM3/13/10
to
On Mar 13, 2:42 pm, vsoler <vicente.so...@gmail.com> wrote:

> By the way, I suppose I am the OP. Since I am not an native English
> speaking person, I do not know what it stands for. Perhaps you can
> tell me.

OP means Original Poster (the person who started the discussion)
or sometimes Original Post, depending on context.

Andreas Waldenburger

unread,
Mar 13, 2010, 8:01:37 PM3/13/10
to
On Sat, 13 Mar 2010 13:42:12 -0800 (PST) vsoler
<vicent...@gmail.com> wrote:

> By the way, I suppose I am the OP. Since I am not an native English
> speaking person, I do not know what it stands for. Perhaps you can
> tell me.
>

Perhaps you can find out yourself:

http://www.urbandictionary.com/define.php?term=op

/W


--
INVALID? DE!

Steve Holden

unread,
Mar 14, 2010, 8:36:55 AM3/14/10
to pytho...@python.org
Possibly so, and that's a useful site, but I hope you aren't suggesting
that Vicente shouldn't have asked. It seemed like a perfectly acceptable
question to me.

Andreas Waldenburger

unread,
Mar 14, 2010, 9:28:09 AM3/14/10
to
On Sun, 14 Mar 2010 08:36:55 -0400 Steve Holden <st...@holdenweb.com>
wrote:

> Andreas Waldenburger wrote:
> > On Sat, 13 Mar 2010 13:42:12 -0800 (PST) vsoler
> > <vicent...@gmail.com> wrote:
> >
> >> By the way, I suppose I am the OP. Since I am not an native English
> >> speaking person, I do not know what it stands for. Perhaps you can
> >> tell me.
> >>
> > Perhaps you can find out yourself:
> >
> > http://www.urbandictionary.com/define.php?term=op
> >
> Possibly so, and that's a useful site, but I hope you aren't
> suggesting that Vicente shouldn't have asked. It seemed like a
> perfectly acceptable question to me.
>

I was a bit trigger happy there, I admit. But with vocabulary questions
like these, my first reflex is to ask the web rather than people,
because it's usually quicker than Usenet (albeit often only by a minute
or so). I somehow felt the need to bestow that bit of "wisdom" on the
world.

But yeah, the question is fine, of course.

/W

--
INVALID? DE!

0 new messages