-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunit8_ex8.2.2.py
31 lines (24 loc) · 1.1 KB
/
unit8_ex8.2.2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# exercise 8.2.2 from unit 8
'''
Exercise 8.2.2
Write a function called sort_prices defined as follows:
def sort_prices(list_of_tuples):
The function receives a list of tuples that each have an item and a price.
The function returns a list of tuples sorted by the price of the items in them from the largest to the smallest.
Define the list of tuples that the function receives according to the following form:
[('item', 'price'), ('item', 'price'), ('item', 'price')]
Note that all members are of string type and price is written as a non-integer number.
Running examples of the sort_prices function
>>> products = [('milk', '5.5'), ('candy', '2.5'), ('bread', '9.0')]
>>> sort_prices(products)
[('bread', '9.0'), ('milk', '5.5'), ('candy', '2.5')]
'''
def sort_prices(list_of_tuples):
def get_price(tuple):
return float(tuple[1])
# sort the list of tuples by the price of the item in each tuple
sorted_list = sorted(list_of_tuples, key=get_price, reverse=True)
return sorted_list
# test the function
products = [('milk', '5.5'), ('candy', '2.5'), ('bread', '9.0')]
print(sort_prices(products))