Simple exercise

Chris Angelico rosuav at gmail.com
Thu Mar 10 18:38:01 EST 2016


On Fri, Mar 11, 2016 at 10:24 AM, Gregory Ewing
<greg.ewing at canterbury.ac.nz> wrote:
> Instead, pull them out and give them meaningful names.
> Doing so with the above code gives:
>
>   name = m.group(1)
>   value = m.group(2)
>   if name not in od.keys():
>     od[name] = int(value)
>   else:
>     od[name] += int(od.get(name, 0))
>
> Now it's a lot eaier to see that you haven't used the
> value anywhere in the second case, which should alert
> you that something isn't right.

Although in this case, the code is majorly redundant - and could be
replaced entirely with a defaultdict(int).

ChrisA



More information about the Python-list mailing list