newbie question about dictionnary ?

Michael Geary Mike at DeleteThis.Geary.com
Fri Sep 5 04:15:05 EDT 2003


Sophie Alléon wrote:
>...
> have a triangle class  (made of 3 edges)
> have an edge   class   (made of 2 points and embeding a triangle
> list (those connected to the edge)
>
> for each triangle construct its edges but I have to check if the
> edge already exists
>
> 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 ?

Sophie, dictionary keys can be just about any immutable value. They can be
tuples, if the tuples contain only strings, numbers, or other tuples:

>>> d = { (1,2): 'one two', (3,4): 'three four' }
>>> d
{(1, 2): 'one two', (3, 4): 'three four'}
>>> d[(1,2)]
'one two'
>>> d[(3,4)]
'three four'
>>>

For more information, see the Dictionaries section in the Python tutorial:

http://www.python.org/doc/current/tut/

-Mike






More information about the Python-list mailing list