我有一本叫做 G 的字典。当我输入G. keys ()
,输出的一个示例是:
>>> 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),...
当我使用操作时key in G
结果是假的。
>>> (1490775.0, 12037425.0) in G
False
为什么我的字典无法识别我的按键?
>>> 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'>
您可能是这样遇到这种情况的:
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
:
print(arr[0] in G)
# True
你可能最好不要使用numpy.voids
作为钥匙。相反,如果您确实需要一个字典,那么也许首先将数组转换为列表:
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)