Lower value in a matrix

2

I'm calculating the distance from a point to other points and I keep them in a matrix, how can I get the minimum value by taking the distance it contains, I should show that data found the distance the name and the address of the lowest value

lista=[]
lista += [distancia,nombre,direccion]

The values that are stored as an example are the following.

<class 'list'>: [0.5989068259400513, 'YPF - Torre Blanca', 'C1049AAP, Alem 744']
<class 'list'>: [1.5000067783445622, 'YPF - Torre Blanca', 'C1049AAP, Tucumán 466']
<class 'list'>: [4.6825980651789, 'YPF - Torre Blanca', 'C1049AAP, Parana 890']
<class 'list'>: [0.9999068259400518, 'YPF - Torre Blanca', 'C1049AAP, Lavalle 147']

etc ..

Thanks for the help.

Edit:

for lugar in busquedajson['results']:

        nombre=lugar['name']
        direccion=lugar['vicinity']

        #Point one
        latJson = lugar['geometry']['location']['lat']
        lonJson = lugar['geometry']['location']['lng']

        distancia = gpxpy.geo.haversine_distance(latJson,lonJson,r[1],r[0])

        metros = int(float(distancia))

        lista=[]
        lista += [metros,nombre,direccion]

        #archivolugares.write(lugar['name'] + ',' + str(lugar['geometry']['location']['lng']) \
        #+ ',' + str(lugar['geometry']['location']['lat']) + '\n')

minimo = min ( ((i , lista[ i ]) for i in range ( 0 , len ( lista ) , 3 )) , key=itemgetter ( 1 ) )[ 0 ]
print ( lista[ minimo:minimo + 3 ] )
    
asked by Debianitas 08.02.2018 в 15:47
source

2 answers

0

I suggest you manage the elements in a different way, like a list of lists, something like this:

lista=[]
lista.append([0.5989068259400513, 'YPF - Torre Blanca', 'C1049AAP, Alem 744'])
lista.append([1.5000067783445622, 'YPF - Torre Blanca', 'C1049AAP, Tucumán 466'])
lista.append([4.6825980651789, 'AAYPF - Torre Blanca', 'C1049AAP, Parana 890'])
lista.append([0.9999068259400518, 'YPF - Torre Blanca', 'C1049AAP, Lavalle 147'])

In which case the solution would be to use operator.itemgetter combined with min() as follows:

lista = [
          [0.5989068259400513, 'YPF - Torre Blanca', 'C1049AAP, Alem 744'],
          [1.5000067783445622, 'YPF - Torre Blanca', 'C1049AAP, Tucumán 466'],
          [4.6825980651789, 'YPF - Torre Blanca', 'C1049AAP, Parana 890'],
          [0.9999068259400518, 'YPF - Torre Blanca', 'C1049AAP, Lavalle 147']
        ]

from operator import itemgetter
print(min((e for e in lista),key=itemgetter(0)))

Exit:

[0.5989068259400513, 'YPF - Torre Blanca', 'C1049AAP, Alem 744']

Basically to min() we pass each complete sublist, but we tell you that the minimum search does it only on the first element of each list.

Now if you maintain your structure:

lista=[]
lista += [0.5989068259400513, 'YPF - Torre Blanca', 'C1049AAP, Alem 744']
lista += [1.5000067783445622, 'YPF - Torre Blanca', 'C1049AAP, Tucumán 466']
lista += [4.6825980651789, 'AAYPF - Torre Blanca', 'C1049AAP, Parana 890']
lista += [0.9999068259400518, 'YPF - Torre Blanca', 'C1049AAP, Lavalle 147']

That is a single list with all the values one behind the other, this is something a little more complex to administer, for example to access the distance we must iterate from three elements, but eventually we can also handle it with a similar solution to the previous one only a little more complex:

from operator import itemgetter

lista = [0.5989068259400513, 'YPF - Torre Blanca', 'C1049AAP, Alem 744',
        1.5000067783445623, 'YPF - Torre Blanca', 'C1049AAP, Tucumán 466', 
        4.6825980651789, 'AAYPF - Torre Blanca', 'C1049AAP, Parana 890', 
        0.9999068259400518, 'YPF - Torre Blanca', 'C1049AAP, Lavalle 147'
        ]

minimo = min(((i, lista[i]) for i in range(0,len(lista),3)),key=itemgetter(1))[0]
print(lista[minimo:minimo+3])

> [0.5989068259400513, 'YPF - Torre Blanca', 'C1049AAP, Alem 744']

With (i, lista[i]) for i in range(0,len(lista),3) we generate a set of tuples with the index that points to the distance and the value of it. We apply the min() in a similar way to the previous example and we arrive at a value of the style (0, 0.5989068259400513) of which only we are interested in the index that we assign to minimo , finally we make a cut (slice) of the list taking from the index where the minimum appears, only three values print(lista[minimo:minimo+3])

    
answered by 08.02.2018 / 16:03
source
0

Completing the end of @ Patricio-Moracho's response:

There is an elegant way to chop and get the minimum using only iterators:

from operator import itemgetter
from itertools import islice

lista = [0.5989068259400513, 'YPF - Torre Blanca', 'C1049AAP, Alem 744',
        1.5000067783445623, 'YPF - Torre Blanca', 'C1049AAP, Tucumán 466', 
        4.6825980651789, 'AAYPF - Torre Blanca', 'C1049AAP, Parana 890', 
        0.9999068259400518, 'YPF - Torre Blanca', 'C1049AAP, Lavalle 147'
        ]

it = iter(lista)
troceo = (list(islice(it,3)) for j in range(4))
minimo = min(troceo, key=itemgetter(0))

....

    
answered by 08.02.2018 в 18:47