dict文档

文档

class dict(object):
    """
    dict() -> new empty dictionary
    创建字典的方式有两种:
    1、dic = {}
    2、dic = dict()
    ----------------------------------------------------------------------
    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)
    """
    def clear(self): # real signature unknown; restored from __doc__
        """
        D.clear() -> None.  Remove all items from D.
        清空字典
        """
        pass

    def copy(self): # real signature unknown; restored from __doc__
        """
        D.copy() -> a shallow copy of D
        拷贝一个字典
        跟列表的lcopy方法类似,也是一个浅拷贝,如果字典里面嵌套字典或者列表,内层不会
        如果需要深拷贝,也要导入copy模块
        --------------------------------------------------------------------------
        dic3 = {'k1':{'kk1':1},'k2':{'kk2':'2'}}
        dic4 = dic3.copy()
        print(dic3['k1'] is dic4['k1'])    # True
        dic3['k1']['kk1'] = 'kkk1'
        print(dic3)
        print(dic4)
        """
        pass

    @staticmethod # known case
    def fromkeys(*args, **kwargs): # real signature unknown
        """
        Returns a new dict with keys from iterable and values equal to value.
        创建一个新的字典,字典的key是从第一个参数(可迭代对象)里一一获取的,
        value值是从第二个参数(任意数据类型),默认是None
        注意!这个是一个静态方法,是用dict类直接调用的!!!
        ---------------------------------------------------------------------------
        s = 'fuyong'
        d1 = dict.fromkeys(s)
        print(d1)          # {'g': None, 'n': None, 'f': None, 'y': None, 'u': None, 'o': None}

        l = ['a','b','c']
        d2 = dict.fromkeys(l,1000)
        print(d2)         # {'a': 1000, 'b': 1000, 'c': 1000}
        ----------------------------------------------------------------------------
         """
        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.
        根据key值来查找value值
        如果找不到不会报错,返回None
        -------------------------------------------------------------------------------
        d1 = {'a':'b'}
        print(d1.get('a'))      # b
        print(d1.get('c'))      # None
        """
        pass

    def items(self): # real signature unknown; restored from __doc__
        """
        D.items() -> a set-like object providing a view on D's items
        将字典的每一个元素的key和value值放到一个元组里,然后以一个类似列表形式包起来
        但是返回值不是一个列表,而是一个dict_items对象
        ---------------------------------------------------------------
        d1 = {'a':1,'b':2}
        print(d1.items())           # dict_items([('b', 2), ('a', 1)])
        print(type(d1.items()))     # <class 'dict_items'>
        ----------------------------------------------------------------
        可以用循环的方式遍历items
        for k,v in d1.items():
            print(k,v)
        # b 2
        # a 1
        """
        pass

    def keys(self): # real signature unknown; restored from __doc__
        """
        D.keys() -> a set-like object providing a view on D's keys
        获取字典所有key,以一个类似于列表的东西包起来,是一个dict_keys对象
        可以遍历所有value
        ------------------------------------------------------------------------
        d1 = {'a':1,'b':2}
        print(d1.keys())            # dict_keys(['a', 'b'])
        print(type(d1.keys()))      # <class 'dict_keys'>
        """
        pass

    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
        按照指定的key删除一个字典中的元素
        如果能找到key值,则会返回删除元素的值
        如果找不到指定的key,会报错
        ---------------------------------------------------------------------------
        d1 = {'a':1,'b':2,'c':3}
        print(d1.pop('a'))
        """
        pass

    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.
        随机删除一个字典里的元素,并且以元组的形式将被删除的键值对返回
        如果字典为空则会报错
        -------------------------------------------------------------------------
        d1 = {'a':1,'b':2,'c':3}
        print(d1.popitem())         # 因为是随机删除,每次执行返回值不同
        """
        pass

    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
        1、如果key值在原字典里存在:
        不会被改变
        d1 = {'a':1,'b':2,'c':3}
        d1.setdefault('b')
        print(d1)                   # {'a': 1, 'b': 2, 'c': 3}
        d1.setdefault('b',222)      # {'a': 1, 'b': 2, 'c': 3}
        print(d1)
        -----------------------------------------------------------------------------------
        2、如果key值在原字典里不存在:
        会在字典里添加一个键值对,如果不指定value值,则设置为None
        d1 = {'a':1,'b':2,'c':3}
        d1.setdefault('d')
        print(d1)                   # {'a': 1, 'c': 3, 'd': None, 'b': 2}
        d1.setdefault('e',6)
        # print(d1)                 # {'a': 1, 'c': 3, 'e': 6, 'd': None, 'b': 2}
        """
        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]
        更新一个字典,可以将另一个字典添加进去
        ----------------------------------------------------------------------------
        d1 = {'a':1,'b':2,'c':3}
        d2 = {'d':4,'e':5}
        d1.update(d2)
        print(d1)               # {'c': 3, 'a': 1, 'b': 2, 'e': 5, 'd': 4}
        """
        pass

    def values(self): # real signature unknown; restored from __doc__
        """
        D.values() -> an object providing a view on D's values
        获取字典所有value,以一个类似于列表的东西包起来,是一个dict_values对象
        可以遍历所有value
        -------------------------------------------------------------------------------
        d1 = {'a':1,'b':2}
        print(d1.values())              # dict_values([2, 1])
        print(type(d1.values()))        # <class 'dict_values'>

        """
        pass

示例

dic1 = {}
dic2 = dict()
print(type(dic1),type(dic2))

dic3 = {'k1':{'kk1':1},'k2':{'kk2':'2'}}
dic4 = dic3.copy()

print(dic3['k1'] is dic4['k1'])    # True

dic3['k1']['kk1'] = 'kkk1'
print(dic3)
print(dic4)

s = 'fuyong'
d1 = dict.fromkeys(s)
print(d1)

l = ['a','b','c']
d2 = dict.fromkeys(l,1000)
print(d2)

d1 = {'a':'1,b':2}
print(d1.get('a'))
print(d1.get('c'))

d1 = {'a':1,'b':2}
print(d1.items())
print(type(d1.items()))

for k,v in d1.items():
    print(k,v)

d1 = {'a':1,'b':2}
print(d1.keys())
print(type(d1.keys()))


d1 = {'a':1,'b':2}
print(d1.values())
print(type(d1.values()))


d1 = {'a':1,'b':2,'c':3}
print(d1.pop('a'))

d1 = {'a':1,'b':2,'c':3}
print(d1.popitem())

d1 = {'a':1,'b':2,'c':3}
d1.setdefault('b')
print(d1)
d1.setdefault('b',222)
print(d1)

d1 = {'a':1,'b':2,'c':3}
d1.setdefault('d')
print(d1)
d1.setdefault('e',6)
print(d1)


d1 = {'a':1,'b':2,'c':3}
d2 = {'d':4,'e':5}
d1.update(d2)
print(d1)

  

posted @ 2018-03-22 21:27  人生不如戏  阅读(286)  评论(0编辑  收藏  举报