1
00:00:00,390 --> 00:00:04,980
All right. So we're now onto our final list comprehension exercise of the day.

2
00:00:05,580 --> 00:00:10,260
And just a word of warning, this exercise is quite hard.

3
00:00:10,710 --> 00:00:15,710
I decided that the previous two were a little bit easier and you've now kind of

4
00:00:15,900 --> 00:00:20,430
warmed up and you've gotten going, it's time to test your knowledge on a number

5
00:00:20,430 --> 00:00:21,330
of things that you've learned.

6
00:00:22,200 --> 00:00:26,970
The first thing I want you to do is take a look at these two files and look at

7
00:00:26,970 --> 00:00:29,340
the structure of the data in there,

8
00:00:29,970 --> 00:00:34,650
and then have a read through the instruction and see if you can complete this

9
00:00:34,680 --> 00:00:35,513
challenge.

10
00:00:35,700 --> 00:00:40,700
This is the output that you are expecting when your new list called result is

11
00:00:40,830 --> 00:00:44,100
printed. Pause the video and give this a go.

12
00:00:47,670 --> 00:00:48,030
All right.

13
00:00:48,030 --> 00:00:53,030
So we know that we've got two text files with a new piece of data on each line.

14
00:00:54,900 --> 00:00:55,440
Essentially,

15
00:00:55,440 --> 00:01:00,390
what we're trying to do here is we're trying to take the data in file1.

16
00:01:00,390 --> 00:01:04,890
txt and compare it against the data in file2.txt,

17
00:01:05,310 --> 00:01:09,750
and then we're trying to get a hold of the data which exists in both of these

18
00:01:09,750 --> 00:01:14,670
files. So if file1 contained one, two, three, file2 contained two,

19
00:01:14,670 --> 00:01:15,503
three, four,

20
00:01:15,690 --> 00:01:20,250
then the result should be a list that contains the overlapping numbers.

21
00:01:20,550 --> 00:01:24,750
Two and three exists in both files. Now,

22
00:01:24,750 --> 00:01:29,370
in order to pass the test, the result must be a list that contains integers,

23
00:01:29,430 --> 00:01:33,180
so whole numbers, not strings. So when you print it out,

24
00:01:33,300 --> 00:01:37,470
if you see the numbers as they are, then that means they are integers.

25
00:01:37,800 --> 00:01:40,560
But if you see quotation marks around each of the numbers,

26
00:01:40,800 --> 00:01:42,450
then they are strings instead.

27
00:01:42,900 --> 00:01:45,990
And our code checking is not going to allow that to pass.

28
00:01:47,190 --> 00:01:52,190
The first thing we need to do is to use our read lines method that we've used

29
00:01:52,320 --> 00:01:57,320
several times before to read the data from each of these files and create a list

30
00:01:57,780 --> 00:02:00,780
from each line. Let's go ahead and do that.

31
00:02:00,990 --> 00:02:05,430
So I'm going to use my with open and I'm going to tap into my file1.

32
00:02:05,430 --> 00:02:09,000
txt, and I'm going to save this as file1.

33
00:02:09,900 --> 00:02:13,710
And then this file is going to be read,

34
00:02:13,950 --> 00:02:16,800
and I'm going to call file1.readlines,

35
00:02:17,220 --> 00:02:22,220
and then I'm going to save this list to a new variable called file_1_data. And

36
00:02:25,260 --> 00:02:27,510
notice how I've actually made a typo here

37
00:02:27,510 --> 00:02:31,680
cause I want file1.txt instead of just file.text.

38
00:02:32,010 --> 00:02:33,120
It's good that I spotted it.

39
00:02:34,320 --> 00:02:39,320
And then we're going to do the same with file2 and change all of the places

40
00:02:39,960 --> 00:02:40,890
where we have one.

41
00:02:41,520 --> 00:02:45,180
So copying and pasting is always a little bit prone to error,

42
00:02:45,360 --> 00:02:47,490
but hopefully we'll manage this time.

43
00:02:48,270 --> 00:02:53,130
Now that I've got file_1_data and file_2_data which are both lists,

44
00:02:53,490 --> 00:02:58,490
I can now think about how I can compare those two and create my new list

45
00:02:59,440 --> 00:03:01,300
which is going to be called result.

46
00:03:02,410 --> 00:03:07,410
Now result is a new list and I'm going to use my full list of keywords

47
00:03:09,010 --> 00:03:12,670
inside this new list to create the list comprehension.

48
00:03:13,000 --> 00:03:17,500
So new item for item in lists if test,

49
00:03:18,340 --> 00:03:23,020
the list that I'm going to test is actually the first list.

50
00:03:23,590 --> 00:03:27,460
I'm going to take the file_1_data which is a list of numbers,

51
00:03:28,090 --> 00:03:33,090
and then I'm going to go through each of the numbers inside this list,

52
00:03:35,350 --> 00:03:37,300
and then I'm going to do a check.

53
00:03:37,900 --> 00:03:42,070
And the check is where this file2 is going to come into play.

54
00:03:42,640 --> 00:03:44,200
So I'm going to check to see, well,

55
00:03:44,290 --> 00:03:48,850
if this number that I'm looping through exists in,

56
00:03:48,880 --> 00:03:50,350
remember the in keyword,

57
00:03:50,620 --> 00:03:54,010
this file_2_data, this other list,

58
00:03:54,340 --> 00:03:58,870
well then I'm going to include this number inside my new list.

59
00:03:59,860 --> 00:04:04,360
So this number is going to go into the new list. At the moment when I hit run,

60
00:04:04,420 --> 00:04:08,170
you can see them getting a whole bunch of strings. So in addition,

61
00:04:08,200 --> 00:04:13,030
I have to modify this number so that I convert it into an integer.

62
00:04:13,510 --> 00:04:17,650
So I end up with the same output as is expected here.

63
00:04:19,390 --> 00:04:21,940
Did you remember all of the things that we've learned before

64
00:04:21,940 --> 00:04:26,940
like the in and the with open and also the converting to int?

65
00:04:27,790 --> 00:04:31,810
Did you manage to complete this challenge? If you had any trouble at all,

66
00:04:31,960 --> 00:04:34,840
be sure to review the relevant parts, for example,

67
00:04:35,080 --> 00:04:40,060
opening files or reading lines from the files which we covered in previous days.

68
00:04:40,690 --> 00:04:43,540
Alternatively, if it's the list comprehension that's hard,

69
00:04:43,930 --> 00:04:45,400
then be sure to review the list

70
00:04:45,400 --> 00:04:50,020
comprehension lesson in today's course and make sure that you really understand

71
00:04:50,020 --> 00:04:52,600
what's going on in the code before you continue.

