The Get function in Python

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.