newbie question about dictionnary ?

Ganesan R rganesan at myrealbox.com
Fri Sep 5 04:27:42 EDT 2003


>>>>> "Sophie" == Sophie Alléon <alleon at club-internet.fr> writes:

> 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 ?

The key can be any immutable object. So key can be a tuple. For example you
can do 

------------------------------
mydict = {}

mydict[((1,2),(2,3))] = ...
------------------------------

You can even use your own class as a key. So to make it more readable, you
can do something like

---------------------------------------
class Edge:
    def __init__(self, point1, point2):
        self.point1 = point1
        self.point2 = point2

p1 = (1,2)
p2 = (2,3)
e = Edge(p1, p2)
mydict[e] = ...
---------------------------------------

You get the idea :-)

Ganesan

-- 
Ganesan R 





More information about the Python-list mailing list