Simple exercise

Thomas 'PointedEars' Lahn PointedEars at web.de
Thu Mar 10 05:30:13 EST 2016


Rodrick Brown wrote:

> […]
>     if m:
>       if m.group(1) not in od.keys():
>         od[m.group(1)] = int(m.group(2))
>       else:
>         od[m.group(1)] += int(od.get(m.group(1),0))
> […]

This program logic appears to be wrong as you are not adding the value that 
you just read to the dictionary entry for the key that you just read but the 
value that you had in the dictionary for that key before.  Perhaps you were 
looking for this (I also optimized a bit):

        key = m.group(1)
        value = int(m.group(1))

        if key not in od:
          od[key] = value
        else:
          od[key] += value

But there is probably an even more pythonic way to do this.

-- 
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.



More information about the Python-list mailing list