From: vsoler on
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
From: Jon Clements on
On 13 Mar, 15:05, vsoler <vicente.so...(a)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
From: Patrick Maupin on
On Mar 13, 9:05 am, vsoler <vicente.so...(a)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
From: Steve Holden on
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://pycon.blip.tv/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/

From: Jon Clements on
On 13 Mar, 15:28, Patrick Maupin <pmau...(a)gmail.com> wrote:
> On Mar 13, 9:05 am, vsoler <vicente.so...(a)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.