Mailing List Archive

Help with dictionary, please
I'm rapidly trying to learn Python and despite reading the documentation
and the "Learning Python" book, I can't figure out how to do the following:

- I want to have a 2-dimensional array of word counts, indexed by
a pair of words. I.e. dict[word1,word2]
- In C, I'd just create a 2-dimensional array of integers and increment
as required, but would have to hash the words into integer indices.

- I tried the following in Python:
dict = {}
prev1 = ''
prev2 = ''
[snip]

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

I'm obviously missing something here!

Any guidance greatly appreciated!

Thank you,
Rob Lake
rbl@hal.cwru.edu
Help with dictionary, please [ In reply to ]
rbl@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'
AW: Help with dictionary, please [ In reply to ]
Hi Rob,

I tried the example and it worked for me. Maybe it's just a problem of
indentication, because the last two lines in Your example should be indentet
at the same level and you indented two more spaces in front of
'dict[key]...' ?!?

> if prev1 != '' and prev2 != '':
> key = (prev2,prev1)
> dict[key] = dict[key] + 1

Or You're mixing spaces and tabs?!? Just a guess...

-Felix
---
Dipl.-Ing. Felix von Delius Fon: 0911/4244-110
Projektleiter-IT Fax: 0911/4244-100
EBox GmbH - Agentur für neue Medien Mobil: 0172/8261220
Vordere Cramergasse 11 Email: delius@ebox.de
D-90425 Nuernberg Web: http://www.ebox.de
Help with dictionary, please [ In reply to ]
On 6 Aug 1999 rbl@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@hal.cwru.edu

Oleg.
----
Oleg Broytmann Netskate/Inter.Net.Ru phd@emerald.netskate.ru
Programmers don't die, they just GOSUB without RETURN.
Help with dictionary, please [ In reply to ]
Rob Lake writes:

> 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

The variable key should be a string, you're supplying a tupple.


//Klaus

--
···[ Magnetic Ink ]················································· ><> ···
···[ http://www.magnetic-ink.dk/download/ ]·································
Help with dictionary, please [ In reply to ]
rbl@hal.cwru.edu writes:
|
| 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
|
| I'm obviously missing something here!

What you do seems good to me. I wonder if you have trouble with white
space, since the code snippet shown above contains both space and tab
characters.

White space characters in indentation make sense in Python. To avoid
mysterious syntax error, you'd better use either space or tab
characters, and not mix them.

Regards,

--
KAJIYAMA, Tamito <kajiyama@grad.sccs.chukyo-u.ac.jp>
Help with dictionary, please [ In reply to ]
Klaus Alexander Seistrup wrote:

> The variable key should be a string, you're supplying a tupple.

Not at all. Tuples (or any immutable object) make fine dictionary
keys.

The original problem is almost undoubtedly indentation (tabs &
spaces, with editor interpretation out of sync with Python's).

- Gordon
Help with dictionary, please [ In reply to ]
Gordon McMillan wrote:

> > The variable key should be a string, you're supplying a tupple.
>
> Not at all. Tuples (or any immutable object) make fine dictionary
> keys.

Sure, I was confusing dictionaries with anydbm objects. I even tried to
cancel my article but it seems that you were too fast. ;-) Sorry about
the misinformation.

> The original problem is almost undoubtedly indentation (tabs &
> spaces, with editor interpretation out of sync with Python's).

Yes, I read your answer.


Cheers,

//Klaus

--
···[ Magnetic Ink ]················································· ><> ···
···[. Key fingerprint: 2E37 EAED F848 815B 050A FBAA 8F68 DD66 B4B3 7EC4 ]···