Rounding off Values of dicts (in a list) to 2 decimal points

Discussion in 'Python' started by tripsvt@gmail.com, Oct 2, 2013.

  1. Guest

    am trying to round off values in a dict to 2 decimal points but have been unsuccessful so far. The input I have is like this:


    y = [{'a': 80.0, 'b': 0.0786235, 'c': 10.0, 'd': 10.6742903}, {'a': 80.73246, 'b': 0.0, 'c': 10.780323, 'd': 10.0}, {'a': 80.7239, 'b': 0.7823640, 'c': 10.0, 'd': 10.0}, {'a': 80.7802313217234, 'b': 0.0, 'c': 10.0, 'd': 10.9762304}]



    I want to round off all the values to two decimal points using the ceil function. Here's what I have:


    def roundingVals_toTwoDeci():
    global y
    for d in y:
    for k, v in d.items():
    v = ceil(v*100)/100.0
    return
    roundingVals_toTwoDeci()



    But it is not working - I am still getting the old values.
     
    , Oct 2, 2013
    #1
    1. Advertising

  2. > def roundingVals_toTwoDeci():
    > global y
    > for d in y:
    > for k, v in d.items():
    > v = ceil(v*100)/100.0
    > return
    > roundingVals_toTwoDeci()
    >
    >
    >
    > But it is not working - I am still getting the old values.



    You're not assigning the rounded value back into d. After assigning to
    v try this:

    d[k] = v

    Skip
     
    Skip Montanaro, Oct 2, 2013
    #2
    1. Advertising

  3. On Wed, Oct 2, 2013 at 1:01 PM, <> wrote:
    > am trying to round off values in a dict to 2 decimal points but have been unsuccessful so far. The input I have is like this:
    >
    >
    > y = [{'a': 80.0, 'b': 0.0786235, 'c': 10.0, 'd': 10.6742903}, {'a': 80.73246, 'b': 0.0, 'c': 10.780323, 'd': 10.0}, {'a': 80.7239, 'b': 0.7823640, 'c': 10.0, 'd': 10.0}, {'a': 80.7802313217234, 'b': 0.0, 'c': 10.0, 'd': 10.9762304}]
    >
    >
    >
    > I want to round off all the values to two decimal points using the ceil function. Here's what I have:


    This is a snippet of what you have I am guessing. There is no print
    statement so you won't be able to see the results. Its best if you
    include your complete code (if its short) or an example that actually
    shows the problem..
    >
    >
    > def roundingVals_toTwoDeci():
    > global y
    > for d in y:
    > for k, v in d.items():
    > v = ceil(v*100)/100.0
    > return
    > roundingVals_toTwoDeci()
    >
    >

    That being said, you should pass y as a parameter to your function.
    Using globals is always a bad idea. That's another discussion
    entirely, but you should google why globals are a bad idea to learn
    more.
    Your code does a calculation to create a value you call v. You should
    put a print statement below that to see what v has become. Your inner
    loop rewrites v for each loop. It actually re-writes it twice i think
    -- once when it iterates, and once when it calculates. So you need to
    fix that. Also I think you need to interate using d.interitems()

    That's a start. Come back with the code you actually wrote and the
    results it showed you.
    Af


    >
    > But it is not working - I am still getting the old values.
    > --
    > https://mail.python.org/mailman/listinfo/python-list




    --
    Joel Goldstick
    http://joelgoldstick.com
     
    Joel Goldstick, Oct 2, 2013
    #3
  4. writes:

    > am trying to round off values in a dict to 2 decimal points but
    > have been unsuccessful so far. The input I have is like this:
    >
    > y = [{'a': 80.0, 'b': 0.0786235, 'c': 10.0, 'd': 10.6742903},
    > {'a': 80.73246, 'b': 0.0, 'c': 10.780323, 'd': 10.0}, {'a':
    > 80.7239, 'b': 0.7823640, 'c': 10.0, 'd': 10.0}, {'a':
    > 80.7802313217234, 'b': 0.0, 'c': 10.0, 'd': 10.9762304}]
    >
    > I want to round off all the values to two decimal points using the
    > ceil function. Here's what I have:
    >
    > def roundingVals_toTwoDeci():
    > global y
    > for d in y:
    > for k, v in d.items():
    > v = ceil(v*100)/100.0
    > return
    > roundingVals_toTwoDeci()
    >
    > But it is not working - I am still getting the old values.


    You are assigning to a local variable, v. Instead, store the new
    values back to the dict like this:

    d[k] = ceil(v*100)/100.0

    And you don't need to declare y global. It would only be needed if you
    assigned directly to it, as in y = ... (usually not a good idea).

    The rounding may not work the way you expect, because float values are
    stored in binary. You may need a decimal type, or you may need to
    format the output when printing instead.
     
    Jussi Piitulainen, Oct 2, 2013
    #4
  5. Neil Cerutti Guest

    On 2013-10-02, <> wrote:
    > am trying to round off values in a dict to 2 decimal points
    > but have been unsuccessful so far. The input I have is like
    > this:
    >
    > y = [{'a': 80.0, 'b': 0.0786235, 'c': 10.0, 'd': 10.6742903}, {'a': 80.73246, 'b': 0.0, 'c': 10.780323, 'd': 10.0}, {'a': 80.7239, 'b': 0.7823640, 'c': 10.0, 'd': 10.0}, {'a': 80.7802313217234, 'b': 0.0, 'c': 10.0, 'd': 10.9762304}]
    >
    > I want to round off all the values to two decimal points using
    > the ceil function. Here's what I have:


    I recommend using the builtin function round instead of
    math.ceil. math.ceil doesn't do what is normally thought of as
    rounding. In addition, it supports rounding to different numbers
    of decimal places.

    > def roundingVals_toTwoDeci():
    > global y


    You are hopefully* making modifications to y's object, but not rebinding y,
    so you don't need this global statement.

    > for d in y:
    > for k, v in d.items():
    > v = ceil(v*100)/100.0


    [*] You're binding v to a new float object here, but not
    modifying y. Thus, this code will have no effect on y.

    You need to assign to y[k] here instead.

    for k, v in d.items():
    y[k] = round(v, 2)

    > return


    Bare returns are not usual at the end of Python functions. Just
    let the function end; it returns None either way. Only return
    when you've got an interesting value to return, or when you need
    to end execution of the function early.

    --
    Neil Cerutti
     
    Neil Cerutti, Oct 2, 2013
    #5
  6. Guest

    On Wednesday, October 2, 2013 10:01:16 AM UTC-7, wrote:
    > am trying to round off values in a dict to 2 decimal points but have beenunsuccessful so far. The input I have is like this:
    >
    >
    >
    >
    >
    > y = [{'a': 80.0, 'b': 0.0786235, 'c': 10.0, 'd': 10.6742903}, {'a':80.73246, 'b': 0.0, 'c': 10.780323, 'd': 10.0}, {'a': 80.7239, 'b': 0.7823640, 'c': 10.0, 'd': 10.0}, {'a': 80.7802313217234, 'b': 0.0, 'c': 10.0, 'd': 10.9762304}]
    >
    >
    >
    >
    >
    >
    >
    > I want to round off all the values to two decimal points using the ceil function. Here's what I have:
    >
    >
    >
    >
    >
    > def roundingVals_toTwoDeci():
    >
    > global y
    >
    > for d in y:
    >
    > for k, v in d.items():
    >
    > v = ceil(v*100)/100.0
    >
    > return
    >
    > roundingVals_toTwoDeci()
    >
    >
    >
    >
    >
    >
    >
    > But it is not working - I am still getting the old values.

    ____________________________________

    I am not sure what's going on but here's the current scenario: I get the values with 2 decimal places as I originally required. When I do json.dumps(), it works fine. The goal is to send them to a URL and so I do a urlencode.When I decode the urlencoded string, it gives me the same goodold 2 decimal places. But, for some reason, at the URL, when I check, it no longer limits the values to 2 decimal places, but shows values like 9.10003677694312. What's going on. Here's the code that I have:

    class LessPrecise(float):
    def __repr__(self):
    return str(self)

    def roundingVals_toTwoDeci(y):
    for d in y:
    for k, v in d.iteritems():
    d[k] = LessPrecise(round(v, 2))
    return

    roundingVals_toTwoDeci(y)
    j = json.dumps(y)
    print j

    //At this point, print j gives me

    [{"a": 80.0, "b": 0.0, "c": 10.0, "d": 10.0}, {"a": 100.0, "b": 0.0, "c": 0..0, "d": 0.0}, {"a":
    80.0, "b": 0.0, "c": 10.0, "d": 10.0}, {"a": 90.0, "b": 0.0, "c": 0.0, "d":10.0}]

    //then I do,
    params = urllib.urlencode({'thekey': j})

    //I then decode params and print it and it gives me

    thekey=[{"a": 80.0, "b": 0.0, "c": 10.0, "d": 10.0}, {"a": 100.0, "b": 0.0, "c": 0.0, "d":
    0.0}, {"a": 80.0, "b": 0.0, "c": 10.0, "d": 10.0}, {"a": 90.0, "b": 0.0, "c": 0.0, "d": 10.0}]

    However, at the URL, the values show up as 90.000043278694123
     
    , Oct 3, 2013
    #6
  7. Peter Otten Guest

    wrote:

    > On Wednesday, October 2, 2013 10:01:16 AM UTC-7, wrote:
    >> am trying to round off values in a dict to 2 decimal points but have been
    >> unsuccessful so far. The input I have is like this:
    >>
    >>
    >>
    >>
    >>
    >> y = [{'a': 80.0, 'b': 0.0786235, 'c': 10.0, 'd': 10.6742903}, {'a':
    >> 80.73246, 'b': 0.0, 'c': 10.780323, 'd': 10.0}, {'a': 80.7239, 'b':
    >> 0.7823640, 'c': 10.0, 'd': 10.0}, {'a': 80.7802313217234, 'b': 0.0,
    >> 'c': 10.0, 'd': 10.9762304}]
    >>
    >>
    >>
    >>
    >>
    >>
    >>
    >> I want to round off all the values to two decimal points using the ceil
    >> function. Here's what I have:
    >>
    >>
    >>
    >>
    >>
    >> def roundingVals_toTwoDeci():
    >>
    >> global y
    >>
    >> for d in y:
    >>
    >> for k, v in d.items():
    >>
    >> v = ceil(v*100)/100.0
    >>
    >> return
    >>
    >> roundingVals_toTwoDeci()
    >>
    >>
    >>
    >>
    >>
    >>
    >>
    >> But it is not working - I am still getting the old values.

    > ____________________________________
    >
    > I am not sure what's going on but here's the current scenario: I get the
    > values with 2 decimal places as I originally required. When I do
    > json.dumps(), it works fine. The goal is to send them to a URL and so I do
    > a urlencode. When I decode the urlencoded string, it gives me the same
    > goodold 2 decimal places. But, for some reason, at the URL, when I check,
    > it no longer limits the values to 2 decimal places, but shows values like
    > 9.10003677694312. What's going on. Here's the code that I have:
    >
    > class LessPrecise(float):
    > def __repr__(self):
    > return str(self)
    >
    > def roundingVals_toTwoDeci(y):
    > for d in y:
    > for k, v in d.iteritems():
    > d[k] = LessPrecise(round(v, 2))
    > return


    That should only process the first dict in the list, due to a misplaced
    return.

    > roundingVals_toTwoDeci(y)
    > j = json.dumps(y)
    > print j
    >
    > //At this point, print j gives me
    >
    > [{"a": 80.0, "b": 0.0, "c": 10.0, "d": 10.0}, {"a": 100.0, "b": 0.0, "c":
    > [{0.0, "d": 0.0}, {"a":
    > 80.0, "b": 0.0, "c": 10.0, "d": 10.0}, {"a": 90.0, "b": 0.0, "c": 0.0,
    > "d": 10.0}]
    >
    > //then I do,
    > params = urllib.urlencode({'thekey': j})
    >
    > //I then decode params and print it and it gives me
    >
    > thekey=[{"a": 80.0, "b": 0.0, "c": 10.0, "d": 10.0}, {"a": 100.0, "b":
    > 0.0, "c": 0.0, "d": 0.0}, {"a": 80.0, "b": 0.0, "c": 10.0, "d": 10.0},
    > {"a": 90.0, "b": 0.0, "c": 0.0, "d": 10.0}]
    >
    > However, at the URL, the values show up as 90.000043278694123


    Can you give the actual code, including the decoding part? Preferably you'd
    put both encoding and decoding into one small self-contained demo script.
     
    Peter Otten, Oct 3, 2013
    #7
  8. Neil Cerutti Guest

    On 2013-10-03, <> wrote:
    > thekey=[{"a": 80.0, "b": 0.0, "c": 10.0, "d": 10.0}, {"a":
    > 100.0, "b": 0.0, "c": 0.0, "d": 0.0}, {"a": 80.0, "b": 0.0,
    > "c": 10.0, "d": 10.0}, {"a": 90.0, "b": 0.0, "c": 0.0, "d":
    > 10.0}]
    >
    > However, at the URL, the values show up as 90.000043278694123


    You'll need to convert them to strings yourself before submitting
    them, by using % formatting or str.format.

    --
    Neil Cerutti
     
    Neil Cerutti, Oct 3, 2013
    #8
  9. Guest

    On Thursday, October 3, 2013 11:03:17 AM UTC-7, Neil Cerutti wrote:
    > On 2013-10-03, <> wrote:
    >
    > > thekey=[{"a": 80.0, "b": 0.0, "c": 10.0, "d": 10.0}, {"a":

    >
    > > 100.0, "b": 0.0, "c": 0.0, "d": 0.0}, {"a": 80.0, "b": 0.0,

    >
    > > "c": 10.0, "d": 10.0}, {"a": 90.0, "b": 0.0, "c": 0.0, "d":

    >
    > > 10.0}]

    >
    > >

    >
    > > However, at the URL, the values show up as 90.000043278694123

    >
    >
    >
    > You'll need to convert them to strings yourself before submitting
    >
    > them, by using % formatting or str.format.
    >
    >
    >
    > --
    >
    > Neil Cerutti


    I thought the class 'LessPrecise' converts them to strings. But even when Itry doing it directly without the class at all, as in str(round(v, 2)), itgives all the expected values (as in {"a": "10.1", "b": "3.4", etc.}) but at the URL, it gives all the decimal places - 10.78324783923783
     
    , Oct 3, 2013
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ChrisMM
    Replies:
    8
    Views:
    41,948
    Dale King
    Apr 15, 2006
  2. =?Utf-8?B?U3Vl?=
    Replies:
    2
    Views:
    1,189
    =?Utf-8?B?U3Vl?=
    Jun 26, 2006
  3. Yuan HOng

    rounding of a decimal object

    Yuan HOng, Jul 18, 2006, in forum: Python
    Replies:
    0
    Views:
    335
    Yuan HOng
    Jul 18, 2006
  4. jimgardener

    rounding off double values

    jimgardener, Aug 13, 2008, in forum: Java
    Replies:
    7
    Views:
    454
    Arne Vajhøj
    Aug 14, 2008
  5. bruce
    Replies:
    0
    Views:
    273
    bruce
    Jan 10, 2012
Loading...

Share This Page