updating dictionaries from/to dictionaries
Edwin.Madari at VerizonWireless.com
Edwin.Madari at VerizonWireless.com
Thu Aug 14 21:23:59 EDT 2008
if the values for any of the keys are None, both do not work as can be seen below!!.
since both loops are iterating over keys(), 'get' method which would return a '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: python-list-bounces+edwin.madari=verizonwireless.com at python.org
[mailto:python-list-bounces+edwin.madari=verizonwireless.com at python.org]
On Behalf Of Brandon
Sent: Thursday, August 14, 2008 8:35 PM
To: python-list at 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.
More information about the Python-list
mailing list