Get last tuple from two-dimensional array? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Get last tuple from two-dimensional array? (/thread-12483.html) |
Get last tuple from two-dimensional array? - Winfried - Aug-27-2018 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. RE: Get last tuple from two-dimensional array? - Gribouillis - Aug-27-2018 What is the result of print(type(track['geometry']['coordinates']))? RE: Get last tuple from two-dimensional array? - buran - Aug-27-2018 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)
RE: Get last tuple from two-dimensional array? - Winfried - Aug-27-2018 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 RE: Get last tuple from two-dimensional array? - buran - Aug-27-2018 (Aug-27-2018, 11:44 AM)Winfried Wrote: Odd: This time, "[-1]" workedwhen was it not working? RE: Get last tuple from two-dimensional array? - Winfried - Aug-27-2018 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? RE: Get last tuple from two-dimensional array? - buran - Aug-27-2018 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? |