Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Rounding off Values of dicts (in a list) to 2 decimal points

Reply
Thread Tools

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

 
 
tripsvt@gmail.com
Guest
Posts: n/a
 
      10-02-2013
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.
 
Reply With Quote
 
 
 
 
Skip Montanaro
Guest
Posts: n/a
 
      10-02-2013
> 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
 
Reply With Quote
 
 
 
 
Joel Goldstick
Guest
Posts: n/a
 
      10-02-2013
On Wed, Oct 2, 2013 at 1:01 PM, <(E-Mail Removed)> 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
 
Reply With Quote
 
Jussi Piitulainen
Guest
Posts: n/a
 
      10-02-2013
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
Neil Cerutti
Guest
Posts: n/a
 
      10-02-2013
On 2013-10-02, (E-Mail Removed) <(E-Mail Removed)> 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
 
Reply With Quote
 
tripsvt@gmail.com
Guest
Posts: n/a
 
      10-03-2013
On Wednesday, October 2, 2013 10:01:16 AM UTC-7, (E-Mail Removed) 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

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      10-03-2013
(E-Mail Removed) wrote:

> On Wednesday, October 2, 2013 10:01:16 AM UTC-7, (E-Mail Removed) 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.

 
Reply With Quote
 
Neil Cerutti
Guest
Posts: n/a
 
      10-03-2013
On 2013-10-03, (E-Mail Removed) <(E-Mail Removed)> 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
 
Reply With Quote
 
tripsvt@gmail.com
Guest
Posts: n/a
 
      10-03-2013
On Thursday, October 3, 2013 11:03:17 AM UTC-7, Neil Cerutti wrote:
> On 2013-10-03, (E-Mail Removed) <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
generating unique set of dicts from a list of dicts bruce Python 0 01-10-2012 08:24 PM
rounding off double values jimgardener Java 7 08-14-2008 02:44 AM
rounding of a decimal object Yuan HOng Python 0 07-18-2006 02:37 PM
DataGridItemEventArgs dataitem decimal value rounding =?Utf-8?B?U3Vl?= ASP .Net 2 06-26-2006 03:28 PM
Java rounding with big Decimal. ChrisMM Java 8 04-15-2006 04:10 AM



Advertisments