字典不识别浮点键 [英] Dictionary not recognizing floating point keys
本文介绍了字典不识别浮点键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字典G.当我输入 G。 keys()
,输出的示例是:
I have a dictionary called G. When I enter G. keys ()
, an example of the output is:
>>> G.keys ()
[(1490775.0, 12037425.0), (1493775.0, 12042675.0), (1481055.0, 12046305.0), (1503105.0, 12047415.0), (1488585.0, 12050685.0), (1483935.0, 12051405.0),...
当我在G中使用操作键时,结果是假的。
When I use the operation key in G
the result is false.
>>> (1490775.0, 12037425.0) in G
False
为什么我的字典不识别我的钥匙?
Why isn't my dictionary recognizing my keys?
>>> type (G.keys()[0])
<type 'numpy.void'>
>>> type (G.keys()[0][0])
<type 'numpy.float64'>
>>> type (G.keys()[0][1])
<type 'numpy.float64'>
type(G)
<type 'dict'>
推荐答案
这可能是你如何到达这种情况: / p>
This might be how you arrived in this situation:
import numpy as np
arr = np.array([(1490775.0, 12037425.0)], dtype=[('foo','<f8'),('bar','<f8')])
arr.flags.writeable = False
G = dict()
G[arr[0]] = 0
print(type(G.keys()[0]))
# <type 'numpy.void'>
print(type(G.keys()[0][0]))
# <type 'numpy.float64'>
print(type(G.keys()[0][1]))
# <type 'numpy.float64'>
print(type(G))
# <type 'dict'>
一个浮点数元组不是 G
:
print((1490775.0, 12037425.0) in G)
# False
但是,numpy.void实例是 G
中的一个键:
But the numpy.void instance is a key in G
:
print(arr[0] in G)
# True
您将无需使用 numpy.voids
作为键。相反,如果您真的需要一个dict,那么可能首先将数组转换为列表:
You will probaby be better off not using numpy.voids
as keys. Instead, if you really need a dict, then perhaps convert the array to a list first:
In [173]: arr.tolist()
Out[173]: [(1490775.0, 12037425.0)]
In [174]: G = {item:0 for item in arr.tolist()}
In [175]: G
Out[175]: {(1490775.0, 12037425.0): 0}
In [176]: (1490775.0, 12037425.0) in G
Out[176]: True
这篇关于字典不识别浮点键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文