Data type sequence error:Dictionary

Brian van den Broek bvande at po-box.mcgill.ca
Wed Jul 28 10:26:58 CEST 2004


Derek Thomson said unto the world upon 28/07/2004 04:01:
> 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))

Hi,

for the second question (about the trailing ...01) see 
<http://docs.python.org/tut/node15.html>, particularly the subsection on 
Representation Error.

Best,

Brian vdB




More information about the Python-list mailing list