Data type sequence error:Dictionary

B

BlackWhite

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?
 
D

Derek Thomson

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:
(('x', 42), ('y', 3.1400000000000001), ('z', 9))
 
B

Brian van den Broek

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


(('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
 
D

Derek Thomson

[3.1400..01,42,9] //Why 3.140000000000000001 should not have 1 in
the end

Why this happened?

I just realized you were asking two questions there ...

The reason 3.14 didn't get returned as exactly 3.14 is that, in
Python, real numbers are represented in "floating point." This is a
particular scheme for storing numbers in computers that, while
allowing the representation of very large and very small real numbers
and at the same time staying within a strict size, sacrifices the
ability to exactly represent all possible numbers. 3.14 is one of
those numbers.

Wikipedia has an article on floating point numbers here:
http://en.wikipedia.org/wiki/Floating_point

If you can't live with this restriction, you may have to use some
other representation. I'm not sure which (if any) are available with
Python, so someone else will have to weigh in there.

dt.
 
B

Brian van den Broek

Derek Thomson said unto the world upon 28/07/2004 04:27:
[3.1400..01,42,9] //Why 3.140000000000000001 should not have 1 in
the end

Why this happened?


I just realized you were asking two questions there ...

The reason 3.14 didn't get returned as exactly 3.14 is that, in
Python, real numbers are represented in "floating point." This is a
particular scheme for storing numbers in computers that, while
allowing the representation of very large and very small real numbers
and at the same time staying within a strict size, sacrifices the
ability to exactly represent all possible numbers. 3.14 is one of
those numbers.

Wikipedia has an article on floating point numbers here:
http://en.wikipedia.org/wiki/Floating_point

If you can't live with this restriction, you may have to use some
other representation. I'm not sure which (if any) are available with
Python, so someone else will have to weigh in there.

dt.

Hi again,

I've not tried 2.4 alpha, but there's a relevant section in Kuchling's
What's New in Python 2.4 -- 5 PEP 327: Decimal Data Type
<http://www.python.org/dev/doc/devel/whatsnew/node6.html>

Best,

Brian vdB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top