[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

building a dict

vsoler

3/13/2010 3:05:00 PM

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
12 Answers

Jon Clements

3/13/2010 3:26:00 PM

0

On 13 Mar, 15:05, vsoler <vicente.so...@gmail.com> wrote:
> 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

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

3/13/2010 3:28:00 PM

0

On Mar 13, 9:05 am, vsoler <vicente.so...@gmail.com> wrote:
> 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.

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

3/13/2010 3:37:00 PM

0

vsoler wrote:
> 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

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://pyco...
Holden Web LLC http://www.hold...
UPCOMING EVENTS: http://holdenweb.event...

Jon Clements

3/13/2010 3:58:00 PM

0

On 13 Mar, 15:28, Patrick Maupin <pmau...@gmail.com> wrote:
> On Mar 13, 9:05 am, vsoler <vicente.so...@gmail.com> wrote:
>
> > 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.
>
> 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

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.

rurpy@yahoo.com

3/13/2010 4:14:00 PM

0

On Mar 13, 8:28 am, Patrick Maupin <pmau...@gmail.com> wrote:
> On Mar 13, 9:05 am, vsoler <vicente.so...@gmail.com> wrote:
>
> > 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.
>
> 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.

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.

rurpy@yahoo.com

3/13/2010 4:27:00 PM

0

On Mar 13, 9:13 am, ru...@yahoo.com wrote:
> On Mar 13, 8:28 am, Patrick Maupin <pmau...@gmail.com> wrote:
>
>
>
> > On Mar 13, 9:05 am, vsoler <vicente.so...@gmail.com> wrote:
>
> > > 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.
>
> > 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.
>
> 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.

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

rurpy@yahoo.com

3/13/2010 5:16:00 PM

0

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

3/13/2010 9:42:00 PM

0

On 13 mar, 18:16, ru...@yahoo.com wrote:
> 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.

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

rurpy@yahoo.com

3/13/2010 10:05:00 PM

0

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

3/14/2010 1:02:00 AM

0

On Sat, 13 Mar 2010 13:42:12 -0800 (PST) vsoler
<vicente.soler@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.p...

/W


--
INVALID? DE!