Domanda

I have a dictionary of latitude and longitude coordinates, and I am trying to index that dictionary with other coordinates. However, my coordinate get truncated inside my tuple. For example:

Value of the tuple:

In [17]: latLon
Out[17]: (37.540567000000003, -77.436044999999993)

When indexing:

KeyError: u'no item named (37.540567, -77.436045)'

How can I prevent python from truncating these values, so I can still use it as a key?

È stato utile?

Soluzione

Instead of using floating point numbers either use strings, where you can control the format, or decimals from the decimal library.

You need to do this because floating point numbers are rarely precise e.g.:

>>> ot = 1.0/10
>>> ot
0.10000000000000001
>>> decimal.Decimal(1)/10
Decimal('0.1')
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top