Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 44 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
44
Dung lượng
2 MB
Nội dung
data in files and arrays you are here 4 143 How do you think you can remember the names and the scores for each surfer in the contest? Once you've thought about this problem, turn over to Chapter 5 and see if you can resolve this issue. You somehow forgot the surfer names With your rush to catch some waves before the light is gone, you forgot about the other piece of data stored in the results.txt file: the name of each surfer. Without the names, you can’t possibly know which score goes with which name, so the scoreboard is only half-complete. The trouble is, your array stores one data item in each element, not two. Looks like you still have your work cut out for you. There’ll be no catching waves until this issue is resolved. Name Score Johnny 8.65 Juan 9.12 Joseph 8.45 Stacey 7.81 Aideen 8.05 Zack 7.21 Aaron 8.31 You forgot to process the names. 144 Chapter 4 programming toolbox CHAPTER 4 Your Programming Toolbox You’ve got Chapter 4 under your belt. Let’s look back at what you’ve learned in this chapter: Programming Tools * files - reading data stored on disk * arrays - a collection variable that holds multiple data items that can be accessed by index * sorting - arranging a collection in a specific order Python Tools * open() - open a file for processing * close() - close a file * for - iterate over something * string.split() - cut a string into multiple parts * [] - the array index operator * array.append() - add an item to the end of an array * array.sort() - sort an array, lowest-to- highest * array.reverse() - change the order of an array by reversing it this is a new chapter 145 To surf one‛s data properly, one must constantly practice one‛s pose hashes and databases 5 Putting data in its place Arrays aren’t the only show in town when it comes to data. Programming languages come with other data-arranging goodies too, and our chosen tool, Python, is no exception. In this chapter, you’ll associate values with names using a data structure commonly called the hash (better known as dictionary to Python-folk). And when it comes to working with stored data, you’ll read data from an external database system as well as from regular text-based files. All the world’s awash with data, so turn the page and start applying your ever-expanding programming skills to some cool data-processing tasks. still looking for a winner Who won the surfing contest? In the previous chapter, you worked out the top three scores, but they’re not much use without the names of the surfers that achieved those scores. There will no be surfing for you until you’ve finished the program. Here’s the code so far: scores = [] result_f = open("results.txt") for line in result_f: (name, score) = line.split() scores.append(float(score)) result_f.close() scores.sort() scores.reverse() print("The top scores were:") print(scores[0]) print(scores[1]) print(scores[2]) 1 st 2 nd 3 rd W i n n e r s 146 Chapter 5 You still don't know who won. hashes and databases The fix is easy. Just use two arrays. How hard is that? Rewrite your current program to use two arrays: one to keep track of the scores, the other to keep track of the surfer names. you are here 4 147 148 Chapter 5 two arrays You were to rewrite your current program to use two arrays: one to keep track of the scores, the other to keep track of the surfer names. scores = [] names = [] result_f = open(“results.txt") for line in result_f: (name, score) = line.split() scores.append(float(score)) names.append(name) result_f.close() scores.sort() scores.reverse() names.sort() names.reverse() print(“The highest scores were:") print(names[0] + ‘ with ' + str(scores[0])) print(names[1] + ‘ with ' + str(scores[1])) print(names[2] + ‘ with ' + str(scores[2])) As well as the scores array, you now need a names array, too. Remember to sort the names array. Append the surfer's name to the names array. Load this! Don't forget to download results. txt from the Head First Programming website before continuing. you are here 4 149 hashes and databases Test Drive These results don't make sense! Those results look a little strange. Zack is only a novice surfer but, according to the results from your program, Zack has the highest score. It looks like the association between the surfer names and their scores is somehow lost and if you think about it, this is exactly what’s happening. The two arrays are independent of each other: one contains scores, the other names. When the data is in the file, the surfer name and the scores are associated with each other because they appear on the same line. However, once the split occurs and the data is in the arrays, the association is severed. Sorting one array has no effect on the ordering of the other. No wonder your results are a little off the wall. How do you fix this? With the results.txt file saved to the same directory as your program, enter this code into IDLE and see what happens. Remember to save your program, then press F5 to run it. 150 Chapter 5 need a new data structure Zack Stacey Juan Joseph Johnny Aideen Aaron 9.12 8.65 8.45 8.31 8.05 7.81 7.21 Associate the name with the score Using two arrays just won’t cut it. You need some other data structure to hold your data in such a way that the association between the surfers’ name and their score is maintained. You need a different data structure. But which one? Data Structure A standard method of organizing a collection of data items in your computer's memory. You've already met one of the classic data structures: the array. Scores Names 8.65 Johnny 9.12 Juan 8.45 Joseph 7.81 Stacey 8.05 Aideen 7.21 Zack 8.31 Aaron What you need is something that looks like this. Many rows of data Exactly two columns of matched data Here's the array of sorted scores and here's the array of sorted names. But the scores no longer match the names! you are here 4 151 hashes and databases A variable with multiple indexed slots for holding data Array Match the data structure names on the left with their descriptions on the right. We’ve already done one for you. Which one do you think you’ll need to use for the surfer data? A variable that creates a chain of data where one data item points to another data item, which itself points to another data item, and another, and so on and so forth Linked list A variable that allows data to enter at one end of a collection and leave at the other end, supporting a first-in, first-out mechanism Queue A variable that has exactly two columns and (potentially) many rows of data Hash A variable that contains a collection of unique data items Set A variable that contains data arranged as a matrix of multiple dimensions (but typically, only two) Multi-dimensional array 152 Chapter 5 hash it out A variable with multiple indexed slots for holding data Array You were asked to match the data structure names on the left with their descriptions on the right. You were also to identify which one you thought you might need to use for the surfer data. A variable that creates a chain of data where one data item points to another data item, which itself points to another data item, and another, and so on and so forth Linked list A variable that allows data to enter at one end of a collection and leave at the other end, supporting a first-in, first-out mechanism Queue A variable that has exactly two columns and (potentially) many rows of data Hash A variable that contains a collection of unique data items Set A variable that contains data arranged as a matrix of multiple dimensions (but typically, only two) Multi-dimensional array Use a hash You need to use a data structure that maintains the association between the surfer score and the surfer name, which is exactly what a hash gives you. There are lots of surfers with lots of scores, and you need to maintain the association between the two pieces of information. Let’s take a look at how hashes work. Geek Bits Hashes go by different names in different programming languages: mapping, dictionary, associative array, and key-value list, to name a few. In this book, we’ll stick to using the name hash. SOlUTion This cuts down on the amount of typing and saves our poor fingers! Here's the one you need. Known in the Python world as a “dictionary.” [...]... before) cursor.close() return(s) cursor.close() return({}) 172 Chapter 5 = str(row[‘age']) hashes and databases Test Drive Change your program in IDLE to use the new version of the function (which now talks to TVN’s database) Save your program under a new name and press F5 to run it Load this! Download surfersDB.sdb from the Head First Programming website before running this test drive Now, as well as... time, that function’s code might change to suit your needs Smart programmers take advantage of modular programming techniques to keep their workload manageable Let’s find out how in the pages that follow this is a new chapter 177 technology upgrade Head First Health Club is upgrading some systems Head First Health Club has a new CEO, and he loves new technology He was shocked when he saw how old the... the caller return({}) You processed the en file but found NO Mtire ATCH Close the file and return an empty hash 166 Chapter 5 Cut up the line (using split()) and assign the data to the hash (using multiple-assignment) Load this! Download surfing_data.csv from the Head First Programming website before continuing Be sure to put the file in the same directory/folder as your code hashes and databases... their own set of methods, but append() is not one of them 156 Chapter 5 Q: A: Can I use anything as the key of a hash? No, you can’t The rules Python applies here can be complex (as they can be in other programming languages, too) The best advice we can give you is to stick with numbers and strings as keys Trust us: this isn’t as restrictive as it first sounds and is, by far, the best/easiest strategy... happier Your program has made their day The TVN sports writers know how to celebrate My guys are happy and so am I, Who do I make the check out to? 174 Chapter 5 hashes and databases Your Programming Toolbox CHAPTER 5 You’ve got Chapter 5 under your belt Let’s look back at what you’ve learned in this chapter: ols rammingucToe that Prog ta str tur da * hash - aa name with a value associates the value... the one-line change applied, save your program and press F5 to run it again The scores are associated with the surfer's names and they are now sorted, too! Fantastic! You’ve identified the top 3 surfers Time to hit those waves, dude! Surf-A-Thon 1 Juan 9.12 2 Johnny 8. 65 3 Joseph 8. 45 That makes it threein-a-row for Juan! you are here 4 159 more complex surfer data When data gets complex Hot on... of the data (if we have a match) Tidy up after yourself (always a good idea) 170 Chapter 5 power of this code if you know a It is possible to improve the efficiency andavoiding improving TVN's SQL However, little bit about SQL We are deliberately to those who want to learn more we strongly recommend Head First SQL” hashes and databases Rewrite your function to retrieve the data it needs from the... square brackets scores[8. 45] = 'Joseph' Scores 8. 45 A new row of data is added to the hash Note the association to the right of and put the value ator the assignment oper Joseph Note how the act of assigning a value to a key CREATES the hash entry (assuming it's not already there) In Python, there's no explicit “append()" method for hashes as there is for arrays you are here 4 153 iterate hash data Iterate... ID Name 101;Johnny 'wave-boy' Jones;USA;8.32;Fish;21 Country 102;Juan Martino;Spain;9.01;Gun;36 Average score 103;Joseph 'smitty' Smyth;USA;8. 85; Cruiser;18 Preferred board type 104;Stacey O'Neill;Ireland;8.91;Malibu;22 Age 1 05; Aideen 'board babe' Wu;Japan;8. 65; Fish;24 106;Zack 'bonnie-lad' MacFadden;Scotland;7.82;Thruster;26 107;Aaron Valentino;Italy;8.98;Gun;19 RSA's data is stor in each line, with... let’s go ahead and make that change Now that you are sorting the keys of the hash (which represent the surfer’s scores), it should be clear why the scores were used as the key when adding data into the hash: you need to sort the scores, not the surfer names, so the scores need to be on the left side of the hash (because that’s what the built-in sorted() function works with) 158 Chapter 5 Do this! . to save your program, then press F5 to run it. 150 Chapter 5 need a new data structure Zack Stacey Juan Joseph Johnny Aideen Aaron 9.12 8. 65 8. 45 8.31 8. 05 7.81 7.21 Associate the name with. resolved. Name Score Johnny 8. 65 Juan 9.12 Joseph 8. 45 Stacey 7.81 Aideen 8. 05 Zack 7.21 Aaron 8.31 You forgot to process the names. 144 Chapter 4 programming toolbox CHAPTER 4 Your Programming Toolbox You’ve. waves, dude! Surf-A-Thon 1. Juan 9.12 2. Johnny 8. 65 3. Joseph 8. 45 you are here 4 159 That makes it three- in-a-row for Juan! 160 Chapter 5 more complex surfer data 101;Johnny 'wave-boy'