Data type sequence error:Dictionary

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BlackWhite

    Data type sequence error:Dictionary

    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.1400000000000 00001 should not have 1 in
    the end

    Why this happened?


  • Derek Thomson

    #2
    Re: Data type sequence error:Dictionar y

    On Wed, 28 Jul 2004 15:56:00 +0800, BlackWhite <live1024@hotma il.com> wrote:[color=blue]
    > 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.1400000000000 00001 should not have 1 in
    > the end
    >
    > Why this happened?
    >[/color]

    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:
    [color=blue][color=green][color=darkred]
    >>> items = ( ("x", 42), ("y", 3.14), ("z", 9) )
    >>> items[/color][/color][/color]
    (('x', 42), ('y', 3.1400000000000 001), ('z', 9))

    Comment

    • Brian van den Broek

      #3
      Re: Data type sequence error:Dictionar y

      Derek Thomson said unto the world upon 28/07/2004 04:01:[color=blue]
      > On Wed, 28 Jul 2004 15:56:00 +0800, BlackWhite <live1024@hotma il.com> wrote:
      >[color=green]
      >>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.1400000000000 00001 should not have 1 in
      >>the end
      >>
      >>Why this happened?
      >>[/color]
      >
      >
      > 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:
      >
      >[color=green][color=darkred]
      >>>>items = ( ("x", 42), ("y", 3.14), ("z", 9) )
      >>>>items[/color][/color]
      >
      > (('x', 42), ('y', 3.1400000000000 001), ('z', 9))[/color]

      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

      Comment

      • Derek Thomson

        #4
        Re: Data type sequence error:Dictionar y

        On Wed, 28 Jul 2004 15:56:00 +0800, BlackWhite <live1024@hotma il.com> wrote:[color=blue]
        > [3.1400..01,42,9] //Why 3.1400000000000 00001 should not have 1 in
        > the end
        >
        > Why this happened?[/color]

        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:


        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.

        Comment

        • Brian van den Broek

          #5
          Re: Data type sequence error:Dictionar y

          Derek Thomson said unto the world upon 28/07/2004 04:27:
          [color=blue]
          > On Wed, 28 Jul 2004 15:56:00 +0800, BlackWhite <live1024@hotma il.com> wrote:
          >[color=green]
          >>[3.1400..01,42,9] //Why 3.1400000000000 00001 should not have 1 in
          >>the end
          >>
          >>Why this happened?[/color]
          >
          >
          > 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.[/color]

          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

          Comment

          Working...