Python - 'float' object not subscriptable (GEOJSON file)

Viewed 431

I am trying to extract the index of a pair of coordinates within a GeoJSON file.

Code:

coords = []
for school in data_set['features']:    
  points = school['geometry']['coordinates']
 p1 = [point[0] for point in points]
coords.append(points)
print(coords)

Error message:

p1 = [point[0] for point in points]TypeError: 'float' object is not subscriptable.

When I run the code without defining p1 & p2 I receive the coordinates as floating points that are a nested list within a list. What do I have to do to make the object subscriptable? Also, what does subscriptable mean?

Thank you

2 Answers

strings are subscriptable, integers are not. Please use str() function

newStringForSubscriptable=str(point)

Generally, subscriptable means that you can use p1[index] to access something within p1 at index. The error tells you that point[0] is trying to access a float like that - which does not work.

The reason you're getting a nested list of floats is visible in this example that I ran in the python shell: append inserts the whole list, not each item individually.

>>> a = []
>>> b = [1,2,3]
>>> a.append(b)
>>> a
[[1, 2, 3]]

Instead, you probably wanted to use extend, which adds a list's element to the list.

>>> c = []
>>> c.extend(b)
>>> c
[1, 2, 3]

This means that your points is actually a non-nested list. And you could get the first point with points[0]

Related