Rounding off to 2 decimal points in a list of dict

This is the place for queries that don't fit in any of the other categories.

Rounding off to 2 decimal points in a list of dict

Postby askance » Wed Oct 02, 2013 4:59 pm

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

Code: Select all
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:

Code: Select all
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.

Here's the expanded code ---

Code: Select all
cpuStats = psutil.cpu_times_percent(percpu=True)

x = [Stats._asdict() for Stats in cpuStats]
y = [dict(stat) for stat in x]
#print y


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

cpuVals = json.dumps(x)
print cpuVals


Now when I do the print statement inside the function, I get the rounded off values, but the print statement after "json.dumps" returns the old values again.

Here's the output:

Code: Select all
0.0
0.0
18.2
0.0
27.3
54.5
0.0
0.0
0.0
[{"softirq": 0.0, "idle": 0.0, "user": 27.300000000000001, "irq": 0.0, "iowait": 54.5, "steal": 0.0, "system": 18.199999999999999, "guest": 0.0, "nice": 0.0}]

Last edited by askance on Wed Oct 02, 2013 5:30 pm, edited 1 time in total.
askance
 
Posts: 5
Joined: Fri Sep 20, 2013 10:25 pm

Re: Rounding off to 2 decimal points in a list of dict

Postby stranac » Wed Oct 02, 2013 5:18 pm

  1. Don't use globals like that. It's ugly and unnecessary.
  2. You're not actually working with the value from the dict here. dict.items() returns a copy of the key-value pairs, which are then assigned to local variables.
    One way you could make this work is:
    Code: Select all
    def ceil_to_two_decimals(some_dict):
        for k in some_dict:
            some_dict[k] = ceil(some_dict[k] * 100) / 100.0

    Another, more pythonic way would be using a dict comprehension, but this creates a new dict rather than modifying the old one:
    Code: Select all
    def ceil_to_two_decimals(some_dict):
       return {k: ceil(v * 100) / 100.0 for k, v in some_dict.items()}
  3. It's not exactly rounding if you use math.ceil()
In case you're using python 2, you might want to use .iteritems() instead of .items()
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: Rounding off to 2 decimal points in a list of dict

Postby askance » Wed Oct 02, 2013 6:27 pm

Thx everyone. But I am getting stuck in looping through all the dicts in the list. What am I doing wrong here?

Code: Select all
x = [Stats._asdict() for Stats in cpuStats]
y = [dict(stat) for stat in x]
#print y
for d in y:
    for k, v in d.items():
        v = ceil(v*100)/100.0
        print v
        d[k] = v
print y
cpuVals = json.dumps(x)
print cpuVals


Code: Select all
0.0
0.0
9.1
90.9
0.0
0.0
0.0
0.0
0.0
[{'softirq': 0.0, 'irq': 0.0, 'system': 9.0999999999999996, 'idle': 90.900000000000006, 'user': 0.0, 'iowait': 0.0, 'nice': 0.0, 'steal': 0.0, 'guest': 0.0}]
[{"softirq": 0.0, "idle": 90.900000000000006, "user": 0.0, "irq": 0.0, "iowait": 0.0, "steal": 0.0, "system": 9.0999999999999996, "guest": 0.0, "nice": 0.0}]


Why is it printing fine when inside the for-loop and not when I print y again below the loop?
askance
 
Posts: 5
Joined: Fri Sep 20, 2013 10:25 pm

Re: Rounding off to 2 decimal points in a list of dict

Postby stranac » Thu Oct 03, 2013 7:39 am

Looks fine to me.
There are just some imprecisions in how floating point numbers work.
If you really need precise results, you should use the decimal modu.e.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests