roee attias
roee attias

Reputation: 37

Organize the products from the most expensive one to the cheapest one (PYTHON)

I'm trying to organize a list with products from the most expensive one to the cheapest. and I only succeeded about recognize the most expensive one and i dont have an idea about how I am going to recognize the middel one or the cheapest one. if someone can help me I will really appreciate that. this is my code:

def sort_prices(list_of_tuples):
    i = 0
    count = 0
    the_new_list = [] # the final list with the products
    for prices in list_of_tuples:
        price = float(prices[1])
        count = 0
        for check_big in list_of_tuples: ## for loop to check if that the bigger price
            if price > float(check_big[1]):
                count += 1
        if count == len(list_of_tuples) - 1:
            the_new_list += prices
        i += 1

    print(the_new_list)
def main():
    products = [('milk', '5.5'), ('bread', '9.0'), ('candy', '2.5')]
    sort_prices(products)
if __name__ == "__main__":
    main()

Upvotes: 1

Views: 1047

Answers (3)

pakpe
pakpe

Reputation: 5479

You have several options:

Using Python built-in functions: You can use Python's built-in sorted() or sort() functions. Both functions take a key which can be set to a named function or an anonymous lambda function to determine how the list is sorted. Finally, the reversed=True argument makes sure it is sorted in descending order.

def sort_prices(list_of_tuples):
    the_new_list = sorted(list_of_tuples, key= lambda x: float(x[1]), reverse= True)
    print(the_new_list)

#[('bread', '9.0'), ('milk', '5.5'), ('candy', '2.5')]

Making your own sort function: If you are trying to make your own sort function, you need to decide which sort algorithm you want to use. There are several: bubble sort, insertion sort, selection sort, merge sort, quick sort, etc. Below is how insertion sort can be implemented for your list of tuples.

def sort_price(list_of_tuples):
    """Uses insertion sort algorithm. Traverses the list, comparing each item to the one
    before it and swapping it repeatedly with the previous item back through the preceding
    sorted portion of the list until it finds its rightful place."""

    my_list = list(list_of_tuples) #makes a copy of list_of_tuples
    for i in range(len(my_list)):
        j = i
        while float(my_list[j][1]) < float(my_list[j-1][1]) and j > 0:
            my_list[j-1], my_list[j] = my_list[j], my_list[j-1] #swap
            j-=1
    my_list.reverse() #to get it in descending order
    print(my_list)

#[('bread', '9.0'), ('milk', '5.5'), ('candy', '2.5')]

Upvotes: 1

abe
abe

Reputation: 987

def takeSecond(elem):
    return float(elem[1])

products = [('milk', '5.5'), ('bread', '9.0'), ('candy', '2.5')]
products.sort(key=takeSecond)
products
>>> [('candy', '2.5'), ('milk', '5.5'), ('bread', '9.0')]

Upvotes: 1

Avo Asatryan
Avo Asatryan

Reputation: 414

You can sort the list with key as follows:

products = [('milk', '5.5'), ('bread', '9.0'), ('candy', '2.5'), ('candy2', '20.5')]
products.sort(key=lambda x: float(x[1]))

Upvotes: 1

Related Questions