TypeError: 'float' object is not subscriptable. Split coordinates by two lists

598 Views Asked by At

I need to split the list with coordinates in latitude and longitude lists.

coordinate =[[28.1944412,59.3611303],
         [28.1950085,59.3609766],
         [28.1950943,59.3611666],
         [28.1952418,59.3611556],
         [28.1951894,59.3609233],
         [28.1949748,59.3607484],
         [28.1932367,59.3601032],
         [28.1924642,59.3598571],
         [28.1909515,59.3595509],
         [28.1902434,59.3593814],
         [28.1902329,59.3593308],
         [28.1902322,59.3593196]]
         latitude=[]
         longtitude=[]
         for coodinates in coordinate:
             for coordinatere in coodinates:
                 longtitude.append(coordinatere[o])
                 latitude.append(coordinatere[i])
         print(latitude)

I receive this message.

TypeError: 'float' object is not subscriptable.

How can I fix it? Thanks for the help.

3

There are 3 best solutions below

1
On BEST ANSWER

Try this.

coordinate =[[28.1944412,59.3611303],[28.1950085,59.3609766],[28.1950943,59.3611666],[28.1952418,59.3611556],[28.1951894,59.3609233],[28.1949748,59.3607484],[28.1932367,59.3601032],[28.1924642,59.3598571],[28.1909515,59.3595509],[28.1902434,59.3593814],[28.1902329,59.3593308],[28.1902322,59.3593196]]
latitude=[]
longtitude=[]
for coodinates in coordinate:
    
    longtitude.append(coodinates[0])
    latitude.append(coodinates[1])
print(latitude)
0
On

you don't need the second for loop

for coodinates in coordinate:
     longtitude.append(coodinates[0])
     latitude.append(coodinates[1])
0
On

Single line list comprehension should do the job:


longitudes = [i[0] for i in coordinate]
latitudes = [i[1] for i in coordinate]