Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #55361 > unrolled thread

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

Started bytripsvt@gmail.com
First post2013-10-02 10:01 -0700
Last post2013-10-03 11:17 -0700
Articles 9 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  Rounding off Values of dicts (in a list) to 2 decimal points tripsvt@gmail.com - 2013-10-02 10:01 -0700
    Re: Rounding off Values of dicts (in a list) to 2 decimal points Skip Montanaro <skip@pobox.com> - 2013-10-02 12:13 -0500
    Re: Rounding off Values of dicts (in a list) to 2 decimal points Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-10-02 20:22 +0300
    Re: Rounding off Values of dicts (in a list) to 2 decimal points Joel Goldstick <joel.goldstick@gmail.com> - 2013-10-02 13:19 -0400
    Re: Rounding off Values of dicts (in a list) to 2 decimal points Neil Cerutti <neilc@norwich.edu> - 2013-10-02 17:32 +0000
    Re: Rounding off Values of dicts (in a list) to 2 decimal points tripsvt@gmail.com - 2013-10-03 10:06 -0700
      Re: Rounding off Values of dicts (in a list) to 2 decimal points Peter Otten <__peter__@web.de> - 2013-10-03 19:41 +0200
      Re: Rounding off Values of dicts (in a list) to 2 decimal points Neil Cerutti <neilc@norwich.edu> - 2013-10-03 18:03 +0000
        Re: Rounding off Values of dicts (in a list) to 2 decimal points tripsvt@gmail.com - 2013-10-03 11:17 -0700

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

Fromtripsvt@gmail.com
Date2013-10-02 10:01 -0700
SubjectRounding off Values of dicts (in a list) to 2 decimal points
Message-ID<0aa3c507-a570-4d39-823d-106ba63b0b70@googlegroups.com>
 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.

[toc] | [next] | [standalone]


#55363

FromSkip Montanaro <skip@pobox.com>
Date2013-10-02 12:13 -0500
Message-ID<mailman.641.1380733997.18130.python-list@python.org>
In reply to#55361
>     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

[toc] | [prev] | [next] | [standalone]


#55365

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2013-10-02 20:22 +0300
Message-ID<qotpprnio5g.fsf@ruuvi.it.helsinki.fi>
In reply to#55361
tripsvt@gmail.com 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.

[toc] | [prev] | [next] | [standalone]


#55366

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2013-10-02 13:19 -0400
Message-ID<mailman.643.1380734819.18130.python-list@python.org>
In reply to#55361
On Wed, Oct 2, 2013 at 1:01 PM,  <tripsvt@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#55367

FromNeil Cerutti <neilc@norwich.edu>
Date2013-10-02 17:32 +0000
Message-ID<bb33kuFq0ljU1@mid.individual.net>
In reply to#55361
On 2013-10-02, tripsvt@gmail.com <tripsvt@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#55425

Fromtripsvt@gmail.com
Date2013-10-03 10:06 -0700
Message-ID<23564349-7dd1-4d7a-9bfb-8483ddf3335e@googlegroups.com>
In reply to#55361
On Wednesday, October 2, 2013 10:01:16 AM UTC-7, tri...@gmail.com 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
            
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

[toc] | [prev] | [next] | [standalone]


#55427

FromPeter Otten <__peter__@web.de>
Date2013-10-03 19:41 +0200
Message-ID<mailman.686.1380822013.18130.python-list@python.org>
In reply to#55425
tripsvt@gmail.com wrote:

> On Wednesday, October 2, 2013 10:01:16 AM UTC-7, tri...@gmail.com 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.

[toc] | [prev] | [next] | [standalone]


#55432

FromNeil Cerutti <neilc@norwich.edu>
Date2013-10-03 18:03 +0000
Message-ID<bb5pr5Fd36uU1@mid.individual.net>
In reply to#55425
On 2013-10-03, tripsvt@gmail.com <tripsvt@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#55434

Fromtripsvt@gmail.com
Date2013-10-03 11:17 -0700
Message-ID<58286399-5eb3-40f8-9d28-e6036451fbf9@googlegroups.com>
In reply to#55432
On Thursday, October 3, 2013 11:03:17 AM UTC-7, Neil Cerutti wrote:
> On 2013-10-03, tripsvt@gmail.com <tripsvt@gmail.com> 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 I try doing it directly without the class at all, as in str(round(v, 2)), it gives 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web