RE: updating dictionaries from/to dictionaries

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Edwin.Madari@VerizonWireless.com

    #1

    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(f oo)

    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.Typ eError'>, TypeError("unsu pported operand type(s) for +=: 'NoneType' and 'NoneType'",))
    bar (<type 'exceptions.Typ eError'>, TypeError("unsu pported 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.m adari=verizonwi reless.com@pyth on.org
    [mailto:python-list-bounces+edwin.m adari=verizonwi reless.com@pyth on.org]
    On Behalf Of Brandon
    Sent: Thursday, August 14, 2008 8:35 PM
    To: python-list@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.
    --



    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.


  • John Machin

    #2
    Re: updating dictionaries from/to dictionaries

    On Aug 15, 11:23 am, Edwin.Mad...@Ve rizonWireless.c om 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.

    Comment

    Working...