1 00:00:00,000 --> 00:00:02,340 This is the solution of the two previous 2 00:00:02,370 --> 00:00:05,820 exercises, and so let's start with the first one. 3 00:00:05,940 --> 00:00:08,940 I'm going to create a new file. So a New, Python 4 00:00:08,940 --> 00:00:16,475 File, actually, let's name it compute_average_from_list, 5 00:00:16,475 --> 00:00:19,590 and say from_file to make it different 6 00:00:19,590 --> 00:00:22,980 from the previous one. So first, I'm going to need 7 00:00:23,010 --> 00:00:26,640 to read a list of numbers from the file. Let's use 8 00:00:26,640 --> 00:00:29,700 the file_test. So we have six numbers here, and I'm 9 00:00:29,700 --> 00:00:33,480 going to compute the average from those. So we're 10 00:00:33,480 --> 00:00:37,500 going to start with, so the with keyword open, and 11 00:00:37,500 --> 00:00:42,810 then file_test, gonna use the relative path, r 12 00:00:42,810 --> 00:00:46,200 because we want to read from the file, and then as 13 00:00:46,230 --> 00:00:52,680 f:, okay, and I'm going to do for line in f, okay, 14 00:00:52,680 --> 00:00:55,500 and I'm going to put each number that I get from 15 00:00:55,530 --> 00:00:59,340 each line, okay, inside a new list, and I'm going 16 00:00:59,340 --> 00:01:03,270 to create here, a number_list. So an empty list. 17 00:01:03,780 --> 00:01:08,670 And here, I am going to do number_list.append, 18 00:01:09,210 --> 00:01:14,525 to add a new element to that list, line.rstrip 19 00:01:14,525 --> 00:01:20,400 without the backslash character here. The 20 00:01:20,400 --> 00:01:23,160 thing is that this is going to add a string, okay, 21 00:01:23,190 --> 00:01:26,610 to the list, what I want is to add a number. So I 22 00:01:26,610 --> 00:01:30,840 could cast it to an integer, okay, or float number 23 00:01:30,900 --> 00:01:34,170 as you want. And we've to cast it to float, like 24 00:01:34,170 --> 00:01:37,680 this. So this is going to add a float number in 25 00:01:37,710 --> 00:01:42,480 the number_list. And then after this, I have 26 00:01:43,230 --> 00:01:46,620 actually, I have my number_list with all the 27 00:01:46,620 --> 00:01:49,966 numbers here. So let's print actually a number_list, 28 00:01:49,966 --> 00:01:53,430 and what I'm going to do now is to compute 29 00:01:53,460 --> 00:01:56,490 the average. So to compute the average, I'm going 30 00:01:56,490 --> 00:02:00,721 to go back to that file here compute_list_average, 31 00:02:00,721 --> 00:02:04,080 and instead of just copying this function 32 00:02:04,440 --> 00:02:09,030 inside that file, I'm simply going to, to copy 33 00:02:09,030 --> 00:02:12,870 this function inside the my_computations module. 34 00:02:13,710 --> 00:02:16,830 Okay, I add the function here. So now I have two 35 00:02:16,830 --> 00:02:21,210 functions in my module. And then in this file, 36 00:02:21,780 --> 00:02:24,870 where I want to compute the average, I'm going to 37 00:02:24,870 --> 00:02:30,044 add a new line at the top, which is from my_computations 38 00:02:30,044 --> 00:02:35,610 import compute_list_average. And 39 00:02:35,610 --> 00:02:38,957 as you can see here, also, when it's gray on PyCharm, 40 00:02:38,957 --> 00:02:43,110 it means that you are not using the imports, 41 00:02:43,140 --> 00:02:45,630 okay, and it's going to turn into a different 42 00:02:45,630 --> 00:02:48,210 color when you start to use it. And I'm going to 43 00:02:48,210 --> 00:02:53,850 do print(compute_list_average). So still using 44 00:02:53,910 --> 00:02:58,110 the auto completion from the number_lists, so I 45 00:02:58,110 --> 00:03:01,350 don't need to write the function again, here, I 46 00:03:01,350 --> 00:03:03,720 don't need to copy it, I just need to import it 47 00:03:03,720 --> 00:03:07,650 from the module. And now if I run this, so run 48 00:03:08,310 --> 00:03:11,730 with compute_average_from_list_from_file, you can see 49 00:03:12,360 --> 00:03:17,340 we have the list here with the six values, and 50 00:03:17,340 --> 00:03:22,980 then the average right after that. Okay, and 51 00:03:22,980 --> 00:03:25,710 this is the solution for the exercise number two. 52 00:03:25,890 --> 00:03:28,560 So I have created here a list that you are going 53 00:03:28,560 --> 00:03:32,910 to be able to download unordered_cities.txt. So 54 00:03:32,910 --> 00:03:35,730 for once I have added an extension, okay, so you 55 00:03:35,730 --> 00:03:38,190 can add an extension or not, it doesn't really 56 00:03:38,190 --> 00:03:42,600 matter. Okay, so 10 cities, 10 Random cities that 57 00:03:42,600 --> 00:03:45,930 are not sorted alphabetically, as you can see. So 58 00:03:45,930 --> 00:03:49,920 I'm going to create a new Python file, let's say 59 00:03:49,920 --> 00:03:55,350 sort_cities. Okay. So I'm going to read first all 60 00:03:55,350 --> 00:03:59,900 the cities here and put them in a list. So city_list 61 00:03:59,900 --> 00:04:04,830 is an empty list. I'm going to the with open 62 00:04:05,700 --> 00:04:08,853 unordered_cities, 63 00:04:08,853 --> 00:04:11,370 I can use the auto completion here with 64 00:04:11,610 --> 00:04:15,270 r because we want to read, okay, from the file as 65 00:04:15,300 --> 00:04:19,860 f and when to do for line in f. So as you can see, 66 00:04:19,860 --> 00:04:25,175 that's always the same structure, okay. city_list.append, 67 00:04:25,175 --> 00:04:30,140 and then I'm going to do line.rstrip 68 00:04:30,140 --> 00:04:33,780 backslash n. So again, that's always the 69 00:04:33,780 --> 00:04:37,230 same thing here. And now so this is a string I 70 00:04:37,230 --> 00:04:41,970 added to a list, which contains strings, so the 71 00:04:41,970 --> 00:04:45,570 datatype is correct. And now I'm going to sort the 72 00:04:45,570 --> 00:04:51,885 list I'm going to create a new list named sorted_city_list 73 00:04:51,885 --> 00:04:54,570 is equal to and then sorted function 74 00:04:55,200 --> 00:04:59,010 with the city_list. This is going to return the 75 00:04:59,010 --> 00:05:02,520 sorted version of that list and I put that in a 76 00:05:02,520 --> 00:05:06,750 new variable or a new list here, sorted_city_list. 77 00:05:07,200 --> 00:05:10,620 If you want, you can also print the intermediate 78 00:05:10,620 --> 00:05:14,550 result to see how is it working. And I'm going to 79 00:05:14,700 --> 00:05:18,210 create a new file or just write to a new file, 80 00:05:18,540 --> 00:05:22,410 that new list I have created here. So with open 81 00:05:23,490 --> 00:05:30,615 and we're going to create a file named unordered_cities.txt, 82 00:05:30,615 --> 00:05:33,690 okay, and I'm going to use w for 83 00:05:33,720 --> 00:05:37,620 write get as f. And now what I'm going to do is 84 00:05:37,620 --> 00:05:41,760 I'm going to put each cities okay from this new 85 00:05:41,760 --> 00:05:44,640 list, okay, in a different line. So I'm going to 86 00:05:44,640 --> 00:05:52,650 do for city in sorted_city_list. So 87 00:05:52,650 --> 00:05:55,860 make sure you do a follow up on that list and not 88 00:05:55,890 --> 00:05:58,200 that list. Otherwise, you're not going to change 89 00:05:58,440 --> 00:06:02,400 the order in the file. So for each city I have, 90 00:06:02,460 --> 00:06:05,670 I'm going to write it in a new line. So I'm going 91 00:06:05,670 --> 00:06:11,610 to do f.write, city, and if I do that, it's 92 00:06:11,610 --> 00:06:14,310 just going to add all the cities together in the 93 00:06:14,310 --> 00:06:17,910 same line. So I'm going to do plus quotes, quotes 94 00:06:17,940 --> 00:06:21,960 and backslash n to add a new line character. This 95 00:06:21,960 --> 00:06:25,410 way I can write each element from this array from 96 00:06:25,410 --> 00:06:29,940 this list on a new line. That's also a very common 97 00:06:29,940 --> 00:06:34,440 structure, you're going to use many times and well 98 00:06:34,470 --> 00:06:38,550 that's it, I'm simply going to do print down Okay, 99 00:06:38,550 --> 00:06:42,210 so it's always nice to have down or okay, at the 100 00:06:42,210 --> 00:06:45,120 end of a Python script, so you can know when this 101 00:06:45,120 --> 00:06:51,210 is finished, okay. And now ran. So cities, you can 102 00:06:51,210 --> 00:06:55,410 see done and well you can see on the left, we have 103 00:06:55,410 --> 00:06:59,730 so we have an ordered series here. And we have 104 00:06:59,940 --> 00:07:03,360 other cities, okay, this is a new text file, which 105 00:07:03,390 --> 00:07:06,930 also contains density, but these time 106 00:07:07,140 --> 00:07:09,824 alphabetically ordered.