Help with dictionary, please

Oleg Broytmann phd at emerald.netskate.ru
Fri Aug 6 08:38:34 EDT 1999


On 6 Aug 1999 rbl at hal.EPBI.CWRU.Edu wrote:
> 	 	   key = (prev2,prev1)
> 	           dict[key] = dict[key] + 1
>    and got this error:
> 	  File "Metro:Python 1.5.1:learn2.py", line 34
>           dict[key] = dict[key] + 1
>                          ^
> 	SyntaxError: invalid token
> 
> I'm obviously missing something here!

   You messed tabs and spaces. Make all things tabs or spaces, but do not
use both.

   Advice N2:
   The line
      dict[key] = dict[key] + 1
   will cause troubles, if key not in dict. You better write it the
following way:
      dict[key] = dict.get(key, 0) + 1

   dict.get is a function that returns either value for the key or default,
if the key not in the dictionary.

> Any guidance greatly appreciated!
> 
> Thank you,
> Rob Lake
> rbl at hal.cwru.edu

Oleg.
---- 
    Oleg Broytmann        Netskate/Inter.Net.Ru        phd at emerald.netskate.ru
           Programmers don't die, they just GOSUB without RETURN.





More information about the Python-list mailing list