Get () function action
Take Classcount.get (voteilabel,0) as an example:
Classcount.get (voteilabel,0) Returns the value of the Voteilabel element in the dictionary ClassCount and, if none, initializes
If there is no Voteilabel, the Voteilabel element is generated in the dictionary ClassCount and the corresponding number is 0, which is
ClassCount = {voteilabel:0}
At this point the Classcount.get (voteilabel,0) function is to detect and generate new elements, 0 of the parentheses are used as initialization, and then no effect
When there is a Voteilabel element in the dictionary, the Classcount.get (voteilabel,0) function returns the value corresponding to that element, which is 0
Take the code in the book as an example:
Classcount[voteilabel] = Classcount.get (voteilabel,0) + 1;
- When initializing ClassCount = {}, enter ClassCount at this time, and the output is:
ClassCount = {}
When you first encounter a new label, add a new label to the dictionary ClassCount and initialize its corresponding value to 0
Then +1, that is, the label has appeared once, at this time input classcount, the output is:
ClassCount = {Voteilabel:1}
When the same label is encountered the second time, Classcount.get (voteilabel,0) returns the corresponding value (the 0 in parentheses does not work, because it has already been initialized), and then +1, when you enter ClassCount, the output is:
ClassCount = {Voteilabel:2}
As you can see, +1 is a function every time, because it is necessary to record this element regardless of the existence or absence of the dictionary.
The Get function in Python