Minimum value of a list

2

I'm putting together a list

lista.append(['{}{}'.format('Metros: ', metros),nombre,direccion])

and the output is:

[['Metros: 405', 'edificio A', 'San Lorenzo'], ['Metros: 1843', 'edificio B', 'Eusebio Blanco'], ['Metros: 3067', 'edificio C', 'Av. San Martín Sur , Godoy Cruz'], ['Metros: 2863', 'edificio D', 'Tomás Godoy Cruz']]

How can I get the minimum value of Meters from the list?

In this example I should return

  

['Meters: 405', 'building A', 'San Lorenzo']

    
asked by Sebastian 10.04.2018 в 17:07
source

4 answers

1

A very compact way to solve it could be:

lista = [['Metros: 405', 'edificio A', 'San Lorenzo'], 
         ['Metros: 1843', 'edificio B', 'Eusebio Blanco'], 
         ['Metros: 3067', 'edificio C', 'Av. San Martín Sur , Godoy Cruz'], 
         ['Metros: 2863', 'edificio D', 'Tomás Godoy Cruz']]

print(min([(e,float(e[0].split()[1])) for e in lista],key = lambda x: x[1])[0])

['Metros: 405', 'edificio A', 'San Lorenzo']

Detail :

  • By understanding lists: [(e,float(e[0].split()[1])) for e in lista] we build a new list of tuples that will contain the original element along with the meters taken to float . With split() we separate the first element of each list (meters) by space since we are only interested in the number.
  • We use min() on the previous list, indicating what index of the tuples will be applied: key = lambda x: x[1]
answered by 10.04.2018 / 17:46
source
2

You should consider transforming the list of lists into a list of dictionaries if possible, as you say @MLStud, these operations would be much simpler and more efficient, especially using operator.itemgetter and min / max , only by way of idea:

lista = [{'Metros': 405,   "Nombre": 'edificio A', "Direccion": 'San Lorenzo'},
         {'Metros': 1843,  "Nombre": 'edificio B', "Direccion": 'Eusebio Blanco'},
         {'Metros': 3067,  "Nombre": 'edificio C', "Direccion": 'Av. San Martín Sur , Godoy Cruz'},
         {'Metros': 2863,  "Nombre": 'edificio D', "Direccion": 'Tomás Godoy Cruz'}]


from operator import itemgetter

minimo = min(lista, key=itemgetter("Metros"))
>>> minimo
{'Metros': 405, "Nombre": 'edificio A', "Direccion": 'San Lorenzo'}

You can build the dictionary and add it to the list with:

lista.append({'Metros': metros, 'Nombre': nombre, 'Direccion': direccion})

If you can not do this, you can also use the built-in min and its argument key but passing it a function that is responsible for taking each sublist, take the first item, get the number by applying str.rsplit and pass it to float or int (so as not to carry out a lexicographical ordering). This is significantly more inefficient than using a dictionary since it requires a function call in pure Python, called%% co and casting to scale and two indexing operations on lists (whereas the previous example is mostly done using C code) compiled directly):

minimo = min(lista, key=lambda item: float(item[0].rsplit(" ", maxsplit=1)[-1]))
>>> minimo 
['Metros: 405', 'edificio A', 'San Lorenzo']
  

Note: str.rsplit looks for the first space in the chain from right to left and breaks the string there without looking further.

    
answered by 10.04.2018 в 18:28
1
min=-1
for x in lista:
   #Aqui miro si nunca ha habido un minimo y pongo el primero de la lista.
   if(min == -1):
      min=x.Metros
      menor=x
   #Comprobamos si es menor
   elif(x.Metros < min):
      min=x.Metros
      menor=x
#Imprimimos el menor
print menor

With that it should work

    
answered by 10.04.2018 в 17:19
1

Following Martin's response, I can think of something to optimize the code. Instead of creating a variable with a very low number and overwriting it if it has not been yet, you could create that variable with a very high number, which you will never reach, and from there go comparing it with the different numbers.

    min=  #el número que quieras pero que sea más grande que cualquiera de los que puedas tener (una exageración vamos)
for x in lista:
   if(min < x.Metros):
      min=x.Metros 
#Se imprime el menor
print min

I think that would be enough with c:

    
answered by 10.04.2018 в 17:44