[Tutor] x and y
Alan Gauld
alan.gauld at btinternet.com
Sun Mar 23 01:28:38 CET 2008
"elis aeris" <hunter92383 at gmail.com> wrote
>I just need a way to key a list of tuples of 2 for referencing.
>
>> I have no idea what you mean by that, can you give a more
>> specific example?
OK, I have now read your discussion with Kent.
Can i ask, have you tried going through any of the basic tutorials?
They all cover this kind of stuff, especially dictionaries and
raw input and string concatenation etc. It will be much more
effective to work through a tutorial than to keep coming back
here with every little question. Try the Raw Materials topic of
my tutorial for starters. It gives an example of a phone book
comprised of a list stored in a dictionary keyed by name...
However as to this question, you can store any Python
object as the value of a dictionary (even another dictionary),
and you can use any *immutable* object as a key.
So if I want to store two numbers as a value against another
two numbers you can just do this:
mapping = { (1,2):(10,20), (3,4): (30,40) }
print mapping[(1,2)] #-> (10,20)
mapping[(5,6)] = (50,60) # assign new value
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list