zoukankan      html  css  js  c++  java
  • python学习随笔之字典

    len():判断字典大小

    dict1={1:'a',2:'b',3:'c',4:'d'}
    len(dict1)
    

    dict1.clear():清除dict1字典中的所有元素,返回空字典{}

    dict1.get(key,default=None):返回key的value,如果该key不存在则返回default指定的值

    dict1={'name':'abc','age':30,'gender':'male'}
    dict1.get(0,)          #正常,无回显
    dict1.get(5,'error') #get键值为5的值,而字典中无key=5的值,则显示error
    

    dict1.keys() :返回dict1字典中所有的key值

    dict1.values() :返回dict1字典中所有的value值

    dict1.pop(key,default=dict1[key]): 删除key的value,如果key存在,返回dict1[key]并删除,如果不存在,则返回default定义的值

    dict1={'name':'abc','age':30,'gender':'male'}
    dict1.pop('name','error') #删除key值为‘name’的value,返回‘abc’并删除
    dict1
    dict1.pop('user','error')  #key值为'user'不存在在字典dict1中,于是返回‘error’
  • 相关阅读:
    BSP与HAL关系(转)
    [Advanced Algorithm]
    [Advanced Algorithm]
    [Advanced Algorithm]
    [Intermediate Algorithm]
    [Intermediate Algorithm]
    [Intermediate Algorithm]
    [Intermediate Algorithm]
    [Intermediate Algorithm]
    [Advanced Algorithm]
  • 原文地址:https://www.cnblogs.com/solozhou/p/6404988.html
Copyright © 2011-2022 走看看