[Tutor] Drifting Values in urllib.
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Sun May 7 03:02:10 CEST 2006
On Sun, 7 May 2006, Alan Gauld wrote:
>> Am I missing something fundamental here? I am not used to dictionaries
>> swirling around like this. :-)
>
> Caveat: I didn't read this through thoroughly but...
>
> Dictionaries are not in any specific order, you cannot rely on the order
> remaining constant. If you are relying on the order you may well get
> bitten, you need to extract the data in the order you need it
The appropriate data structure in this case might then be a list of
key-value pairs. For example, rather than:
{ 'a' : 'alpha',
'b' : 'beta',
'e' : 'eta' }
we can encode the same key-to-value pairing with a list of 2-tuples:
[('a', 'alpha'),
('b', 'beta'),
('c', 'eta')]
which contains the same information content, but optimized for sequential
ordering rather than random-access lookup. This list will maintain the
sequence of the items, and we'll still be able to do key-value lookups if
we write a simple lookup() function.
More information about the Tutor
mailing list