By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,971 Members | 1,467 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,971 IT Pros & Developers. It's quick & easy.

RE: updating dictionaries from/to dictionaries

P: n/a
if the values for any of the keys are None, both do not work as can be seenbelow!!.

since both loops are iterating over keys(), 'get' method which would returna '0' for non existing key does not encounter any non-existing keys.
================================================== ==========
import copy, sys

foo = { 'one' : 1, 'two' : 2, 'three' : None }
bar = copy.deepcopy(foo)

for key in foo:
foo[key] += bar.get(key, 0)
print 'foo', sys.exc_info()[:2]

for key in bar:
foo[key] += bar[key]
print 'bar', sys.exc_info()[:2]
foo (<type 'exceptions.TypeError'>, TypeError("unsupported operand type(s) for +=: 'NoneType' and 'NoneType'",))
bar (<type 'exceptions.TypeError'>, TypeError("unsupported operand type(s) for +=: 'NoneType' and 'NoneType'",))

hope that helps to clarify both point of views.
thanks Edwin

-----Original Message-----
From: py************************************************ **
[mailto:py***************************************** *********]
On Behalf Of Brandon
Sent: Thursday, August 14, 2008 8:35 PM
To: py*********
Subject: Re: updating dictionaries from/to dictionaries
(1) iterating over foo:
for key in foo:
foo[key] += bar.get(key, 0)

(2) iterating over bar:
for key in bar:
foo[key] += bar[key]

I (again) challenge you to say *why* you feel that the "iterating over
bar" solution will not work.

Well if you're going to be clever enough to iterate over bar and then
send the results to another dictionary altogether, I obviously cannot
put up a good argument on this matter!

Thanks for the input, I appreciate it.
The information contained in this message and any attachment may be
proprietary, confidential, and privileged or subject to the work
product doctrine and thus protected from disclosure. If the reader
of this message is not the intended recipient, or an employee or
agent responsible for delivering this message to the intended
recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited.
If you have received this communication in error, please notify me
immediately by replying to this message and deleting it and all
copies and backups thereof. Thank you.
Aug 15 '08 #1
Share this Question
Share on Google+
1 Reply

P: n/a
On Aug 15, 11:23 am, wrote:
if the values for any of the keys are None, both do not work as can be seen below!!.
If any of the values are None, nothing can work. The OP justifiably
(given his problem domain (bigram frequencies)) expects that all
values will be integers. If this is not the case, it means that the
code that populates his dictionaries is majorly stuffed, an exception
will be raised, and he can fix it.

Aug 15 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.