WEBVTT

0
00:00.240 --> 00:05.220
Once you've unzipped and opened up the starting file inside PyCharm,

1
00:05.610 --> 00:06.690
this is what you'd see.

2
00:06.780 --> 00:11.610
There is a CSV file that contains each of the letters and the code word,

3
00:11.970 --> 00:16.380
and there's also our main.py with some of the example code that I showed you

4
00:16.380 --> 00:18.480
in the last lesson. Now,

5
00:18.510 --> 00:23.400
if you don't know how to complete this challenge, especially step 1,

6
00:23.760 --> 00:28.760
I really urge you to go through this example code and just go through each of

7
00:29.730 --> 00:33.480
the different types of loops and see what it is that you get when you print the

8
00:33.480 --> 00:37.170
key or the value. And in this case, when you print the index

9
00:37.200 --> 00:41.550
or when you print the row. And then see what happens when you get hold of some of

10
00:41.550 --> 00:46.500
the items in the row. And seeing how maybe the row.student or the row.

11
00:46.500 --> 00:49.560
score works. Once you fully understood this,

12
00:49.590 --> 00:53.250
then it should be relatively easy to complete step 1.

13
00:53.880 --> 00:58.110
So essentially the goal is that we want to create a dictionary that looks like

14
00:58.110 --> 00:59.760
this from the CSV.

15
01:00.360 --> 01:04.380
The first thing we have to do is to go ahead and import pandas.

16
01:06.270 --> 01:09.540
And if you need to install it, there should be a squiggly line

17
01:09.570 --> 01:10.680
and then you can click on it

18
01:10.800 --> 01:14.550
and the light bulb will help you install the pandas module.

19
01:14.940 --> 01:17.400
But once we've gotten hold of the pandas,

20
01:17.430 --> 01:22.350
we can go ahead and use it to read CSV from our file

21
01:22.350 --> 01:27.270
which is this file. It's under the file path of just nato_phonetic_

22
01:27.270 --> 01:31.470
alphabet.csv. So it will save this as our data

23
01:31.500 --> 01:34.890
which is basically going to be our data frame. Now,

24
01:34.890 --> 01:37.440
once we've got hold of our data,

25
01:37.560 --> 01:41.010
then you can see that if we print this out,

26
01:41.880 --> 01:43.800
it looks like this.

27
01:44.250 --> 01:49.250
But if we use that method, to_dict, and then we try to print it out,

28
01:49.950 --> 01:54.060
you can see that it's not organized the dictionary in the format that we want it

29
01:54.060 --> 01:57.780
to, which is to have the letter as the key,

30
01:57.990 --> 02:02.730
the corresponding code as the value. In order to complete step 1

31
02:02.820 --> 02:06.300
as I mentioned, we're going to have to use dictionary comprehension.

32
02:06.750 --> 02:11.750
And the method is going to be using the iterrows to iterate through each of the

33
02:12.690 --> 02:17.340
rows inside that data frame. So I'm going to copy that line here,

34
02:17.580 --> 02:20.490
and I'm going to paste it in here.

35
02:21.150 --> 02:25.860
Now, this is the format, so let's go ahead and replace each of the keywords.

36
02:26.250 --> 02:29.250
Our data frame, in our case, is just called data.

37
02:30.000 --> 02:33.900
And then we're going to say data iterate through each of the rows,

38
02:34.170 --> 02:37.020
and then for each of the index and the row,

39
02:37.230 --> 02:39.270
we're going to do something with it.

40
02:39.900 --> 02:44.900
Now the new key is going to be the row.letter and the new value is going to

41
02:49.200 --> 02:54.180
come from that row as well and it's going to be row.code.

42
02:55.020 --> 02:58.650
So this is the code that will create our new dictionary

43
02:59.110 --> 03:03.670
and I'm going to call this our phonetic_dictionary.

44
03:04.840 --> 03:06.580
Now, if I print that out,

45
03:07.630 --> 03:12.370
you can see that this is the straight up data.to_dict

46
03:12.790 --> 03:16.810
and then the second line is the one where we've actually formatted it

47
03:17.140 --> 03:20.980
using what we've learned about dictionary comprehension.

48
03:21.730 --> 03:25.570
Once we've gotten hold of this phonetic dictionary,

49
03:25.630 --> 03:30.630
then to do step 2 is incredibly easy because all we have to do is to create

50
03:31.120 --> 03:35.200
some sort of inputs, ask the user to enter a word,

51
03:35.560 --> 03:39.220
and then we can save this input to a variable. Now,

52
03:39.250 --> 03:41.320
once they've entered the word,

53
03:41.530 --> 03:46.530
we have to check it against each of the keys inside this phonetic dictionary.

54
03:47.410 --> 03:50.470
So notice how each of the keys all uppercased.

55
03:50.800 --> 03:55.480
So we're going to have to change whatever it is that the user has inputted all

56
03:55.510 --> 04:00.220
to upper. So that way, if they entered a lowercase or an uppercase,

57
04:00.220 --> 04:03.750
it doesn't really matter because we're going to convert the whole string to

58
04:03.760 --> 04:08.020
uppercase. And then we're going to use our list comprehension.

59
04:08.890 --> 04:13.890
The way we create our list comprehension is going to be new item for item in

60
04:14.890 --> 04:16.570
list. So in this case,

61
04:16.570 --> 04:20.530
our list or the thing that we're going to iterate through is going to be our

62
04:20.530 --> 04:21.363
word.

63
04:21.640 --> 04:25.540
And then we're going to go through each of the letters in the word.

64
04:26.770 --> 04:29.170
And once we have each of the letters,

65
04:29.230 --> 04:33.640
we're going to go through our phonetic dictionary, this one right here,

66
04:33.730 --> 04:38.440
and pick out the value that corresponds to the particular letter that we're

67
04:38.440 --> 04:39.310
iterating on.

68
04:39.550 --> 04:42.970
So we're going to use the square brackets and then pass in the letter like this.

69
04:43.600 --> 04:48.600
Now we've created our output list and we can go ahead and print it out.

70
04:51.310 --> 04:56.310
Now let's go ahead and comment out this line and run our code.

71
04:58.120 --> 05:01.300
So it's going to ask me to enter a word, I'm going to enter my name.

72
05:01.660 --> 05:06.660
And now it's managed to convert that into a list of phonetic alphabets that

73
05:07.450 --> 05:10.300
corresponds to each of the letters in that word.

74
05:11.200 --> 05:15.010
Did you manage to complete this challenge? And if not,

75
05:15.220 --> 05:16.930
which part did you stumble on?

76
05:17.320 --> 05:21.370
Was it the part where you were using the iterrows? If so,

77
05:21.370 --> 05:25.720
then go back to the starting file for this project and go through each of the

78
05:25.720 --> 05:29.560
different ways that we're looping through it and use print statements to really

79
05:29.560 --> 05:34.360
understand how the loop is actually going through and looking through the data.

80
05:35.080 --> 05:35.470
Now,

81
05:35.470 --> 05:40.240
if on the other hand, it was the dictionary comprehension or the list

82
05:40.240 --> 05:41.073
comprehension,

83
05:41.320 --> 05:44.950
then be sure to go back to the relevant lesson and just make sure that you

84
05:44.950 --> 05:48.130
review it and you write out the code in the lesson for yourself

85
05:48.400 --> 05:52.690
just to be sure that you understand what's going on before you continue to the

86
05:52.690 --> 05:53.230
next day.