zoukankan      html  css  js  c++  java
  • dict的特性和基本语法——python3.6

    • 特性
      • key:value结构,字典中的每一个元素,都是键值对
      • key必须可被hash,且必须为不可变数据类型,必须唯一
      • 可存放任意多个值,可修改,可以不唯一
      • 无序
      • 查找速度快,因为hash可以把key变成数字,数字则可通过二分法快速查找,提高查找效率
    • 基本语法
      • clear:清空字典
      • Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'}
        print(Info)    # =>{'stu1': 'LiLei', 'stu2': 'HanMeiMei', 'stu3': 'XiaoMing'}
        Info.clear()
        print(Info)    # =>{}
        View Code
      • copy:复制字典
      • Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'}
        Info_new = Info.copy()
        print(Info_new)   # =>{'stu1': 'LiLei', 'stu2': 'HanMeiMei', 'stu3': 'XiaoMing'}
        View Code
      • fromkeys:返回一个新的字典,key值可迭代,但所有的value都是相等的
      • Info = {}
        Info_new = Info.fromkeys([1,2,3],'Hi')
        print(Info_new)   # =>{1: 'Hi', 2: 'Hi', 3: 'Hi'}
        View Code
      • get:通过key获取对应的value,如果字典中key存在,则返回对应的value,如果不存在,返回None
      • Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'}
        print(Info.get('stu2'))# =>HanMeiMei
        print(Info.get('stu4'))# =>None
        View Code
      • items:获取所有的key-value,以列表方式呈现
      • Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'}
        print(Info.items())# =>dict_items([('stu1', 'LiLei'), ('stu2', 'HanMeiMei'), ('stu3', 'XiaoMing')])
        View Code
      • keys:获取所有的key
      • Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'}
        print(Info.keys()) # =>dict_keys(['stu1', 'stu2', 'stu3'])
        View Code
      • values:获取所有的values
      • Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'}
        print(Info.values()) # =>dict_values(['LiLei', 'HanMeiMei', 'XiaoMing'])
        View Code
      • pop:pop(self,k,d=None)删除指定的key,如果key存在,则返回对应的value,如果key不存在,则返回指定的d,如果d未指定,则报错
      • Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'}
        print(Info.pop('stu1'))# =>LiLei
        print(Info.pop('stu4',0))# =>0
        print(Info.pop('stu4'))# =>报错
        View Code
      • popitem:删除字典中的一对key-value,无序的,当字典为空时,报错
      • Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'}
        print(Info.popitem())# =>('stu3', 'XiaoMing')
        print(Info.popitem())# =>('stu2', 'HanMeiMei')
        print(Info.popitem())# =>('stu1', 'LiLei')
        print(Info.popitem())# =>报错
        View Code
      • setdefault:setdefault(self,k,d=None)如果key存在,则返回对应的value,如果key不存在,则将该key-value添加进字典,并返回该value(即指定的d),若d未指定,则为None 
      • Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'}
        print(Info.setdefault('stu3','Jack'))# =>XiaoMing
        print(Info)# =>{'stu1': 'LiLei', 'stu2': 'HanMeiMei', 'stu3': 'XiaoMing'}
        print(Info.setdefault('stu4','Jack'))# =>Jack
        print(Info)# =>{'stu1': 'LiLei', 'stu2': 'HanMeiMei', 'stu3': 'XiaoMing', 'stu4': 'Jack'}
        View Code
      • update:更新字典,若key不重复,则类似增添新的key-value;若key重复,则key对应的value会被覆盖

      • Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'}
        Info_new = {'stu4':'Jack','stu5':'Tony','stu6':'Tom'}
        Info.update(Info_new)
        print(Info)# =>{'stu1': 'LiLei', 'stu2': 'HanMeiMei', 'stu3': 'XiaoMing', 'stu4': 'Jack', 'stu5': 'Tony', 'stu6': 'Tom'}
        View Code
        Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'}
        Info_new = {'stu1':'Jack','stu5':'Tony','stu6':'Tom'}
        Info.update(Info_new)
        print(Info)# =>{'stu1': 'Jack', 'stu2': 'HanMeiMei', 'stu3': 'XiaoMing', 'stu5': 'Tony', 'stu6': 'Tom'}
        View Code
    • 其他语法
      • 循环输出所有的key
      • Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'}
        for key in Info:
            print(key)
        View Code
      • Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'}
        for key in Info.keys():
            print(key)
        View Code
      • 循环输出所有的value
      • Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'}
        for value in Info.values():
            print(value)
        View Code
      • 循环输出所有的key-value
      • Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'}
        for key in Info:
            print(key,Info[key])
        View Code
      • del:删除指定的key,若key不存在,则报错

      • Info = {'stu1':'LiLei','stu2':'HanMeiMei','stu3':'XiaoMing'}
        del Info['stu1']
        print(Info)# =>{'stu2': 'HanMeiMei', 'stu3': 'XiaoMing'}
        del Info['stu4']# =>报错
        View Code
  • 相关阅读:
    Lost connection to MySQL server at 'waiting for initial communication packet', system error: 0
    Can't connect to MySQL server on '192.168.7.175' (10060)
    单精度浮点数(float)加法计算出错
    当前不会命中断点 还没有为该文档加载任何符号
    64位程序,long*转long 出错
    当前安全设置不允许下载该文件的原因以及图文解决办法
    IndentationError: unindent does not match any outer indentation level
    MongoDB状态查询:db.serverStatus()
    bson.errors.InvalidStringData: strings in documents must be valid UTF-8
    Transformer的PyTorch实现
  • 原文地址:https://www.cnblogs.com/GraceZ/p/7841254.html
Copyright © 2011-2022 走看看