0

I have a list containing the following, which is a bounding box formed of latitude and longitude points:

[u'51.2867602', u'51.6918741', u'-0.510375', u'0.3340155']

I'm currently attempting to convert each value inside this list to a float, in order to pass it into a different function. My current code is set up like this:

for coordinate in coordinates:
    coordinate = float(coordinate)

However, this does not seem to work, as printing the list again provides the exact same output as the initial list. No error arises while running that code, however.

Any help on this would be much appreciated; my experience with Unicode is limited! I'm using Python 2.7.10.

Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
dantdj
  • 1,237
  • 3
  • 19
  • 40

3 Answers3

2

You forgot to do the assignment:

for i, coordinate in enumerate(coordinates):
    coordinates[i] = float(coordinate)
Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
1

coordinate, in this context, is a local variable

for coordinate in coordinates:
    coordinate = float(coordinate)

Therefore, assigning float(coordinate) to the local variable will have no effect on the real list.

Instead, reference the list itself, like this:

for i in range(len(coordinates)):
    coordinates[i] = float(coordinates[i])
rafaelc
  • 57,686
  • 15
  • 58
  • 82
1

Another solution, using map

coordinates = [u'51.2867602', u'51.6918741', u'-0.510375', u'0.3340155']
coordinates = map(float, coordinates)
Jose Ricardo Bustos M.
  • 8,016
  • 6
  • 40
  • 62