newbie question about dictionnary ?

Andrew Eland google at andreweland.org
Fri Sep 5 07:20:59 EDT 2003


"Sophie Alléon" <alleon at club-internet.fr> wrote in message news:<3f582f90$0$20952$7a628cd7 at news.club-internet.fr>...

> for doing this I thought the dictionary was excellent but the key is a
> string while I want it
> to be the 2 points forming the edge ? How to do it ?

Unlike perl, the keys used for a Python dictionary can be arbitary
objects. You're not forced to use strings. If you represent edges as a
tuple of the form (startPoint,endPoint), you can do the following:

>>> start=(1,1)
>>> end=(2,2)
>>> edges={}
>>> edges[(start,end)]=1
>>> if (start,end) in edges:
...     print "Edge exists"
...
Edge exists

If you're using Python 2.3, you can use the new sets module, which
will give you an object repesenting a set, which is a little neater
than just using a dictionary.

 -- Andrew (http://www.andreweland.org)




More information about the Python-list mailing list