Thursday Mornings With Python Part 9 - "I'd Sooner Die Defying Gravity"
Thursday Mornings With Python Part 9 - "I'd Sooner Die Defying Gravity"
Named Tuples.
"Everyone deserves a chance to fly." So I am moving on quickly with these. I get them.
I won't spend too long writing them out. Thank you. I completed the lesson so let's move on.
Default Dict
I have heard of these before. Even used them.
So what are they now?
Default dicts help us to avoid key errors by having default missing values in dictionaries.
This is amazing and so so cool as it allows us to avoid having too many errors! Thanks.
We set the default value using lambda.
I went through this and it made a lot of sense so I'm moving on very quickly.
Ordered Dict
This is to do with the fact that dicts are unordered. We can store a list of dicts themselves inside a list but then we have to do a lot of complicated accessing to access them (although I disagree with the course slightly here - the accessing is fine. I suppose it depends how long the list is - anything more than a few items could get really annoying)!
"The order of the data is preserved when adding it to the OrderedDict". Data can be accessed just like with any other normal dictionary. We can access the order by converting to a list and then accessing by index. And we can move things around, move things to the back, and then pop from the back, and so on.
Chain Map
"The Chain Map container allows us to store many mappings in an ordered group, but lookups accessing the value as in a key are repeated for every mapping inside of the Chain Map until something is found or the end is reached. If we try to modify the data in any way, then only the first mapping in the Chain Map will receive the changes."
"When accessing data, one way to think of the Chain Map is that it treats all of the stored dictionaries as one large dictionary where if there are repeated keys, then the first found result is returned."
Once a Chain Map is set up:
"Now we can access values from any of the stored MAPPINGS."
This one stacks multiple dictionaries in the order you add them in.
And then if there are multiple ones with the same key it just keeps on going in order from the top down returning values until it is done.
There is a .parents call that removes the top level of the stack - I don't know why.
For some reason we can only modify data as well from the first dictionary which makes absolutely no sense at all. Apparently it's because all of the later mappings are considered to be "parents" to the first initial mapping.
If we want to modify something that is deeper down inside one of those deeper dictionaries, then we will have to iterate through those dictionaries. Yep no sorry it still continues to make no sense at all - yep no I'm sorry I'm so sorry it makes no sense at all I'm sorry.
Okay I get it maybe - why is the first one the child - YOU CAN ADD A NEW CHILD TO THE FRONT OF THE MAP USING THE .new_child() METHOD.
Counter
"One of the most common tasks we might have to do in a program is to count instances of an element in a collection." The collections module's Counter tool allows us to count things a bit more efficiently.
This is much more efficient than using a loop and a dictionary, right? I did the lesson.
Very very useful indeed!! And very efficient!
Counter Wrappers
Cool this was just a little intro.
I had a little play around with them.
More next lesson.
Comments
Post a Comment