Page 14 -
P. 14
table of contents
data files and arrays
4 Sort it out
As your programs develop, so do your data handling needs.
And when you have lots of data to work with, using an individual variable for each piece
of data gets really old, really quickly. So programmers employ some rather awesome
containers (known as data structures) to help them work with lots of data. More times
than not, all that data comes from a file stored on a hard disk. So, how can you work
with data in your files? Turns out it’s a breeze.
Surf’s up in Codeville 114
Find the highest score in the results file 115
Iterate through the file with the open, for, close pattern 116
The file contains more than numbers... 120
Split each line as you read it 121
The split() method cuts the string 122
But you need more than one top score 126
Keeping track of 3 scores makes the code more complex 127
An ordered list makes code much simpler 128
Sorting is easier in memory 129
Hey, dude, it‛s You can’t use a separate variable for each line of data 130
Chapter 4... time
for a break - let‛s An array lets you manage a whole train of data 131
catch some waves. Python gives you arrays with lists 132
Sort the array before displaying the results 136
Sort the scores from highest to lowest 139
And the winner is...? 142
You somehow forgot the surfer names 143
Your Programming Toolbox 144
xiii