-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinting_tuple_records.py
More file actions
39 lines (29 loc) · 886 Bytes
/
printing_tuple_records.py
File metadata and controls
39 lines (29 loc) · 886 Bytes
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
32
33
34
35
36
37
38
39
def format_sort_records(data):
"""
This function takes a list of tuples with three
fields, first name, last name and duration as argument.
It will sort the list by last name and print a formatted
output.
@param: list
@return: str
"""
from operator import itemgetter
# Initialize variable.
ls = list()
# Sort data by last name.
data = sorted(data, key=itemgetter(1,0))
# Populate ls with formatted data.
for first, last, time in data:
ls.append("{:10} {:10} {:5.2f}".format(last, first, time))
# Return each record in ls on a new line.
return '\n'.join(ls)
def main():
PEOPLE = [('Joe', 'Biden', 7.85),
('Vladimir', 'Putin', 3.626),
('Jinping', 'Xi', 10.603)
]
# Sort and print a formatted version of PEOPLE.
print()
print( format_sort_records(PEOPLE))
print()
main()