468,535 Members | 1,657 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,535 developers. It's quick & easy.

RE: updating dictionaries from/to dictionaries

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)

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

try:
for key in bar:
foo[key] += bar[key]
except:
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************************************************ **@python.org
[mailto:py***************************************** *********@python.org]
On Behalf Of Brandon
Sent: Thursday, August 14, 2008 8:35 PM
To: py*********@python.org
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.
--
http://mail.python.org/mailman/listinfo/python-list
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
1 1253
On Aug 15, 11:23 am, Edwin.Mad...@VerizonWireless.com 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.

Similar topics

7 posts views Thread by Kerry Neilson | last post: by
13 posts views Thread by omission9 | last post: by
reply views Thread by Till Plewe | last post: by
8 posts views Thread by Frohnhofer, James | last post: by
3 posts views Thread by Shivram U | last post: by
210 posts views Thread by Christoph Zwerschke | last post: by
3 posts views Thread by Faisal Alquaddoomi | last post: by
9 posts views Thread by Brandon | last post: by
1 post views Thread by Edwin.Madari | last post: by
14 posts views Thread by cnb | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.