Which item was sold less in Python [closed]

0

I have a list of tuples:

Tupper 11.4*5.6 cm, 2000, 30, 1400

He asks me to show which are the items that were sold in less quantities. (row 3, is the one that indicates the quantities sold)

I do not know how to follow the idea, I think it would be with an if condition but I would not know how to do it.

    
asked by 07.12.2018 в 12:36
source

1 answer

1

You can sort your list based on a key with sorted , then take the first or last item (depends if you are looking for the lowest or highest sales):

lista= [] 

archivo= open("arch.txt", "r")

for linea in archivo:

            denominacion, stock, precio, cant_vend = linea.split(",")

            tupla = (denominacion, int(stock), int(precio), int(cant_vend))

            lista.append(tupla)

newList = sorted(lista,key=lambda x: x[3]) //Ordena la lista en forma ascendente tomando como key el valor de la cuarta columna
print(newList)
print(newList[0]) //El primer elemento (menor venta)
print(newList[-1]) //El último elemento (mayor venta)

Exit:

[('Tupper 14*6.9 cm', 2000, 50, 1100), ('Tupper 16.7*8.1 cm', 2000, 70, 1300), ('Tupper 11.4*5.6 cm', 2000, 30, 1400), ('Mesa', 3000, 700, 1500), ('Silla', 4000, 300, 2000), ('Platos', 4000, 20, 2200), ('Vasos', 3000, 15, 2800), ('Botella', 5000, 100, 3000)]
('Tupper 14*6.9 cm', 2000, 50, 1100)
('Botella', 5000, 100, 3000)
    
answered by 07.12.2018 / 14:20
source