zoukankan      html  css  js  c++  java
  • python基础--字典

    Python基础--字典


    字典的常用函数:


    dict.clear( )--->无任何返回值


    说明:

      清除字典内的所有的元素

    语法:

    In [5]: dict.clear?
    Type:        method_descriptor
    String form: <method 'clear' of 'dict' objects>
    Namespace:   Python builtin
    Docstring:   D.clear() -> None.  Remove all items from D.

    实例:

    In [61]: dict_demo = {'name':'Kevin','age':18}
    
    In [62]: dict_demo.clear()
    
    In [63]: dict_demo
    Out[63]: {}

    dict.copy()-->返回字典的浅复制


    说明:

      Python 字典 copy() 函数返回一个字典的浅复制。

    语法:

    In [22]: dict.copy?
    Type:        builtin_function_or_method
    String form: <built-in method copy of dict object at 0x7f4d2c4577c8>
    Docstring:   D.copy() -> a shallow copy of D

    实例

    In [14]: dict = {'name':'Kevin','age':18}
    
    In [15]: dict_copy = dict.copy()
    
    In [16]: dict_copy
    Out[16]: {'name': 'Kevin', 'age': 18}

    dict.fromkeys(seq[, value])----返回列表


     

    说明:

      Python 字典 fromkeys() 函数用于创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值。

    参数:

    • seq 字典的键值列表
    • value(可选参数) :设置键序列(seq)的值。

    语法:

    In [25]: dict.fromkeys?
    Type:        builtin_function_or_method
    String form: <built-in method fromkeys of type object at 0xa3cb40>
    Definition:  dict.fromkeys(type, iterable, value)
    Docstring:   Returns a new dict with keys from iterable and values equal to value.

    实例:

    In [33]: dict_demo = {}
    
    In [34]: l = ['name','age','sex']
    
    In [35]: dict_demo = {}
    
    In [36]: dict_demo = dict_demo.fromkeys(l)
    In [
    37]: dict_demo Out[37]: {'sex': None, 'name': None, 'age': None} In [38]: dict_demo = dict_demo.fromkeys(l,'xx') In [39]: dict_demo Out[39]: {'sex': 'xx', 'name': 'xx', 'age': 'xx'}

    dict.get(key,default=None)-------返回字典指定键对应的值,如果值不存在则返回默认值。


    说明:

      Python 字典 get() 函数返回指定键的值,如果值不在字典中返回默认值。

    语法:

    In [2]: dict.get?
    Type:        method_descriptor
    String form: <method 'get' of 'dict' objects>
    Namespace:   Python builtin
    Docstring:   D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.

    实例:

    In [23]: dict
    Out[23]: {'age': 18, 'sex': '', 'name': 'Bob'}
    
    In [24]: dict.get('name','please try again')
    Out[24]: 'Bob'
    
    In [25]: dict.get('school','please try again')
    Out[25]: 'please try again'

    dict.items()--->返回可遍历的(键, 值) 元组数组。


     

    说明:

      Python 字典 items() 方法以列表返回可遍历的(键, 值) 元组数组。

    语法:

    In [41]: dict.items?
    Type:        builtin_function_or_method
    String form: <built-in method items of dict object at 0x7f4d2c357cc8>
    Docstring:   D.items() -> a set-like object providing a view on D's items

    实例:

    In [42]: dict_demo = {'name':'kevin','age':18}
    
    In [43]: dict_demo.items()
    Out[43]: dict_items([('name', 'kevin'), ('age', 18)])
    
    In [44]: type(dict_demo.items())
    Out[44]: dict_items

    字典的for循环:

    In [50]: dict_demo
    Out[50]: {'name': 'kevin', 'age': 18}
    
    In [51]: for k,v in dict_demo.items():
       ....:     print(k,v)
       ....:     
    name kevin
    age 18

    dict.keys()--->返回的类型为列表-列表中包含字典所有的键。


     

    说明:

      Python 字典 keys() 方法以列表返回一个字典所有的键。

    语法:

    In [57]: dict.keys?
    Type:        builtin_function_or_method
    String form: <built-in method keys of dict object at 0x7f4d2c357cc8>
    Docstring:   D.keys() -> a set-like object providing a view on D's keys

    实例:

    In [52]: dict_demo
    Out[52]: {'name': 'kevin', 'age': 18}
    
    In [53]: dict_demo.keys()
    Out[53]: dict_keys(['name', 'age'])
    
    In [54]: dict_list = dict_demo.keys()
    
    In [55]: dict_list
    Out[55]: dict_keys(['name', 'age'])
    
    In [56]: for i in dict_list:
       ....:     print(i)
       ....:     
    name
    age

    pop(key[,default])---返回值为被删除的值


     

    说明:

      Python 字典 pop() 方法删除字典给定键 key 所对应的值,返回值为被删除的值。key值必须给出。 否则,返回default值。

    参数:

    • key:删除字典给定键key所对应的值。
    • default(可选参数):如果指定的key不存在将返回默认值。

    语法:

    In [71]: dict.pop?
    Type:        builtin_function_or_method
    String form: <built-in method pop of dict object at 0x7f4d2c357cc8>
    Docstring:
    D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
    If key is not found, d is returned if given, otherwise KeyError is raised

    实例:

    In [67]: dict_demo = {'name':'kevin','age':18,'sex':'man'}
    
    In [68]: dict_demo.pop('name')
    Out[68]: 'kevin'
    
    In [69]: dict_demo.pop('name','Sorry please try again')
    Out[69]: 'Sorry please try again'
    
    In [70]: dict_demo
    Out[70]: {'sex': 'man', 'age': 18}

    dict.setdefault(key,default=None)----如果 key 在 字典中,返回对应的值。如果不在字典中,则插入 key 及设置的默认值 default,并返回 default ,default 默认值为 None。


    说明:

      Python 字典 setdefault() 方法和get()方法类似, 如果键不已经存在于字典中,将会添加键并将值设为默认值。

    参数:

      key: 查找的键

      default : 键不存在时,设置默认的键值

    实例:

    In [39]: dict = {'name':'Kevin','age':18,'sex':''}
    
    In [40]: dict.setdefault('name')
    Out[40]: 'Kevin'
    
    In [41]: dict.setdefault('school','将会把这个键值对重新的添加到dict中')
    Out[41]: '将会把这个键值对重新的添加到dict中'
    
    In [42]: dict
    Out[42]: {'name': 'Kevin', 'age': 18, 'sex': '', 'school': '将会把这个键值对重新的添加到dict中'}

    dict.update(dict2)---返回一个新的字典


    说明:

      Python 字典 update() 函数把字典参数 dict2 的 key/value(键/值) 对更新到字典 dict 里。

    参数:

      dict2 -- 添加到指定字典dict里的字典。

    实例

    In [4]: dict = {'name':'Kevin','age':18,'sex':''}
    
    In [5]: dict2 = {'school':'heigh school'}
    
    In [6]: dict.update(dict2)
    
    In [7]: dict
    Out[7]: {'school': 'heigh school', 'age': 18, 'sex': '', 'name': 'Kevin'}

    注意:当插入的字典与初始字典具有相同的键的时候那么值将会被覆盖

    In [8]: dict
    Out[8]: {'school': 'heigh school', 'age': 18, 'sex': '', 'name': 'Kevin'}
    
    In [9]: dict3 = {'name':'Bob'}
    
    In [10]: dict.update(dict3)
    
    In [11]: dict
    Out[11]: {'school': 'heigh school', 'age': 18, 'sex': '', 'name': 'Bob'}

    dict.values()--以列表的形式返回字典中的所有的值


    说明:

      Python 字典 values() 方法以列表返回字典中的所有值。

    实例:  

    In [11]: dict
    Out[11]: {'school': 'heigh school', 'age': 18, 'sex': '', 'name': 'Bob'}
    
    In [12]: dict.values()
    Out[12]: dict_values(['heigh school', 18, '', 'Bob'])
    
    In [13]: list(dict.values())
    Out[13]: ['heigh school', 18, '', 'Bob']

    dict.popitem() ----返回值是已经删除的字典的键值对


    说明:  

      Python 字典 popitem() 方法随机返回并删除字典中的一对键和值(一般删除末尾对)。

      如果字典已经为空,却调用了此方法,就报出KeyError异常。

    实例: 

    In [14]: dict
    Out[14]: {'school': 'heigh school', 'age': 18, 'sex': '', 'name': 'Bob'}
    
    In [15]: dict.popitem()
    Out[15]: ('school', 'heigh school')
    
    In [16]: dict
    Out[16]: {'age': 18, 'sex': '', 'name': 'Bob'}

    字典的操作


    待更新。。。。

  • 相关阅读:
    记录下Cookie与Session
    宝塔部署 springboot 项目遇到的 一些bug处理方案
    [IDEA] [SpringBoot] 项目所写的内容不能同步到编译出的文件中
    cookie和session的区别
    JVM类加载
    线程与线程池
    子父类继承相关(static)
    界面控件开发包DevExpress 9月正式发布v21.1.6
    Delphi开发工具DevExpress VCL全新发布v21.1.5
    强大的Visual Studio插件CodeRush v21.1.7已正式发布
  • 原文地址:https://www.cnblogs.com/Echo-O/p/9323297.html
Copyright © 2011-2022 走看看