Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将此元组列表转换为dict将浮点值舍入为0.0,但类似的列表不会';T_Python - Fatal编程技术网

Python 将此元组列表转换为dict将浮点值舍入为0.0,但类似的列表不会';T

Python 将此元组列表转换为dict将浮点值舍入为0.0,但类似的列表不会';T,python,Python,在Python 2.7和3.6.1中,如果我这样做: print(dict([(9, 0.3864042114900435), (10, 0.3782101167315175), (8, 0.16763561455710688), (7, 0.05233844510566873), (4, 0.0), (5, 0.0), (6, 0.0), (7, 0.0), (8, 0.0), (9, 0.0), (10, 0.0), (11, 0.0)])) 我得到: {9: 0.0, 10: 0.0,

在Python 2.7和3.6.1中,如果我这样做:

print(dict([(9, 0.3864042114900435), (10, 0.3782101167315175), (8, 0.16763561455710688), (7, 0.05233844510566873), (4, 0.0), (5, 0.0), (6, 0.0), (7, 0.0), (8, 0.0), (9, 0.0), (10, 0.0), (11, 0.0)]))
我得到:

{9: 0.0, 10: 0.0, 8: 0.0, 7: 0.0, 4: 0.0, 5: 0.0, 6: 0.0, 11: 0.0}
为什么它是四舍五入的,我能做些什么来防止它

我认为这是Python解释器中不知道的某种浮点精度问题,但如果是这样的话,那么为什么会这样:

print(dict([(9, 0.3864042114900435), (10, 0.3782101167315175), (4, 0.0)]))
返回(无舍入):


这是因为您正在用0.0覆盖初始值


7、8、9和10在第一个表示中出现两次,第二个值为0.0。

您已经分别使用了7、8、9和10键两次。字典是用元组列表中较晚出现的元组构建的。这些都是0。哇,我真是太蠢了。。。我没有看到那些,它们也不应该在那里。非常感谢@thesilkworm!!!
{9: 0.3864042114900435, 10: 0.3782101167315175, 4: 0.0}