将dict值舍入为2位小数 [英] Round off dict values to 2 decimals
问题描述
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}
我需要将值舍入到只有2位的小数位。
当我尝试以下内容:
def roundingVals_toTwoDeci(y) :
pre>
for y in y:
for k,v in d.items():
v = ceil(v * 100)/100.0
print v
d [k] = v
return
roundingVals_toTwoDeci(y)
s = json.dumps(y)
打印s
我得到:
0.0
0.0
18.2
0.0
27.3
54.5
0.0
0.0
0.0
[{a:0.0,b 0.0,c:27.300000000000001,d:0.0,e:54.5,f:0.0,g:18.199999999999999,h:0.0,i:0.0}]
我需要使用2.4+以上的版本,所以我不会使用dict的理解。
首先,我很难循环遍历所有关键字,所有的原始数据中的值。
第二,当它在函数内部打印时,这个结果只有1位小数点而不是2?
第三,为什么'json.dumps'然后'print'不显示函数内的值?
编辑:
使用@Mark Ransom的答案在下面,我得到所需的o / p。但是,我必须url编码
json.dumps
值并将其发送到URL。在URL处,它会将值解码为所有小数位。所以,例如,如果josn.dumps
给了{a:9.1},则该URL将其显示为(urlencode)之后为9.10034254344365
。修改后的代码如下:
class LessPrecise(float):
def __repr __(self):
return str(self)
def roundingVals_toTwoDeci(y):
for y in:
for k,v in d.items():
v = LessPrecise (圆(v,2))
打印v
d [k] = v
roundingVals_toTwoDeci(y)
j = json.dumps(y)
print j
params = urllib.urlencode({'thekey':j})
print json.dumps
给出{a:9.1}
在urlencode
之后的URL,它给出9.1078667322034
而不是9.1
如下:
输出:::
100.0
0.0
0.0
0.0
100.0
0.0
0.0
0.0
81.8
0.0
18.2
0.0
90.0
0.0
0.0
10.0
[{a:100.0,b:0.0, c:0.0,d:0.0},{a:100.0,b:0.0,c:0.0,d :0.0},{a:
81.8,b:0.0,c:18.2,d:0.0},{a:90.0,b:0.0,c :0.0,d:10.0}]
在URL:
9.100000381469727
JSON string json.dumps()
[{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 ,c:10.0,d:10.0},{a:90.0,b:0.0,c:0.0,d:10.0}]
urlencode字符串 - 解码后,在 http://meyerweb.com/eric/tools/dencoder/
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 :10.0}]
在URL处,我得到如$ code> 18.200000762939453 (这个值来自一个ter脚本运行)
解决方案从几个其他答案中获取最佳位数:
class LessPrecise(float):
def __repr __(self):
return str(self)
def roundingVals_toTwoDeci y):
for y in:
for k,v in d.items():
v = LessPrecise(round(v,2))
print v
d [k] = v
>>> roundingVals_toTwoDeci(y)
80.0
10.0
0.08
10.67
80.73
10.78
0.0
10.0
80.72
10.0
0.78
10.0
80.78
10.0
0.0
10.98
>>> s = json.dumps(y)
>>> s
'[{a:80.0,c:10.0,b:0.08,d:10.67},{a:80.73,c:10.78,b 0.0,d:10.0},{a:80.72,c:10.0,b:0.78,d:10.0},{a:80.78,c :0.0,d:10.98}]'
I'm having a hard time rounding off values in dicts. What I have is a list of dicts 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 need to round off the values to just 2 decimal places.
When I try the following:
def roundingVals_toTwoDeci(y): 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) s = json.dumps(y) print s
I get:
0.0 0.0 18.2 0.0 27.3 54.5 0.0 0.0 0.0 [{"a": 0.0, "b": 0.0, "c": 27.300000000000001, "d": 0.0, "e": 54.5, "f": 0.0, "g": 18.199999999999999, "h": 0.0, "i": 0.0}]
I need to make this work with versions 2.4+ and so am not using dict comprehensions. First, I am having a hard time looping through all the key, values in all the dicts in the original. Second, this result has just 1 decimal point instead of 2 when it prints inside the function? Third, why is the 'json.dumps' and then 'print' not showing the values from inside the function?
EDIT:
Working with @Mark Ransom's answer below, I get the desired o/p. However, I have to urlencode the
json.dumps
value and send it to a URL. At the URL, it decodes the values into all the decimal places. So, for example, if,josn.dumps
gives {"a": 9.1}, the URL shows it (after urlencode) as9.10034254344365
. The modified code is as below:class LessPrecise(float): def __repr__(self): return str(self) def roundingVals_toTwoDeci(y): for d in y: for k, v in d.items(): v = LessPrecise(round(v, 2)) print v d[k] = v roundingVals_toTwoDeci(y) j = json.dumps(y) print j params = urllib.urlencode({'thekey': j})
print json.dumps
gives{"a": 9.1}
At the URL afterurlencode
, it gives9.1078667322034
instead of9.1
as in the following:Output:::
100.0 0.0 0.0 0.0 100.0 0.0 0.0 0.0 81.8 0.0 18.2 0.0 90.0 0.0 0.0 10.0 [{"a": 100.0, "b": 0.0, "c": 0.0, "d": 0.0}, {"a": 100.0, "b": 0.0, "c": 0.0, "d": 0.0}, {"a": 81.8, "b": 0.0, "c": 18.2, "d": 0.0}, {"a": 90.0, "b": 0.0, "c": 0.0, "d": 10.0}]
At the URL:
9.100000381469727
The JSON string after json.dumps()
[{"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}]
The urlencode string - after decoding at http://meyerweb.com/eric/tools/dencoder/
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}]
At the URL, I get values like
18.200000762939453
(this value is from a later script run)解决方案Taking the best bits from a couple of other answers:
class LessPrecise(float): def __repr__(self): return str(self) def roundingVals_toTwoDeci(y): for d in y: for k, v in d.items(): v = LessPrecise(round(v, 2)) print v d[k] = v >>> roundingVals_toTwoDeci(y) 80.0 10.0 0.08 10.67 80.73 10.78 0.0 10.0 80.72 10.0 0.78 10.0 80.78 10.0 0.0 10.98 >>> s=json.dumps(y) >>> s '[{"a": 80.0, "c": 10.0, "b": 0.08, "d": 10.67}, {"a": 80.73, "c": 10.78, "b": 0.0, "d": 10.0}, {"a": 80.72, "c": 10.0, "b": 0.78, "d": 10.0}, {"a": 80.78, "c": 10.0, "b": 0.0, "d": 10.98}]'
这篇关于将dict值舍入为2位小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!