zoukankan      html  css  js  c++  java
  • get()函数

    get()函数作用

    以classCount.get(voteIlabel,0)为例: 
    classCount.get(voteIlabel,0)返回字典classCount中voteIlabel元素对应的值,若无,则进行初始化

    • 若不存在voteIlabel,则字典classCount中生成voteIlabel元素,并使其对应的数字为0,即 
      classCount = {voteIlabel:0} 
      此时classCount.get(voteIlabel,0)作用是检测并生成新元素,括号中的0只用作初始化,之后再无作用

    • 当字典中有voteIlabel元素时,classCount.get(voteIlabel,0)作用是返回该元素对应的值,即0

    以书中代码为例: 
    classCount[voteIlabel] = classCount.get(voteIlabel,0) + 1;

    • 初始化classCount = {}时,此时输入classCount,输出为: 

      classCount = {}

    • 当第一次遇到新的label时,将新的label添加到字典classCount,并初始化其对应数值为0 
      然后+1,即该label已经出现过一次,此时输入classCount,输出为:

      classCount = {voteIlabel:1}

    • 当第二次遇到同一个label时,classCount.get(voteIlabel,0)返回对应的数值(此时括号内的0不起作用,因为已经初始化过了),然后+1,此时输入classCount,输出为:

      classCount = {voteIlabel:2}

    可以看出,+1是每一次都会起作用的,因为不管遇到字典内已经存在的或者不存在的,都需要把这个元素记录下来

  • 相关阅读:
    VSCode C++ 主题
    Linux 软连接应用
    Python 调用 C 动态库
    Qt 打包程序
    Qt 样式修改
    libusb 批传输的使用方法
    Qt 数据库操作
    Qt 调用第三方库
    CS 调用 C 动态库
    Qt 串口操作
  • 原文地址:https://www.cnblogs.com/whnbky/p/11323947.html
Copyright © 2011-2022 走看看