Operation Manual

Experiments in Python
92
Notes:
# sort a le full of lm names
# dene a function that will return the year a lm was made
# split the right side of the line at the rst ”(
def lmYear( l m ):
return l m.rsplit('(',1)[ 1]
# load the le into a list in Python memory
# and then close the le because the content is now in memory
with open("lmlist", "r") as le:
lmlist = le.read().splitlines()
# sort by name using library function
lmlist.sort()
# sort by year using key to library function - the lm list
# must end with a year in the format (NNNN)
lmlist.sort(key=lmYear)
# print the list
for lm in lmlist:
print(lm)
Instead of taking input from the user of the program, this experiment takes input
from a file containing text. It processes the file to sort its lines into name and year
of release order and then prints the list to the screen.
Getting the data from a file requires it to be opened, read into a Python list and
then closed. There is a possibility that the file could be very large and not fit in the
Python memory. We will cover how to solve this problem later.
Sorting the list is easy, as there is a function called “sort, which runs a well-
established sorting algorithm on the list. It sorts by the initial letters (alphabetically)
of each line. This is done first in this example. If you want to sort some other way,
you need to provide the sorting key. This key is the bit of each list entry that is to
be compared with other keys to decide which comes first.
The second sort uses a key for the film year. To get the year out of any list entry,
the “filmYear” function splits the entry into two at the rightmost “(” and uses that.
This is the year of the film, in our case. This program will fail if the film title does
not have a year following the first final “(” in the name, or if there is a line in the file
that contains no film name. Normally you will have to validate your input. One of
the rules of programming for real people is: “Never trust your user”.
Try removing each sort to see what happens. Try putting extra “(” in some of the
film names.