Dictionary not recognizing floating point keys Dictionary not recognizing floating point keys numpy numpy

Dictionary not recognizing floating point keys


This might be how you arrived in this situation:

import numpy as nparr = np.array([(1490775.0, 12037425.0)], dtype=[('foo','<f8'),('bar','<f8')])arr.flags.writeable = FalseG = dict()G[arr[0]] = 0print(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'>

A tuple of floats is not a key in G:

print((1490775.0, 12037425.0) in G)# False

But the numpy.void instance is a key in G:

print(arr[0] in G)# True

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]: GOut[175]: {(1490775.0, 12037425.0): 0}In [176]: (1490775.0, 12037425.0) in GOut[176]: True