Data type sequence error:Dictionary

Derek Thomson derek.thomson at gmail.com
Wed Jul 28 04:01:53 EDT 2004


On Wed, 28 Jul 2004 15:56:00 +0800, BlackWhite <live1024 at hotmail.com> wrote:
> Source code:
> d={"x":42,"y":3.14,"z":9}
> d.keys()
> ['y','x','z']                 //Why y in font  of x
> d.values()
> [3.1400..01,42,9]            //Why 3.140000000000000001 should not have 1 in
> the end
> 
> Why this happened?
>

Because the items in a dictionary are not guaranteed to be kept in the
order you put them in there. If you want to preserve ordering, use a
list or a tuple. Here's an example that uses tuples:

>>> items = ( ("x", 42), ("y", 3.14), ("z", 9) )
>>> items
(('x', 42), ('y', 3.1400000000000001), ('z', 9))



More information about the Python-list mailing list