Help with dictionary, please
Frank Niessink
frankn=news at cs.vu.nl
Fri Aug 6 08:32:27 EDT 1999
rbl at hal.cwru.edu wrote:
> if prev1 != '' and prev2 != '':
> 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
You need to initialize the value at dict[key], it is not automatically
set to 0 or something like that. For example:
if dict.has_key(key):
dict[key] = dict[key] + 1
else:
dict[key] = 1
However, you should've got a KeyError, I don't understand why you're
getting the SyntaxError...
HTH, Frank
--
He went across to the telephone machine and fiddled and fumed with all its
buttons for a while, because it was the one which was particularly recom-
mended by Which? magazine and is almost impossible to use without going mad.
-- Douglas Adams, 'So long, and thanks for all the fish'
More information about the Python-list
mailing list