将float赋值为字典键将改变其精度(Python) [英] Assigning float as a dictionary key changes its precision (Python)

查看:1731
本文介绍了将float赋值为字典键将改变其精度(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个浮动列表(实际上它是一个熊猫系列对象,如果它改变了任何东西),看起来像这样:

$ p $ mySeries:

...
22 16.0
23 14.0
24 12.0
25 10.0
26 3.1
.. 。

(所以这个系列的元素在右边,索引在左边) m试图从这个系列中的元素作为字典中的键,并将索引作为值,如下所示:

$ $ p $ {code $ {mySeries [我]:我为我在mySeries.index}

,...除外...

  {6400.0:0,66.0:13,3.1000000000000001:23,133.0:10,...为什么 3.1 突然改变为  code> 3.1000000000000001 ?我想这与浮点数的表示方式有关(?),但是为什么现在呢,我该如何避免/修复它?



编辑:如果这个问题不准确,请随时提出一个更好的标题。 好吧,看起来它是完全相同的号码,只是打印的方式不同。不过,如果我将 mySeries [26] 作为字典键,然后尝试运行:

  myDict [mySeries [26]] 

我得到 KeyError异常。什么是最好的方法来避免它?

解决方案

字典不改变3.1的浮点表示,但它实际上显示完整的精度。您的mySeries [26]的打印截断精度,并显示近似值。

您可以证明这一点:$ b​​
$ b <$ ($'$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'$

  0 16.00000000000000000000 
1 14.00000000000000000000
2 12.00000000000000000000
3 10.00000000000000000000
4 3.10000000000000008882
dtype:float64


$ b 编辑 p>

每一个计算机程序员应该知道浮点运算总是一个很好的阅读。



编辑

关于KeyError,我无法复制问题。

 >> x = pd.Series([16,14,12,10,3.1])$ ​​b $ b>> a = {x [i]:i for i in x.index} 
>> a [x [4]]
4
>> a.keys()
[16.0,10.0,3.1000000000000001,12.0,14.0]
>> hash(x [4])
2093862195
>> hash(a.keys()[2])
2093862195


I have a list of floats (actually it's a pandas Series object, if it changes anything) which looks like this:

mySeries:

...
22      16.0
23      14.0
24      12.0
25      10.0
26       3.1
...

(So elements of this Series are on the right, indices on the left.) Then I'm trying to assign the elements from this Series as keys in a dictionary, and indices as values, like this:

{ mySeries[i]: i for i in mySeries.index }

and I'm getting pretty much what I wanted, except that...

{ 6400.0: 0, 66.0: 13, 3.1000000000000001: 23, 133.0: 10, ... }

Why has 3.1 suddenly changed into 3.1000000000000001? I guess this has something to do with the way the floating point numbers are represented (?) but why does it happen now and how do I avoid/fix it?

EDIT: Please feel free to suggest a better title for this question if it's inaccurate.

EDIT2: Ok, so it seems that it's the exact same number, just printed differently. Still, if I assign mySeries[26] as a dictionary key and then I try to run:

myDict[mySeries[26]]

I get KeyError. What's the best way to avoid it?

解决方案

The dictionary isn't changing the floating point representation of 3.1, but it is actually displaying the full precision. Your print of mySeries[26] is truncating the precision and showing an approximation.

You can prove this:

pd.set_option('precision', 20)

Then view mySeries.

0    16.00000000000000000000
1    14.00000000000000000000
2    12.00000000000000000000
3    10.00000000000000000000
4     3.10000000000000008882
dtype: float64

EDIT:

What every computer programmer should know about floating point arithmetic is always a good read.

EDIT:

Regarding the KeyError, I was not able to replicate the problem.

>> x = pd.Series([16,14,12,10,3.1])
>> a = {x[i]: i for i in x.index}
>> a[x[4]]
4
>> a.keys()
[16.0, 10.0, 3.1000000000000001, 12.0, 14.0]
>> hash(x[4])
2093862195
>> hash(a.keys()[2])
2093862195

这篇关于将float赋值为字典键将改变其精度(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆