building a dict

V

vsoler

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
 
J

Jon Clements

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
 
P

Patrick Maupin

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
 
S

Steve Holden

vsoler said:
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.
.... return [0, 0]
........ if key is not None and n is not None:
.... c, t = d[key]
.... d[key] = [c+1, t+n]
....
regards
Steve
 
J

Jon Clements

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.
 
R

rurpy

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.
 
R

rurpy

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
 
R

rurpy

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.
 
V

vsoler

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
 
R

rurpy

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.
 
A

Andreas Waldenburger

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top