1 00:00:08,750 --> 00:00:09,721 Welcome back. 2 00:00:09,721 --> 00:00:13,391 So let's go over some questions that use dictionary accumulation. 3 00:00:13,391 --> 00:00:17,210 In this question we're provided a string, sentence. 4 00:00:17,210 --> 00:00:21,270 And the question asks us to split the string into a list of words, and then to 5 00:00:21,270 --> 00:00:25,900 create a dictionary that contains each word and the number of times it occurs. 6 00:00:25,900 --> 00:00:28,565 And that dictionary should be named word_counts. 7 00:00:29,610 --> 00:00:32,100 So the first thing that I'm going to do 8 00:00:32,100 --> 00:00:35,170 is I'm going to split our sentence into words. 9 00:00:35,170 --> 00:00:38,661 We can do that by calling the split method on string. 10 00:00:38,661 --> 00:00:44,162 I'll say words = sentence.split(). 11 00:00:45,921 --> 00:00:48,430 And so words is going to be a list of strings. 12 00:00:48,430 --> 00:00:50,272 The first item is going to be The. 13 00:00:50,272 --> 00:00:52,691 The 2nd item is going to be dog. 14 00:00:52,691 --> 00:00:55,008 The 3rd item is going to be chased, and so on. 15 00:00:57,011 --> 00:01:00,023 So then we want to use dictionary accumulation. 16 00:01:00,023 --> 00:01:05,170 And in this question we’re told to use the dictionary word_counts. 17 00:01:05,170 --> 00:01:08,710 So I’m going to name my accumulator variable word_counts. 18 00:01:08,710 --> 00:01:13,742 And I’m going to initialize it as an empty dictionary. 19 00:01:13,742 --> 00:01:16,100 Then, we need to loop through all of the words. 20 00:01:16,100 --> 00:01:22,052 So I'll say for every word in words. 21 00:01:22,052 --> 00:01:28,106 And we want to say, if that word isn't in our dictionary so 22 00:01:28,106 --> 00:01:32,430 far, so if word is not in word_counts. 23 00:01:32,430 --> 00:01:38,314 And if it's not there we initialize its value to 0, 24 00:01:38,314 --> 00:01:42,107 so word_counts[word] = 0. 25 00:01:42,107 --> 00:01:47,484 And then still inside of our for loop, but outside of the if statement, 26 00:01:47,484 --> 00:01:52,338 we're going to increment the value of word_counts[word]. 27 00:01:52,338 --> 00:02:00,075 So word_counts[word] = word_counts[word] + 1. 28 00:02:02,676 --> 00:02:07,791 So let's run our code, and we can see that it produces the correct value. 29 00:02:07,791 --> 00:02:11,001 If I actually print out the value of word_counts. 30 00:02:11,001 --> 00:02:14,248 So if I say print(word_counts). 31 00:02:16,001 --> 00:02:21,860 Then we can see that the word 'The' with a capital T appeared one time. 32 00:02:21,860 --> 00:02:26,393 The word 'the' with a lowercase t appeared three times. 33 00:02:26,393 --> 00:02:28,570 'Into' appeared once. 34 00:02:29,686 --> 00:02:34,297 'Rabbit' appeared twice, and so on. 35 00:02:34,297 --> 00:02:38,847 In this question we're asked to create a dictionary called char_d from 36 00:02:38,847 --> 00:02:41,980 the string stri, so that the key is a character and 37 00:02:41,980 --> 00:02:45,990 its value is how many times that character occurred. 38 00:02:45,990 --> 00:02:49,082 So this is again a straightforward application of the dictionary 39 00:02:49,082 --> 00:02:50,271 accumulation pattern. 40 00:02:50,271 --> 00:02:55,356 So I'm going to name my dictionary accumulator char_d. 41 00:02:57,092 --> 00:03:00,680 Then I loop through every character in stri. 42 00:03:00,680 --> 00:03:05,612 So I'll say for c in stri. 43 00:03:05,612 --> 00:03:12,311 And if I haven't seen that character before, 44 00:03:12,311 --> 00:03:19,366 so if c is not in char_d then char_d[c] = 0. 45 00:03:19,366 --> 00:03:23,633 And then inside of the for, but outside of the if, 46 00:03:23,633 --> 00:03:27,296 I say char_d[c] = its previous value. 47 00:03:29,282 --> 00:03:30,495 + 1. 48 00:03:32,673 --> 00:03:36,420 So when I run my code then I can see that it worked correctly. 49 00:03:36,420 --> 00:03:41,192 But let me run this code in CodeLens, just to see again what's going on here. 50 00:03:41,192 --> 00:03:46,852 So on line 1 we initialize stri to be the string "what can I do". 51 00:03:46,852 --> 00:03:51,402 On line 3 we initialize our accumulator variable, our accumulator dictionary, 52 00:03:51,402 --> 00:03:53,830 to be an empty dictionary. 53 00:03:53,830 --> 00:03:57,130 Then we loop through every character in stri. 54 00:03:57,130 --> 00:04:03,930 So we first say, c is the character "w", or the first character. 55 00:04:03,930 --> 00:04:08,700 And then we say if c is not in char_d, which it is not, so 56 00:04:08,700 --> 00:04:14,600 we assign char_d[c] or char_d[w] to be 0, and then we increment it. 57 00:04:16,040 --> 00:04:17,870 Same thing with the letter "h". 58 00:04:17,870 --> 00:04:23,400 So "h" isn't in our dictionary, so we're going to add it, and 59 00:04:23,400 --> 00:04:26,550 initialize its value to 1. 60 00:04:26,550 --> 00:04:28,173 Same thing with "a". 61 00:04:28,173 --> 00:04:33,517 The character "a" was not in our dictionary so we initialize it to 1. 62 00:04:33,517 --> 00:04:36,219 Same thing with "t". 63 00:04:36,219 --> 00:04:38,011 Same thing with the character " ". 64 00:04:40,097 --> 00:04:42,726 And "c". 65 00:04:42,726 --> 00:04:45,393 So at this point we're at this "a". 66 00:04:45,393 --> 00:04:52,149 And we're going to see that "a" is in our dictionary. 67 00:04:52,149 --> 00:04:53,807 We're going to skip this if, and 68 00:04:53,807 --> 00:04:56,900 we're going to say char_d[a] = its previous value + 1. 69 00:04:56,900 --> 00:04:59,790 So we'll see this update to be 2. 70 00:04:59,790 --> 00:05:04,554 And then we keep adding characters as we go on. 71 00:05:07,181 --> 00:05:09,520 The second time we get to a space. 72 00:05:09,520 --> 00:05:11,650 So at this point we're at this space. 73 00:05:11,650 --> 00:05:17,920 Then we'll see this value increment by 1, and so on. 74 00:05:17,920 --> 00:05:19,580 That's all for now, until next time.