round numbers in an array without importing Numeric or Math?

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

    round numbers in an array without importing Numeric or Math?

    Is there an easy way to round numbers in an array?

    I have
    Test = [1.1,2.2,3.7]

    and want to round so the values are

    print Test [1,2,4]


    Lance
  • Alexander Schmolck

    #2
    Re: round numbers in an array without importing Numeric or Math?

    Lance Hoffmeyer <lance@augustma il.com> writes:
    [color=blue]
    > Is there an easy way to round numbers in an array?
    >
    > I have
    > Test = [1.1,2.2,3.7]
    >
    > and want to round so the values are
    >
    > print Test [1,2,4][/color]

    [int(x+0.5) for x in Test]

    'as

    Comment

    • Lance Hoffmeyer

      #3
      Re: round numbers in an array without importing Numeric or Math?

      May have a complicating issue with the array? Have
      the numbers have been interpreted as strings? I have
      been pulling them from a Word doc using regex's

      print Test
      [u'9.0', u'58.6', u'97.8', u'10.0', u'9.6', u'28.1']


      Lance


      Alexander Schmolck wrote:[color=blue]
      > Lance Hoffmeyer <lance@augustma il.com> writes:
      >[color=green]
      >> Is there an easy way to round numbers in an array?
      >>
      >> I have
      >> Test = [1.1,2.2,3.7]
      >>
      >> and want to round so the values are
      >>
      >> print Test [1,2,4][/color]
      >
      > [int(x+0.5) for x in Test]
      >
      > 'as[/color]

      Comment

      • Dan Sommers

        #4
        Re: round numbers in an array without importing Numeric or Math?

        On Tue, 16 May 2006 13:41:37 -0500,
        Lance Hoffmeyer <lance@augustma il.com> wrote:
        [color=blue]
        > May have a complicating issue with the array? Have
        > the numbers have been interpreted as strings? I have
        > been pulling them from a Word doc using regex's[/color]
        [color=blue]
        > print Test
        > [u'9.0', u'58.6', u'97.8', u'10.0', u'9.6', u'28.1'][/color]

        Then you'll have to convert your strings to floats first:

        [int(float(x)+0. 5) for x in Test]

        HTH,
        Dan

        --
        Dan Sommers
        <http://www.tombstoneze ro.net/dan/>
        "I wish people would die in alphabetical order." -- My wife, the genealogist

        Comment

        • skip@pobox.com

          #5
          Re: round numbers in an array without importing Numeric or Math?


          Lance> May have a complicating issue with the array? Have the numbers
          Lance> have been interpreted as strings? I have been pulling them from
          Lance> a Word doc using regex's

          [int(float(x)+0. 5) for x in Test]

          S

          Comment

          • Lance Hoffmeyer

            #6
            Re: round numbers in an array without importing Numeric or Math?- SOLVED, sort of

            The array comes out as unicode. This is probably because I am grabbing the numbers
            from a Word Doc using regex's.

            So, before rounding I perform the following:
            # Convert to String
            Topamax = [str(x) for x in Topamax]
            # Convert to floating
            Topamax = [float(x) for x in Topamax]
            # Finally, round the number
            Topamax= [(x+0.5) for x in Topamax]

            Is there a shorter way?

            Lance



            Lance Hoffmeyer wrote:[color=blue]
            > Is there an easy way to round numbers in an array?
            >
            > I have
            > Test = [1.1,2.2,3.7]
            >
            > and want to round so the values are
            >
            > print Test [1,2,4]
            >
            >
            > Lance[/color]

            Comment

            • Paul McGuire

              #7
              Re: round numbers in an array without importing Numeric or Math? - SOLVED, sort of

              "Lance Hoffmeyer" <lance@augustma il.com> wrote in message
              news:446a2032$0 $61167$ae4e5890 @news.nationwid e.net...[color=blue]
              > The array comes out as unicode. This is probably because I am grabbing[/color]
              the numbers[color=blue]
              > from a Word Doc using regex's.
              >
              > So, before rounding I perform the following:
              > # Convert to String
              > Topamax = [str(x) for x in Topamax]
              > # Convert to floating
              > Topamax = [float(x) for x in Topamax]
              > # Finally, round the number
              > Topamax= [(x+0.5) for x in Topamax]
              >
              > Is there a shorter way?
              >
              > Lance
              >
              >
              >
              > Lance Hoffmeyer wrote:[color=green]
              > > Is there an easy way to round numbers in an array?
              > >
              > > I have
              > > Test = [1.1,2.2,3.7]
              > >
              > > and want to round so the values are
              > >
              > > print Test [1,2,4]
              > >
              > >
              > > Lance[/color][/color]

              (c.l.py people don't cotton to top-posting - when in Rome...)

              # Convert to String, convert to floating, and finally, round the number all
              in one swell foop
              Topamax= [int(float(str(x ))+0.5) for x in Topamax]

              -- Paul


              Comment

              • Paul McGuire

                #8
                Re: round numbers in an array without importing Numeric or Math? - SOLVED, sort of

                "Lance Hoffmeyer" <lance@augustma il.com> wrote in message
                news:446a2032$0 $61167$ae4e5890 @news.nationwid e.net...[color=blue]
                > The array comes out as unicode. This is probably because I am grabbing[/color]
                the numbers[color=blue]
                > from a Word Doc using regex's.
                >
                > So, before rounding I perform the following:
                > # Convert to String
                > Topamax = [str(x) for x in Topamax]
                > # Convert to floating
                > Topamax = [float(x) for x in Topamax]
                > # Finally, round the number
                > Topamax= [(x+0.5) for x in Topamax]
                >
                > Is there a shorter way?
                >
                > Lance
                >
                >[/color]
                .... or if you prefer the functional approach (using map)...

                roundToInt = lambda z : int(z+0.5)
                Topamax = map( roundToInt, map( float, map(str, Topamax) ) )

                (Python also has a built-in round() function, but this returns floats, not
                ints - if that is okay, then just delete the lambda definition, and replace
                roundToInt with round.)

                -- Paul


                Comment

                • Scott David Daniels

                  #9
                  Re: round numbers in an array without importing Numeric or Math?- SOLVED, sort of

                  Paul McGuire wrote:[color=blue]
                  > ... or if you prefer the functional approach (using map)...
                  >
                  > roundToInt = lambda z : int(z+0.5)
                  > Topamax = map( roundToInt, map( float, map(str, Topamax) ) )[/color]

                  Somehow, the list comprehension looks simpler and clearer to me:

                  Topamax = [int(float(uni) + .5) for uni in Topamax]

                  I really dislike lines like:
                  roundToInt = lambda z : int(z+0.5)

                  You've already chosen a name, but you'd rather write in the lisp style.

                  def roundToInt(z):r eturn int(z+0.5)

                  takes all of _one_ more character.

                  I also don't understand why you convert to string from unicode in:

                  ... map(str, Topamax) ) )

                  float works on all string types (including unicode), and will accept
                  some non-ASCII digit values.

                  --
                  -Scott David Daniels
                  scott.daniels@a cm.org

                  Comment

                  • Bernhard Herzog

                    #10
                    Re: round numbers in an array without importing Numeric or Math? -SOLVED, sort of

                    "Paul McGuire" <ptmcg@austin.r r._bogus_.com> writes:
                    [color=blue]
                    > ... or if you prefer the functional approach (using map)...
                    >
                    > roundToInt = lambda z : int(z+0.5)
                    > Topamax = map( roundToInt, map( float, map(str, Topamax) ) )
                    >
                    > (Python also has a built-in round() function, but this returns floats, not
                    > ints - if that is okay, then just delete the lambda definition, and replace
                    > roundToInt with round.)[/color]

                    Your roundToInt behaves differently from round for negative numbers:
                    [color=blue][color=green][color=darkred]
                    >>> roundToInt(-0.6)[/color][/color][/color]
                    0[color=blue][color=green][color=darkred]
                    >>> int(round(-0.6))[/color][/color][/color]
                    -1

                    Bernhard

                    --
                    Intevation GmbH http://intevation.de/
                    Skencil http://skencil.org/
                    Thuban http://thuban.intevation.org/

                    Comment

                    Working...