zoukankan      html  css  js  c++  java
  • python 字典dict方法

    dict方法

    字典(dict)
      特性
        1.由键值对组成的序列。
        2.字典是无序的(非线性序列)。 字典的定义(初始化):
        d = dict()
        d1 = {'k1':'v1','k2','v2'} 生成字典方法: d1
    = {'k1':v1,'k2':'v2'}
      
      字典方法:
    class dict(object):
    """
    dict() -> new empty dictionary
    dict(mapping) -> new dictionary initialized from a mapping object's
    (key, value) pairs
    dict(iterable) -> new dictionary initialized as if via:
    d = {}
    for k, v in iterable:
    d[k] = v
    dict(**kwargs) -> new dictionary initialized with the name=value pairs
    in the keyword argument list. For example: dict(one=1, two=2)
        #字典初始化,可接收name=value形式的参数,生成字典
    ------------------------------------------------------------------------------
    example:
    >>> d1 = dict(k1 = 'v1', age = 27, name = 'handsome man')

    >>> print(d1)
      
    {'k1': 'v1', 'age': 27, 'name': 'handsome man'}
    ------------------------------------------------------------------------------
        """
    def clear(self): # real signature unknown; restored from __doc__
    """ D.clear() -> None. Remove all items from D. """
    pass
      #删除列表中的所有元素。
    -------------------------------------------------------------------------------
    example:

    >>> print(d1)
    {'k1': 'v1', 'age': 27, 'name': 'handsome man'}
    >>> d1.clear()
    >>> print(d1)
    {}

    -------------------------------------------------------------------------------


    -------------------------------------------------------------------------------
    -------------------------------------------------------------------------------

    def copy(self): # real signature unknown; restored from __doc__
    """ D.copy() -> a shallow copy of D """
    pass
      #复制,浅复制,只复制字段第一层的元素。

    -------------------------------------------------------------------------------

    >>> d1 = dict(k1 = 'v1', age = 27, name = 'handsome man')
    >>> d2 = d1.copy()
    >>> print(d2)
    {'k1': 'v1', 'age': 27, 'name': 'handsome man'}

    >>> d3 = {'k1':[1,2,3,4,5,6]}

    >>> d4 = d3.copy()
    >>> d3['k1'].insert(0,100)
    >>> print(d3,d4)
    {'k1': [100, 1, 2, 3, 4, 5, 6]} {'k1': [100, 1, 2, 3, 4, 5, 6]}
    >>>

    -------------------------------------------------------------------------------
      

    @staticmethod # known case
    def fromkeys(*args, **kwargs): # real signature unknown
    """ Returns a new dict with keys from iterable and values equal to value. """
    pass
      #

    def get(self, k, d=None): # real signature unknown; restored from __doc__
    """ D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """
    pass
      #获取键k对应的value,如果k在字典中返回value,否则返回d,d默认值为none
    -------------------------------------------------------------------------------

    >>> d1
    {'k1': 'v1', 'age': 27, 'name': 'handsome man'}
    >>> d1.get('k1')
    'v1'
    >>> d1.get('k2')
    >>> print(d1.get('k2'))
    None

    >>> d1.get('k2','nothiskey')
    'nothiskey'

    -------------------------------------------------------------------------------

    def items(self): # real signature unknown; restored from __doc__
    """ D.items() -> a set-like object providing a view on D's items """
      #返回一个键值对组成的,dict_items对象(可迭代)
    pass
    ------------------------------------------------------------------------------

    >>> d1.items()
    dict_items([('k1', 'v1'), ('age', 27), ('name', 'handsome man')])
    >>> type(d1.items())
    <class 'dict_items'>
    >>> for i,j in d1.items():
    ... print(i,j)
    ...
    k1 v1
    age 27
    name handsome man
    >>>

    ------------------------------------------------------------------------------


    def keys(self): # real signature unknown; restored from __doc__
    """ D.keys() -> a set-like object providing a view on D's keys """
    pass
      #返回一个key组成的,dict_keys对象(可迭代)
    ------------------------------------------------------------------------------

    >>> d1.keys()
    dict_keys(['k1', 'age', 'name'])
    >>> type(d1.keys())
    <class 'dict_keys'>
    >>> for i in d1.keys():
    ... print(i)
    ...
    k1
    age
    name

    ------------------------------------------------------------------------------
        def pop(self, k, d=None): # real signature unknown; restored from __doc__
    """
    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
    """
    pass
      #移除一个键值对,并反馈该键所对应的值。
    ------------------------------------------------------------------------------

    >>> d1
    {'k1': 'v1', 'age': 27, 'name': 'handsome man'}
    >>> d1.pop('k1')
    'v1'

    ------------------------------------------------------------------------------
        def popitem(self): # real signature unknown; restored from __doc__
    """
    D.popitem() -> (k, v), remove and return some (key, value) pair as a
    2-tuple; but raise KeyError if D is empty.
    """
    pass
      #随机移除一个键值对,并返回该键值对组成的元组
    ------------------------------------------------------------------------------

    >>> d1
    {'k1': 'v1', 'age': 27, 'name': 'handsome man'}
    >>> d1.popitem()
    ('name', 'handsome man')
    >>> d1
    {'k1': 'v1', 'age': 27}
    >>>

    ------------------------------------------------------------------------------
        def setdefault(self, k, d=None): # real signature unknown; restored from __doc__
    """ D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """
    pass

    def update(self, E=None, **F): # known special case of dict.update
    """
    D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
    If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]
    If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v
    In either case, this is followed by: for k in F: D[k] = F[k]
    """
    pass
      #用一个字典来更新自己。
    ------------------------------------------------------------------------------

    >>> d2 = {'name':'handsome man','k2':'v2'}
    >>> d1 = dict(k1 = 'v1', age = 27, name = 'handsome man')
    >>> d2.update(d1)
    >>> d2
    {'name': 'handsome man', 'k2': 'v2', 'k1': 'v1', 'age': 27}

    ------------------------------------------------------------------------------

      
    def values(self): # real signature unknown; restored from __doc__
    """ D.values() -> an object providing a view on D's values """
    pass
      #返回value组成的dict_values对象(可迭代)
    -------------------------------------------------------------------------------

    >>> d1
    {'k1': 'v1', 'age': 27, 'name': 'handsome man'}
    >>> d1.values()
    dict_values(['v1', 27, 'handsome man'])
    >>> for i in d1.values():
    ... print(i)
    ...
    v1
    27
    handsome man

    -----------------------------------------------------------------------------------

  • 相关阅读:
    python--模块与包
    内置函数 的总结
    迭代器 生成器 列表推导式 生成器表达式的一些总结
    函数的有用信息 带参数的装饰器 多个装饰器装饰一个函数
    函数名的应用(第一对象) 闭包 装饰器
    动态参数 名称空间 作用域 作用域链 加载顺序 函数的嵌套 global nonlocal 等的用法总结
    函数的初识 函数的返回值 参数
    文件操作 常用操作方法 文件的修改
    遍历字典的集中方法 集合的作用 以及增删查的方法
    计算机硬件的小知识
  • 原文地址:https://www.cnblogs.com/LouisZJ/p/8073505.html
Copyright © 2011-2022 走看看