1 00:00:07,910 --> 00:00:11,490 Welcome back. I'm going to work a problem 2 00:00:11,490 --> 00:00:14,310 that involves traversing a nested data structure, 3 00:00:14,310 --> 00:00:16,455 and accumulating a value. 4 00:00:16,455 --> 00:00:19,590 I've got here a variable called big list. 5 00:00:19,590 --> 00:00:26,175 As you can see, it has some nested lists some inner lists inside of the big list. 6 00:00:26,175 --> 00:00:30,225 In fact, it's got three levels of nesting that we can see right away, 7 00:00:30,225 --> 00:00:33,885 because we've got the three square brackets here. 8 00:00:33,885 --> 00:00:41,040 So, it's going to create a list that has some elements in it. 9 00:00:41,040 --> 00:00:43,670 That first element of that list is itself going to be 10 00:00:43,670 --> 00:00:46,879 a list that has some elements in it, 11 00:00:46,879 --> 00:00:53,680 and the first element of that has a list containing one and two. 12 00:00:56,660 --> 00:01:01,670 So, in order to traverse this and pull out a word like one, 13 00:01:01,670 --> 00:01:02,900 or seven, or nine, 14 00:01:02,900 --> 00:01:06,640 I'm going to have to go three levels into this. 15 00:01:06,640 --> 00:01:11,255 So, that's some combination of three square brackets and three four loops. 16 00:01:11,255 --> 00:01:15,215 In this case, I really want to go through every single word, 17 00:01:15,215 --> 00:01:18,665 because I'm supposed to create a dictionary 18 00:01:18,665 --> 00:01:23,720 word counts that contains all the words in big list as keys. 19 00:01:23,720 --> 00:01:25,895 So, I'm going to have as a key, 20 00:01:25,895 --> 00:01:29,560 the word one, and as a value, 21 00:01:29,560 --> 00:01:35,675 however many times it appears anywhere deep this nested list, 22 00:01:35,675 --> 00:01:41,710 and two, however many times it shows up. 23 00:01:41,710 --> 00:01:45,780 So, that's the dictionary I'm supposed to create. 24 00:01:46,000 --> 00:01:52,370 To do that I'm going to have to find all of the words anywhere in the big list. 25 00:01:52,370 --> 00:01:54,200 So, let's get started on this. 26 00:01:54,200 --> 00:01:58,750 As always, I'd like to make a little template. 27 00:01:58,750 --> 00:02:02,400 I'm supposed to create a dictionary word counts. 28 00:02:02,400 --> 00:02:04,600 I'm going to accumulate that dictionary, 29 00:02:04,600 --> 00:02:06,750 so I'm going to start with an empty one, 30 00:02:06,750 --> 00:02:13,980 and at the end, I'm going to print word counts just so I can check on my work. 31 00:02:13,980 --> 00:02:18,670 I'm going to clear out all of the markings that I have here, 32 00:02:28,160 --> 00:02:30,630 and I can try running this. 33 00:02:30,630 --> 00:02:32,370 Of course it's not going to give me the right answer, 34 00:02:32,370 --> 00:02:35,790 but at least it's going to get me started. 35 00:02:35,790 --> 00:02:42,520 You can see that I've failed the test because my actual value was not what was expected, 36 00:02:42,520 --> 00:02:45,470 but we have a good start. 37 00:02:51,080 --> 00:02:54,765 Just going to make my code window narrower, 38 00:02:54,765 --> 00:02:59,360 so that it doesn't overlap with the output window. 39 00:03:02,600 --> 00:03:08,650 Alright. So, we can see that what we've output is just the dictionary. 40 00:03:08,650 --> 00:03:13,225 That's empty we need to traverse through big list, 41 00:03:13,225 --> 00:03:16,030 and since we're going to have multiple levels here, 42 00:03:16,030 --> 00:03:18,265 I'm going to use a little mnemonic where I say, 43 00:03:18,265 --> 00:03:22,330 for inner list level one, 44 00:03:22,330 --> 00:03:27,790 in big list; IO one is going to be bound to one of the inner lists. 45 00:03:27,790 --> 00:03:31,870 Initially, its going to be bound to one that begins here, 46 00:03:31,870 --> 00:03:34,975 and keeps going off the screen. 47 00:03:34,975 --> 00:03:36,660 So, that's an inner list, 48 00:03:36,660 --> 00:03:40,350 I'm going to have to descend into that as well. 49 00:03:40,790 --> 00:03:47,930 So, I'm going to say for each second level list in the first level list, 50 00:03:47,930 --> 00:03:49,535 I'm going to do something, 51 00:03:49,535 --> 00:03:52,280 and because I need to descend a third level, 52 00:03:52,280 --> 00:03:55,620 I think I'm going to need three for loops here. 53 00:04:00,100 --> 00:04:04,540 I see that I needed a one instead of a two there. 54 00:04:04,540 --> 00:04:07,210 Now, I think that this is the right thing, 55 00:04:07,210 --> 00:04:10,850 but I always like to check my work. 56 00:04:10,850 --> 00:04:15,900 So, I'm just going to see if this gets me the thing that I'm looking for, 57 00:04:17,300 --> 00:04:20,730 and sure enough that prints out all the words. 58 00:04:20,730 --> 00:04:24,040 I'm realizing now that I really shouldn't have called my 59 00:04:24,040 --> 00:04:27,100 iterator variable at level three, IO three, 60 00:04:27,100 --> 00:04:29,030 I really should have called it word, 61 00:04:29,030 --> 00:04:34,320 because that's going to help me remember what kind of thing I have there. 62 00:04:34,320 --> 00:04:38,605 Okay, I change my variable name for my IO three to word, 63 00:04:38,605 --> 00:04:40,990 and I still got exactly the same output. 64 00:04:40,990 --> 00:04:43,300 Now, instead of printing out all these words, 65 00:04:43,300 --> 00:04:48,200 this is where I want to use them to update the word counts dictionary. 66 00:04:48,810 --> 00:04:51,205 Instead of printing the word, 67 00:04:51,205 --> 00:04:55,180 I'm going to check if the word is already in word counts, 68 00:04:55,180 --> 00:04:59,060 and this is a pattern we've seen quite a bit before. 69 00:04:59,060 --> 00:05:01,295 If we've already seen the word, 70 00:05:01,295 --> 00:05:04,370 then we want to update its count, 71 00:05:04,370 --> 00:05:06,780 we want to increment it by one. 72 00:05:09,560 --> 00:05:12,955 If we haven't seen this word before, 73 00:05:12,955 --> 00:05:18,155 then we have to put it into the dictionary for the very first time, 74 00:05:18,155 --> 00:05:23,990 and its initial value ought to be one because we've now seen that word one time. 75 00:05:23,990 --> 00:05:25,490 So, if we've seen it before, 76 00:05:25,490 --> 00:05:26,825 we increment its count, 77 00:05:26,825 --> 00:05:28,220 if we haven't seen before, 78 00:05:28,220 --> 00:05:30,305 we set its count to one. 79 00:05:30,305 --> 00:05:34,055 I think that should get us what we want. 80 00:05:34,055 --> 00:05:40,215 So, associated with the word one is the count of four, 81 00:05:40,215 --> 00:05:44,460 that means It's happening four times somewhere in here, 82 00:05:44,460 --> 00:05:49,380 and you can see it is showing up more than once somewhere there. 83 00:05:49,430 --> 00:05:54,200 So, that's how to work your way up to a solution for a problem. 84 00:05:54,200 --> 00:05:57,690 Remember that if the data you want to extract is nested three levels deep, 85 00:05:57,690 --> 00:06:00,875 you're going to need to descend three levels with your code. 86 00:06:00,875 --> 00:06:03,260 Either square brackets, or four loops. 87 00:06:03,260 --> 00:06:08,940 In this case we have three indented four loops one inside the other, 88 00:06:09,100 --> 00:06:13,160 and we have the first level of dissent, 89 00:06:13,160 --> 00:06:18,320 the second level, and notice that the iterator variable for 90 00:06:18,320 --> 00:06:23,500 the first time becomes the sequence for the second one, 91 00:06:23,500 --> 00:06:26,495 and the iterator variable at the second level 92 00:06:26,495 --> 00:06:30,315 becomes the sequence at the third level and so on. 93 00:06:30,315 --> 00:06:33,425 Hopefully, you'll be able to do some of the other exercises 94 00:06:33,425 --> 00:06:36,620 on this exercise's page at the end of the chapter, 95 00:06:36,620 --> 00:06:38,750 and you'll be able to do some of these on your own. 96 00:06:38,750 --> 00:06:42,240 Good luck. See you next time.