Hello,

I learn tuples and I came acros the folloving explanation which I don't understand practicly because the scipt is not complet..., I think tha they want to show the direction but for me would be better complet example...
so it's from document: Think Python (version 1.1.19) and text is (in green):

It is common to use tuples as keys in dictionaries (primarily because you can’t use lists). For example,
a telephone directory might map from last-name, first-name pairs to telephone numbers.
Assuming that we have defined last, first and number, we could write:
directory[last,first] = number
The expression in brackets is a tuple. We could use tuple assignment to traverse this dictionary.
for last, first in directory:
print first, last, directory[last,first]
This loop traverses the keys in directory, which are tuples. It assigns the elements of each tuple
to last and first, then prints the name and corresponding telephone number.
There are two ways to represent tuples in a state diagram. The more detailed version shows the
indices and elements just as they appear in a list. For example, the tuple ('Cleese', 'John')


here is a link for that doc

http://en.wikibooks.org/wiki/Think_Python/Tuples

thank you

You should go to Thinkpython.com to get the latest version.

Anycase. This is what it's referring to.

>>> x, y = 10, 2
>>> x
10
>>> y
2
>>> d = {("John", "Smith"):"432-594-3020"}
>>> for first, last in d:
...     print first, last, d[first,last]
... 
John Smith 432-594-3020
>>>

Many thanks, it's much better now for understanding.

but what do they mean with this: directory[last,first] = number

but what do they mean with this: directory[last,first] = number

Test it out

IDLE 2.6.5      
>>> directory = {}  #Empty  dictionary
>>> last = 'hansen'
>>> first = 'tom'
>>> number = 22558899
>>> directory[last,first] = number
>>> directory
{('hansen', 'tom'): 22558899}
>>> 
>>> dir(directory)
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
>>> #Here you see methods under dictionary
>>> directory.keys()
[('hansen', 'tom')]
>>> #So first last is a tuple key in the dictionary
>>> directory.values()
[22558899]

thank you, it's very good example! now I understand.

Vlady

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.