This is the solution of the two previous
exercises, and so let's start with the first one.
I'm going to create a new file. So a New, Python
File, actually, let's name it compute_average_from_list,
and say from_file to make it different
from the previous one. So first, I'm going to need
to read a list of numbers from the file. Let's use
the file_test. So we have six numbers here, and I'm
going to compute the average from those. So we're
going to start with, so the with keyword open, and
then file_test, gonna use the relative path, r
because we want to read from the file, and then as
f:, okay, and I'm going to do for line in f, okay,
and I'm going to put each number that I get from
each line, okay, inside a new list, and I'm going
to create here, a number_list. So an empty list.
And here, I am going to do number_list.append,
to add a new element to that list, line.rstrip
without the backslash character here. The
thing is that this is going to add a string, okay,
to the list, what I want is to add a number. So I
could cast it to an integer, okay, or float number
as you want. And we've to cast it to float, like
this. So this is going to add a float number in
the number_list. And then after this, I have
actually, I have my number_list with all the
numbers here. So let's print actually a number_list,
and what I'm going to do now is to compute
the average. So to compute the average, I'm going
to go back to that file here compute_list_average,
and instead of just copying this function
inside that file, I'm simply going to, to copy
this function inside the my_computations module.
Okay, I add the function here. So now I have two
functions in my module. And then in this file,
where I want to compute the average, I'm going to
add a new line at the top, which is from my_computations
import compute_list_average. And
as you can see here, also, when it's gray on PyCharm,
it means that you are not using the imports,
okay, and it's going to turn into a different
color when you start to use it. And I'm going to
do print(compute_list_average). So still using
the auto completion from the number_lists, so I
don't need to write the function again, here, I
don't need to copy it, I just need to import it
from the module. And now if I run this, so run
with compute_average_from_list_from_file, you can see
we have the list here with the six values, and
then the average right after that. Okay, and
this is the solution for the exercise number two.
So I have created here a list that you are going
to be able to download unordered_cities.txt. So
for once I have added an extension, okay, so you
can add an extension or not, it doesn't really
matter. Okay, so 10 cities, 10 Random cities that
are not sorted alphabetically, as you can see. So
I'm going to create a new Python file, let's say
sort_cities. Okay. So I'm going to read first all
the cities here and put them in a list. So city_list
is an empty list. I'm going to the with open
unordered_cities,
I can use the auto completion here with
r because we want to read, okay, from the file as
f and when to do for line in f. So as you can see,
that's always the same structure, okay. city_list.append,
and then I'm going to do line.rstrip
backslash n. So again, that's always the
same thing here. And now so this is a string I
added to a list, which contains strings, so the
datatype is correct. And now I'm going to sort the
list I'm going to create a new list named sorted_city_list
is equal to and then sorted function
with the city_list. This is going to return the
sorted version of that list and I put that in a
new variable or a new list here, sorted_city_list.
If you want, you can also print the intermediate
result to see how is it working. And I'm going to
create a new file or just write to a new file,
that new list I have created here. So with open
and we're going to create a file named unordered_cities.txt,
okay, and I'm going to use w for
write get as f. And now what I'm going to do is
I'm going to put each cities okay from this new
list, okay, in a different line. So I'm going to
do for city in sorted_city_list. So
make sure you do a follow up on that list and not
that list. Otherwise, you're not going to change
the order in the file. So for each city I have,
I'm going to write it in a new line. So I'm going
to do f.write, city, and if I do that, it's
just going to add all the cities together in the
same line. So I'm going to do plus quotes, quotes
and backslash n to add a new line character. This
way I can write each element from this array from
this list on a new line. That's also a very common
structure, you're going to use many times and well
that's it, I'm simply going to do print down Okay,
so it's always nice to have down or okay, at the
end of a Python script, so you can know when this
is finished, okay. And now ran. So cities, you can
see done and well you can see on the left, we have
so we have an ordered series here. And we have
other cities, okay, this is a new text file, which
also contains density, but these time
alphabetically ordered.