Raspberry_Pi_Education_Manual

Notes:

# sort a file full of film names # define a function that will return the year a film was made # split the right side of the line at the first ”(” def filmYear (film): return film.rsplit( '(' ,1)[1] # load the file into a list in Python memory # and then close the file because the content is now in memory with open ( "filmlist" , "r" ) as file : filmlist = file .read().splitlines() # sort by name using library function filmlist.sort() # sort by year using key to library function - the film list # must end with a year in the format (NNNN) filmlist.sort(key=filmYear) # print the list for film in filmlist: print (film)

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.

Experiments in Python

92

Made with FlippingBook flipbook maker