Python Forum
Get last tuple from two-dimensional array?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get last tuple from two-dimensional array?
#1
Hello,

Searching the archives with "two dimensional array last" returned no hits.

I need to find the last tuple from this kind of output:
[[2.3110949921647266, 48.81514649393979], [2.3113524842301563, 48.815923619981966], …[2.28392243385315, 48.84728589885864], [2.2819232977235515, 48.84809311844251]]
Is there a better way than this?
first = track['geometry']['coordinates'][0]

#BAD last = track['geometry']['coordinates'][-1]
#BAD last = track['geometry']['coordinates'].keys()[-1]
#BAD last = track['geometry']['coordinates'].count()
sizearray = len(track['geometry']['coordinates'])
print(track['geometry']['coordinates'][sizearray-1])
Thank you.
Reply
#2
What is the result of
print(type(track['geometry']['coordinates']))
?
Reply
#3
in Python you can use negative indexes
just
print(track['geometry']['coordinates'][-1])
by the way, this is list of lists, not lists of tuples. Although they look pretty much the same, lists are mutable, and tuples are not.
this is list [1, 2, 3]
and this is tuple (1, 2, 3)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Thanks for the info about list of lists vs. list of tuples.

>> print(type(track['geometry']['coordinates']))
<class 'list'>

>> print(track['geometry']['coordinates'][-1])
[2.2819232977235515, 48.84809311844251]
Odd: This time, "[-1]" worked Dodgy
Reply
#5
(Aug-27-2018, 11:44 AM)Winfried Wrote: Odd: This time, "[-1]" worked
when was it not working?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
In the original post:

#BAD last = track['geometry']['coordinates'][-1]
It returned an error, which is why I used the less elegant "sizearray-1".

Maybe it was a typo I didn't catch.

Another issue I came accross: In the JSON input file, most of the tracks are LineString, but a few of them are crappy Multilinestring (I guess the user's GPS burped at that point), so the code breaks because it ends up querying Nominatim with wrong data:

INPUT
GOOD : [2.3646783828735356, 48.844165146100025]
BAD : [[2.378497123718262, 48.85067473135253], [2.376008033752442, 48.85138776898278]]
import geojson
from geopy.geocoders import Nominatim

…

coords = track['geometry']['coordinates'][0]
#Flip coords: Nominatim expects lat, lon
coords = coords[::-1]
location = "{}, {}".format(coords[0], coords[1])

geolocator = Nominatim(user_agent="my-application",timeout=3)
location = geolocator.reverse(location)
try:
    return(location.raw['address']['town'])
except KeyError:
    return(location.raw['address']['city'])
In this particular case, it doesn't matter if there's a straight line all of a sudden between two coords, so, as a work-around, I was wondering if there's a way to turn Multilinestring into Linestring?
Reply
#7
Can you show small input file in full, with both good and bad data. LineString, MultilineString???
if you have list(array) of coordinates which one is the correct, according to you?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to quantize a 4 dimensional array? PythonNPC 2 1,627 Apr-23-2022, 04:34 PM
Last Post: Gribouillis
  How to create 2 dimensional variables in Python? plumberpy 5 1,866 Mar-31-2022, 03:15 AM
Last Post: plumberpy
  Slicing a 2 dimensional array Scott 2 1,670 Jan-12-2022, 07:18 AM
Last Post: paul18fr
  Problem using two-dimensional interpolation. Result looks bad player1682 4 2,524 Oct-12-2021, 09:27 AM
Last Post: player1682
  Index data must be 1-dimensional : Classifier with sklearn Salma 0 4,329 Apr-01-2021, 03:22 PM
Last Post: Salma
  2 Dimensional Arrays Prithak 4 2,606 Mar-21-2021, 09:35 PM
Last Post: deanhystad
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,847 Nov-04-2020, 11:26 AM
Last Post: Aggam
  comparing 2 dimensional list glennford49 10 4,154 Mar-24-2020, 05:23 PM
Last Post: saikiran
  Python 2 dimensional array creations sharpe 1 1,977 Nov-24-2019, 10:33 AM
Last Post: ThomasL
  How to get first line of a tuple and the third item in its tuple. Need Help, Anybody? SukhmeetSingh 5 3,218 May-21-2019, 11:39 AM
Last Post: avorane

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020