Read Into the Program From the Data Text File and Assign to an Array

Affiliate iv. Data in Files and Arrays: Sort it out

image with no caption

As your programs develop, so do your data handling needs.

And when you accept lots of data to piece of work with, using an private variable for each piece of data gets actually old, actually chop-chop. Then programmers utilize some rather crawly containers (known as data structures ) to aid them work with lots of data. More times than not, all that data comes from a file stored on a hd. So, how tin you piece of work with information in your files? Turns out it'due south a breeze. Flip the page and let's learn how!

Surf'southward up in Codeville

The annual Codeville Surf-A-Thon is more than pop than ever this year.

Considering there are and then many contestants, the organizers asked you to write a Python program to process the scores. Eager to please, you agreed.

The trouble is, even though the contest is over and the embankment is now clear, you lot can't hit the waves until the plan is written. Your program has to work out the highest surfing scores. Despite your urge to surf, a promise is a promise, and so writing the plan has to come get-go.

image with no caption

Observe the highest score in the results file

After the judges charge per unit the competitors, the scores are stored in a file called results.txt. There is one line in the file for each competitor's score. Y'all need to write a program that reads through each of these lines, picks out the score, then works out the highest score in the Surf-A-Thon.

image with no caption

Information technology sounds simple enough, except for 1 small detail. You've written programs to read data from the Spider web, and read data that'south been typed in at the keyboard, but you haven't still written whatever lawmaking that reads data stored in a file.

Iterate through the file with the open, for, shut pattern

If you need to read from a file using Python, 1 fashion is to use the built-in open() command. Open a file called results.txt like this:

image with no caption

The call to open up() creates a file handle , which is a shorthand that you'll utilise to refer to the file you are working with within your code.

Because yous'll need to read the file one line at a fourth dimension , Python gives yous the for loop for simply this purpose. Like while loops, the for loop runs repeatedly, running the loop code in one case for each of the items in something. Call back of a for loop every bit your very own custom-fabricated data shredder:

image with no caption

Each fourth dimension the body of the for loop runs, a variable is set to a string containing the electric current line of text in the file. This is referred to as iterating through the data in the file:

image with no caption

Load this!

To successfully run this program, you lot need to grab a copy of the results.txt information file from the Caput First Programming website. Exist sure to put the data file in the aforementioned directory (or binder) that contains your code.

The file contains more than numbers...

To see what happened, allow'south accept another expect at the judge's score canvass to run across if you missed anything:

image with no caption

The judges also recorded the name of each surf contestant next to his or her score. This is a problem for the program only if the proper name was added to the results.txt file. Let'southward take a await:

image with no caption

Sure enough, the results.txt file also contains the contestant names. And that'south a problem for our code because, as it iterates through the file, the string yous read is no longer just a number .

Divide each line as yous read it

Each line in the for loop represents a single string containing two pieces of information:

image with no caption

You demand to somehow extract the score from the string. In each line, there is a proper name, followed by a infinite, followed by the score. You already know how to extract one string from another; yous did it for Starbuzz dorsum in Chapter 2. And you could practise something similar here using the find() method and alphabetize manipulation, searching for the position of a space (' ') character in each line then extracting the substring that follows it.

Programmers oftentimes accept to bargain with information in strings that contain several pieces of data separated by spaces. It's then common, in fact, that Python provides a special string method to perform the cutting you need: split().

Python strings have a built-in dissever() method.

Note

And y'all'll find that other programming languages have very similar mechanisms for breaking up strings.

The split up() method cuts the string

Imagine you take a cord containing several words assigned to a variable. Think of a variable as if it's a labeled jar :

image with no caption

The rock_band cord, like all Python strings, has a dissever() method that returns a drove of substrings: one for each word in the original string.

Using a programming feature called multiple assignment , yous can take the result from the cutting performed by split() and assign it to a collection of variables:

image with no caption

Each of the return values from the split() on rock_band is assigned to its own separately named variable, which allows yous so to work with each give-and-take in whatever way you want. Annotation that the rock_band variable withal exists and that information technology even so contains the original string of four names.

Looks like you can use multiple consignment and separate() to excerpt the scores from the results.txt file.

Just you lot need more than i top score

As soon equally the top score appears, people start to wonder what the second and third highest scores are:

image with no caption

It seems that the organizers didn't tell you everything you needed to know. The competition doesn't just award a prize for the winner, but as well honors those surfers in second and 3rd place.

Our program currently iterates through each of the lines in the results.txt file and works out the highest score. But what information technology actually needs to do is keep rails of the top iii scores , perhaps in 3 split variables:

image with no caption

Keeping runway of three scores makes the code more complex

And then how will you go along rails of the extra scores? Y'all could do something like this:

image with no caption

You tin run across that there's a lot more logic here, because the program needs to "think" a scrap more. Unfortunately, turning this logic into lawmaking volition make the programme longer and harder to change in the future. And, let's be honest, it's somewhat more difficult to empathize what'due south actually going on with the logic equally shown here.

How could y'all make this simpler?

An ordered list makes code much simpler

If you had some manner of reading the information from the file and then producing an ordered copy of the information, the program would be a lot simpler to write. Ordering data within a program is known every bit "sorting:"

image with no caption

But how practise you order, or sort , your data? What happens to the original data in the file? Does it remain unsorted or is it sorted, besides? Can the information fifty-fifty be sorted on disk and, if so, does this make things easier, faster, or slower?

Sorting sounds catchy... is in that location a "all-time" way?

Sorting is easier in memory

If you are writing a program that is going to deal with a lot of data, you demand to decide where y'all need to proceed that information while the plan works with information technology. Most of the fourth dimension, you will have ii choices:

  1. Keep the data in files on the disk.

    If you have a very large amount of data, the obvious place to put information technology is on disk. Computers tin can store far more information on deejay than they can in memory. Deejay storage is persistent : if you yank the power cord, the computer doesn't forget the information written on the deejay. Simply at that place is one real problem with manipulating data on deejay: it tin be very slow .

  2. Keep the data in retentiveness.

    Data is much quicker to access and change if information technology's stored in the computer's memory. Only, it's non persistent: data in retention disappears when your program exits, or when the calculator is switched off (unless you lot remember to salve information technology to a file, in which case it becomes persistent).

Keep the data in retentiveness

If you want to sort a lot of information, you will demand to shuffle data effectually quite a lot. This is much faster in retention than on disk.

Of course, earlier y'all sort the data, you need to read it into memory, perhaps into a large number of individual variables:

image with no caption

Brain Power

You are going to take a problem if you endeavor to move all those lines of data into the computer's retentiveness. What type of problem do you recollect yous'll take?

You tin't use a separate variable for each line of data

Programming languages use variables to give yous access to information in memory. So if you are going to shop the information from the results.txt file in memory, it makes sense that you'll need to use lots of variables to access all the data, right?

But how many variables do y'all demand?

Imagine the file just had 3 scores in information technology. You could write a program that read each of the lines from the file and stored them in variables chosen first_score, second_score, and third_score:

image with no caption

But what if there were four scores in the file? Or v? Even worse, what if there were ten,000 scores? You'd before long run out of variable names and (peradventure) memory in your computer, not to mention the wear and tear on your fingers.

image with no caption

Sometimes, you demand to bargain with a whole packet of data, all at once. To do that, most languages give yous the array .

An array lets you lot manage a whole train of data

And so far, y'all've used variables to store only a unmarried piece of information . But sometimes, y'all want to refer to a whole bunch of data all at in one case. For that, you need a new type of variable: the array .

An array is a "drove variable" or information structure . It'southward designed to grouping a whole bunch of data items together in i place and give them a name.

Recollect of an array every bit a data train. Each car in the train is called an array chemical element and can store a single piece of data. If you desire to shop a number in one chemical element and a string in another, you can.

image with no caption

You might remember that every bit you are storing all of that information in an array, you lot still might need variables for each of the items information technology stores. But this is not the instance. An array is itself just another variable , and yous can requite it its own variable proper name:

image with no caption

Even though an array contains a whole bunch of data items, the array itself is a single variable , which simply so happens to comprise a collection of information. Once your information is in an array, you tin treat the array just similar whatever other variable.

So how practice y'all apply arrays?

Python gives you arrays with lists

Sometimes, different programming languages accept different names for roughly the same affair. For example, in Python near programmers think array when they are actually using a Python list . For our purposes, recall of Python lists and arrays as the essentially aforementioned matter.

Notation

Python coders typically employ the word "array" to more than correctly refer to a list that contains simply data of i blazon, like a agglomeration of strings or a bunch of numbers. And Python comes with a built-in applied science called "assortment" for simply that purpose. However, as lists are very similar and much more flexible, we prefer to utilize them, so you don't need to worry about this distinction for now.

You create an array in Python like this:

image with no caption

You can read private pieces of data from inside the assortment using an index , just like yous read individual characters from inside a string .

As with strings, the index for the first piece of information is 0. The 2d slice has index 1, and and then on.

Arrays tin can be extended

But what if you need to add together some extra data to an assortment? Similar strings, arrays come with a bunch of built-in methods. Utilize the append() method to add an actress element onto the end of the array:

image with no caption

Sort the assortment before displaying the results

The array is storing the scores in the order they were read from the file. Withal, y'all withal demand to sort them so that the highest scores announced first .

You could sort the assortment past comparing each of the elements with each of the other elements, and then swap whatever that are in the wrong order.

image with no caption

image with no caption

Arrays in Python have a whole host of methods that make many tasks easier.

Let's see which ones might help.

Brain Barbell

Tin can you work out which ii methods you need to utilise to allow you lot to sort the data in the order that yous need?

Brain Barbell Solution

You were to work out which two methods you needed to employ to permit y'all to sort the data in the order that you needed.

The sort() and contrary() methods look the most useful. Yous demand to use contrary() after you sort() the data, considering the default ordering used by sort() is everyman-to-highest , the opposite of what yous need.

Sort the scores from highest to lowest

You now need to add together the two method calls into your lawmaking that will sort the assortment. The lines need to become betwixt the code that reads the data into the list and before the code that displays the first three elements:

image with no caption

image with no caption

Geek $.25

Information technology was very elementary to sort an array of data using only 2 lines of code. But information technology turns out you tin exercise even better than that if yous use an selection with the sort() method. Instead of using these ii lines:

scores.sort() scores.reverse()

you could have used just one, which gives the same result: scores.sort(contrary = True)

And the winner is...?

It's time for the accolade ceremony.

The prizes are lined upward and the scores are on the scoreboard. There's just one problem.

Nobody knows which surfer got which score.

image with no caption

You somehow forgot the surfer names

With your rush to catch some waves earlier the light is gone, y'all forgot about the other piece of data stored in the results.txt file: the name of each surfer.

Without the names, you can't perhaps know which score goes with which proper noun, so the scoreboard is but half-complete.

The trouble is, your array stores one data detail in each chemical element, not two. Looks similar you still have your piece of work cut out for y'all. There'll be no catching waves until this issue is resolved.

image with no caption

How do you retrieve you lot can retrieve the names and the scores for each surfer in the contest?

One time yous've thought most this problem, plough over to Chapter 5 and run across if you can resolve this effect.

You've got Chapter 4 under your belt. Let's look back at what y'all've learned in this affiliate:

Programming Tools

* files - reading data stored on deejay

* arrays - a collection variable that holds multiple data items that can exist accessed past index

* sorting - arranging a drove in a specific lodge

Python Tools

* open() - open up a file for processing

* close() - shut a file

* for - iterate over something

* string.split() - cut a string into multiple parts

* [] - the array index operator

* array.append() - add an detail to the end of an array

* array.sort() - sort an array, everyman-to-highest

* array.reverse() - change the order of an array by reversing information technology

daigletheryiewer92.blogspot.com

Source: https://www.oreilly.com/library/view/head-first-programming/9780596806682/ch04.html

0 Response to "Read Into the Program From the Data Text File and Assign to an Array"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel