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

T

tripsvt

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.
 
S

Skip Montanaro

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
 
J

Joel Goldstick

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
 
J

Jussi Piitulainen

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.
 
N

Neil Cerutti

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)

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.
 
T

tripsvt

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
 
P

Peter Otten

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.
 
N

Neil Cerutti

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.
 
T

tripsvt

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":

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.

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top