python - 将 dict 值四舍五入为 2 位小数

标签 python dictionary rounding

我很难对字典中的值进行四舍五入。我拥有的是这样的字典列表:

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}]

我需要将值四舍五入到小数点后两位。

当我尝试以下操作时:

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

我得到:

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+ 版本上使用它,所以我没有使用字典理解。 首先,我很难遍历原始文件中所有字典中的所有键值。 其次,这个结果在函数内部打印时只有 1 个小数点而不是 2 个小数点? 第三,为什么“json.dumps”和“print”不显示函数内部的值?

编辑:

使用下面@Mark Ransom 的回答,我得到了想要的 o/p。但是,我必须对 json.dumps 值进行 urlencode 并将其发送到 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 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 给出 {"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}]

在网址处:

9.100000381469727

json.dumps()之后的JSON字符串

[{"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}]

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": 0.0, "d": 10.0}]

在 URL 中,我得到类似 18.200000762939453 的值(此值来自稍后的脚本运行)

最佳答案

从其他几个答案中摘取最好的部分:

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}]'

关于python - 将 dict 值四舍五入为 2 位小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19145101/

相关文章:

python - 将聚合行分成不同的行,在 pandas 中添加唯一计数

ios - 如何使用json字典,让key为空value不select?

string - 使用字符串作为函数名 - golang

c# - 获取最后 2 位小数,不四舍五入

python - 在 Python 中查找多播 UDP 消息发件人的 MAC 地址?

python - 在 Linux 中拖放等效的批处理文件

python - 通过 for 循环将字典推送到列表

java - 正确除以 double 即可得到正确的金额

java - java中的四舍五入并添加分隔符

python - 如何使内循环考虑外循环的每次迭代?