1
00:00:00,240 --> 00:00:05,220
Once you've unzipped and opened up the starting file inside PyCharm,

2
00:00:05,610 --> 00:00:06,689
this is what you'd see.

3
00:00:06,780 --> 00:00:11,610
There is a CSV file that contains each of the letters and the code word,

4
00:00:11,970 --> 00:00:16,379
and there's also our main.py with some of the example code that I showed you

5
00:00:16,379 --> 00:00:18,480
in the last lesson. Now,

6
00:00:18,510 --> 00:00:23,400
if you don't know how to complete this challenge, especially step 1,

7
00:00:23,760 --> 00:00:28,760
I really urge you to go through this example code and just go through each of

8
00:00:29,730 --> 00:00:33,480
the different types of loops and see what it is that you get when you print the

9
00:00:33,480 --> 00:00:37,170
key or the value. And in this case, when you print the index

10
00:00:37,200 --> 00:00:41,550
or when you print the row. And then see what happens when you get hold of some of

11
00:00:41,550 --> 00:00:46,500
the items in the row. And seeing how maybe the row.student or the row.

12
00:00:46,500 --> 00:00:49,560
score works. Once you fully understood this,

13
00:00:49,590 --> 00:00:53,250
then it should be relatively easy to complete step 1.

14
00:00:53,880 --> 00:00:58,110
So essentially the goal is that we want to create a dictionary that looks like

15
00:00:58,110 --> 00:00:59,760
this from the CSV.

16
00:01:00,360 --> 00:01:04,379
The first thing we have to do is to go ahead and import pandas.

17
00:01:06,270 --> 00:01:09,540
And if you need to install it, there should be a squiggly line

18
00:01:09,570 --> 00:01:10,680
and then you can click on it

19
00:01:10,800 --> 00:01:14,550
and the light bulb will help you install the pandas module.

20
00:01:14,940 --> 00:01:17,400
But once we've gotten hold of the pandas,

21
00:01:17,430 --> 00:01:22,350
we can go ahead and use it to read CSV from our file

22
00:01:22,350 --> 00:01:27,270
which is this file. It's under the file path of just nato_phonetic_

23
00:01:27,270 --> 00:01:31,470
alphabet.csv. So it will save this as our data

24
00:01:31,500 --> 00:01:34,890
which is basically going to be our data frame. Now,

25
00:01:34,890 --> 00:01:37,440
once we've got hold of our data,

26
00:01:37,560 --> 00:01:41,010
then you can see that if we print this out,

27
00:01:41,880 --> 00:01:43,800
it looks like this.

28
00:01:44,250 --> 00:01:49,250
But if we use that method, to_dict, and then we try to print it out,

29
00:01:49,950 --> 00:01:54,060
you can see that it's not organized the dictionary in the format that we want it

30
00:01:54,060 --> 00:01:57,780
to, which is to have the letter as the key,

31
00:01:57,990 --> 00:02:02,730
the corresponding code as the value. In order to complete step 1

32
00:02:02,820 --> 00:02:06,300
as I mentioned, we're going to have to use dictionary comprehension.

33
00:02:06,750 --> 00:02:11,750
And the method is going to be using the iterrows to iterate through each of the

34
00:02:12,690 --> 00:02:17,340
rows inside that data frame. So I'm going to copy that line here,

35
00:02:17,580 --> 00:02:20,490
and I'm going to paste it in here.

36
00:02:21,150 --> 00:02:25,860
Now, this is the format, so let's go ahead and replace each of the keywords.

37
00:02:26,250 --> 00:02:29,250
Our data frame, in our case, is just called data.

38
00:02:30,000 --> 00:02:33,900
And then we're going to say data iterate through each of the rows,

39
00:02:34,170 --> 00:02:37,020
and then for each of the index and the row,

40
00:02:37,230 --> 00:02:39,270
we're going to do something with it.

41
00:02:39,900 --> 00:02:44,900
Now the new key is going to be the row.letter and the new value is going to

42
00:02:49,200 --> 00:02:54,180
come from that row as well and it's going to be row.code.

43
00:02:55,020 --> 00:02:58,650
So this is the code that will create our new dictionary

44
00:02:59,110 --> 00:03:03,670
and I'm going to call this our phonetic_dictionary.

45
00:03:04,840 --> 00:03:06,580
Now, if I print that out,

46
00:03:07,630 --> 00:03:12,370
you can see that this is the straight up data.to_dict

47
00:03:12,790 --> 00:03:16,810
and then the second line is the one where we've actually formatted it

48
00:03:17,140 --> 00:03:20,980
using what we've learned about dictionary comprehension.

49
00:03:21,730 --> 00:03:25,570
Once we've gotten hold of this phonetic dictionary,

50
00:03:25,630 --> 00:03:30,630
then to do step 2 is incredibly easy because all we have to do is to create

51
00:03:31,120 --> 00:03:35,200
some sort of inputs, ask the user to enter a word,

52
00:03:35,560 --> 00:03:39,220
and then we can save this input to a variable. Now,

53
00:03:39,250 --> 00:03:41,320
once they've entered the word,

54
00:03:41,530 --> 00:03:46,530
we have to check it against each of the keys inside this phonetic dictionary.

55
00:03:47,410 --> 00:03:50,470
So notice how each of the keys all uppercased.

56
00:03:50,800 --> 00:03:55,480
So we're going to have to change whatever it is that the user has inputted all

57
00:03:55,510 --> 00:04:00,220
to upper. So that way, if they entered a lowercase or an uppercase,

58
00:04:00,220 --> 00:04:03,750
it doesn't really matter because we're going to convert the whole string to

59
00:04:03,760 --> 00:04:08,020
uppercase. And then we're going to use our list comprehension.

60
00:04:08,890 --> 00:04:13,890
The way we create our list comprehension is going to be new item for item in

61
00:04:14,890 --> 00:04:16,570
list. So in this case,

62
00:04:16,570 --> 00:04:20,529
our list or the thing that we're going to iterate through is going to be our

63
00:04:20,529 --> 00:04:21,363
word.

64
00:04:21,640 --> 00:04:25,540
And then we're going to go through each of the letters in the word.

65
00:04:26,770 --> 00:04:29,170
And once we have each of the letters,

66
00:04:29,230 --> 00:04:33,640
we're going to go through our phonetic dictionary, this one right here,

67
00:04:33,730 --> 00:04:38,440
and pick out the value that corresponds to the particular letter that we're

68
00:04:38,440 --> 00:04:39,310
iterating on.

69
00:04:39,550 --> 00:04:42,970
So we're going to use the square brackets and then pass in the letter like this.

70
00:04:43,600 --> 00:04:48,600
Now we've created our output list and we can go ahead and print it out.

71
00:04:51,310 --> 00:04:56,310
Now let's go ahead and comment out this line and run our code.

72
00:04:58,120 --> 00:05:01,300
So it's going to ask me to enter a word, I'm going to enter my name.

73
00:05:01,660 --> 00:05:06,660
And now it's managed to convert that into a list of phonetic alphabets that

74
00:05:07,450 --> 00:05:10,300
corresponds to each of the letters in that word.

75
00:05:11,200 --> 00:05:15,010
Did you manage to complete this challenge? And if not,

76
00:05:15,220 --> 00:05:16,930
which part did you stumble on?

77
00:05:17,320 --> 00:05:21,370
Was it the part where you were using the iterrows? If so,

78
00:05:21,370 --> 00:05:25,720
then go back to the starting file for this project and go through each of the

79
00:05:25,720 --> 00:05:29,560
different ways that we're looping through it and use print statements to really

80
00:05:29,560 --> 00:05:34,360
understand how the loop is actually going through and looking through the data.

81
00:05:35,080 --> 00:05:35,470
Now,

82
00:05:35,470 --> 00:05:40,240
if on the other hand, it was the dictionary comprehension or the list

83
00:05:40,240 --> 00:05:41,073
comprehension,

84
00:05:41,320 --> 00:05:44,950
then be sure to go back to the relevant lesson and just make sure that you

85
00:05:44,950 --> 00:05:48,130
review it and you write out the code in the lesson for yourself

86
00:05:48,400 --> 00:05:52,690
just to be sure that you understand what's going on before you continue to the

87
00:05:52,690 --> 00:05:53,230
next day.

