Welcome back. I'm going to work a problem
that involves traversing a nested data structure,
and accumulating a value.
I've got here a variable called big list.
As you can see, it has some nested lists some inner lists inside of the big list.
In fact, it's got three levels of nesting that we can see right away,
because we've got the three square brackets here.
So, it's going to create a list that has some elements in it.
That first element of that list is itself going to be
a list that has some elements in it,
and the first element of that has a list containing one and two.
So, in order to traverse this and pull out a word like one,
or seven, or nine,
I'm going to have to go three levels into this.
So, that's some combination of three square brackets and three four loops.
In this case, I really want to go through every single word,
because I'm supposed to create a dictionary
word counts that contains all the words in big list as keys.
So, I'm going to have as a key,
the word one, and as a value,
however many times it appears anywhere deep this nested list,
and two, however many times it shows up.
So, that's the dictionary I'm supposed to create.
To do that I'm going to have to find all of the words anywhere in the big list.
So, let's get started on this.
As always, I'd like to make a little template.
I'm supposed to create a dictionary word counts.
I'm going to accumulate that dictionary,
so I'm going to start with an empty one,
and at the end, I'm going to print word counts just so I can check on my work.
I'm going to clear out all of the markings that I have here,
and I can try running this.
Of course it's not going to give me the right answer,
but at least it's going to get me started.
You can see that I've failed the test because my actual value was not what was expected,
but we have a good start.
Just going to make my code window narrower,
so that it doesn't overlap with the output window.
Alright. So, we can see that what we've output is just the dictionary.
That's empty we need to traverse through big list,
and since we're going to have multiple levels here,
I'm going to use a little mnemonic where I say,
for inner list level one,
in big list; IO one is going to be bound to one of the inner lists.
Initially, its going to be bound to one that begins here,
and keeps going off the screen.
So, that's an inner list,
I'm going to have to descend into that as well.
So, I'm going to say for each second level list in the first level list,
I'm going to do something,
and because I need to descend a third level,
I think I'm going to need three for loops here.
I see that I needed a one instead of a two there.
Now, I think that this is the right thing,
but I always like to check my work.
So, I'm just going to see if this gets me the thing that I'm looking for,
and sure enough that prints out all the words.
I'm realizing now that I really shouldn't have called my
iterator variable at level three, IO three,
I really should have called it word,
because that's going to help me remember what kind of thing I have there.
Okay, I change my variable name for my IO three to word,
and I still got exactly the same output.
Now, instead of printing out all these words,
this is where I want to use them to update the word counts dictionary.
Instead of printing the word,
I'm going to check if the word is already in word counts,
and this is a pattern we've seen quite a bit before.
If we've already seen the word,
then we want to update its count,
we want to increment it by one.
If we haven't seen this word before,
then we have to put it into the dictionary for the very first time,
and its initial value ought to be one because we've now seen that word one time.
So, if we've seen it before,
we increment its count,
if we haven't seen before,
we set its count to one.
I think that should get us what we want.
So, associated with the word one is the count of four,
that means It's happening four times somewhere in here,
and you can see it is showing up more than once somewhere there.
So, that's how to work your way up to a solution for a problem.
Remember that if the data you want to extract is nested three levels deep,
you're going to need to descend three levels with your code.
Either square brackets, or four loops.
In this case we have three indented four loops one inside the other,
and we have the first level of dissent,
the second level, and notice that the iterator variable for
the first time becomes the sequence for the second one,
and the iterator variable at the second level
becomes the sequence at the third level and so on.
Hopefully, you'll be able to do some of the other exercises
on this exercise's page at the end of the chapter,
and you'll be able to do some of these on your own.
Good luck. See you next time.