1 Furylord OP 用 round 貌似无效 |
![]() | 2 incompatible 2016-07-28 11:10:31 +08:00 因为你的元素是 float 类型的? 想放到字典里,你应该把它转为 string 或者 decimal 类型做为 key 。 |
![]() | 3 ProfFan 2016-07-28 11:12:57 +08:00 你看看你 ndarray 的 dtype |
![]() | 4 ProfFan 2016-07-28 11:15:47 +08:00 顺便 ``` >>> 40.123199999999997 == 40.1232 True >>> a=40.123199999999997 >>> a.as_integer_ratio() (22057962471791, 549755813888) >>> b=40.1232 >>> b.as_integer_ratio() (22057962471791, 549755813888) >>> ``` |
![]() | 5 ProfFan 2016-07-28 11:24:21 +08:00 试了一下,没跑了。 numpy 默认用的 float64 , python 32 , python 的精度太低。 >>> for i in b: ... print(type(tuple(i)[0])) ... <class 'numpy.float64'> <class 'numpy.float64'> <class 'numpy.float64'> 明白了吧 |
![]() | 6 ProfFan 2016-07-28 11:34:58 +08:00 妈蛋弄错了,果然睡觉不能答题 23333 只是显示不一样而已, numpy 默认显示高精度表示 numpy.float64(40.1232) 40.123199999999997 |
![]() | 7 Ahri 2016-07-28 14:03:15 +08:00 话说用 tuple of floats 当 hash key 不是很好的习惯呃,不知道楼主碰到什么样的需求。 |
9 Furylord OP |
![]() | 10 necomancer 2016-07-29 06:54:59 +08:00 |
![]() | 11 necomancer 2016-07-29 06:56:44 +08:00 @Furylord 数据量如果不大就直接 numpy.array 吧,如果只多几万个零是不是也能忍了~我现在是 numbapro 党,一般没有啥效率问题,都能忍,有些问题就懒得去再看 sparse 了。 |
![]() | 12 necomancer 2016-07-29 07:03:22 +08:00 @Furylord 如果想避免小数做索引,我建议做个 dict 把点标号,另一个用正常的矩阵表示,保证索引都是整数~ |
![]() | 13 ruoyu0088 2016-07-30 13:29:57 +08:00 numpy 有自己的浮点数类型:numpy.float64 ,其转换为字符串的程序和 float 类型不同,因此显示不同。可以用 tolist()把数组转换为列表: print(tuple(i.tolist())) |