Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1#!/usr/bin/env python3 

2"""Solution to chapter 3, exercise 13: tuple_records""" 

3 

4 

5import operator 

6PEOPLE = [('Donald', 'Trump', 7.85), 

7 ('Vladimir', 'Putin', 3.626), 

8 ('Jinping', 'Xi', 10.603)] 

9 

10 

11def format_sort_records(list_of_tuples): 

12 """This function expects to get a list 

13of tuples, each representing a person. 

14 

15Each tuple contains three elements -- first 

16name, last name, and distance to travel. 

17 

18(The first two are strings, and the third is 

19a float.) We return a list of strings, 

20sorted by last name and then first name. 

21""" 

22 output = [] 

23 for person in sorted(list_of_tuples, key=operator.itemgetter(1, 0)): 

24 output.append("{1:10} {0:10} {2:5.2f}".format(*person)) 

25 return output