Python基础数据类型之字典Dict

一、字典

定义:{key1:value1,key2:value2},key-value结构,key必须可hash
特性:
1.可存放多个值
2.可修改指定key对应的值,可变
3.无

二、字典的定义

#!/usr/bin/env python
# -*- coding: utf8 -*-
# __Author: "Skiler Hao"
# date: 2017/5/24 16:49

# 只能给多个key赋同一个值,不写100,默认为none
dict1 = {}.fromkeys(['a', 'b', 'c'], 100)

# 利用类来实例化dict
person = dict({"name": "sb", 'age': 18})
dict2 = dict((['a', 'b'], ['age', 18]))
dict3 = dict((('a', 'b'), ('age', 18)))
dict4 = dict(name='skiler',age=18)
# 直接定义字典 person2 = {"name": "sb", 'age': 18}

 三、字典的文档方法

#!/usr/bin/env python
# -*- coding: utf8 -*-
# __Author: "Skiler Hao"
# date: 2017/5/24 16:49

class dict(object):
    """
    dict() -> new empty dictionary 创建一个空字典

    dict(mapping) -> new dictionary initialized from a mapping object's
        (key, value) pairs  # 初始化一个字典,从一个 (键,值)对 的mapping对象
    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)
        使用关键字参数创建字典
    """

    ####################################  创建  ######################################

    @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 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
        如果key不存在,则创建,如果存在,则返回已存在的值且不修改
        """
        pass

    ####################################  删  ######################################

    def clear(self): # real signature unknown; restored from __doc__
        """ D.clear() -> None.  Remove all items from D. 清除字典所有的内容"""
        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,指定d的话,返回d,否则返回KeyError
        """
        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.
        随意删除一个键值对,如果字典为None的,会抛出KeyError异常
        """
        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]
        把字典E的键/值对更新到dict里
        """
        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获取指定的值,如果没有的话,会返回None"""
        pass

    def items(self): # real signature unknown; restored from __doc__
        """ D.items() -> a set-like object providing a view on D's items 所有项的列表形式,列表是(键,值)"""
        passdef keys(self): # real signature unknown; restored from __doc__
        """ D.keys() -> a set-like object providing a view on D's keys 其键的列表"""
        pass

    def values(self): # real signature unknown; restored from __doc__
        """ D.values() -> an object providing a view on D's values 其值的列表"""
        pass


    ####################################  拷贝  ######################################
    def copy(self): # real signature unknown; restored from __doc__
        """ D.copy() -> a shallow copy of D """
        pass

 

 
posted @ 2017-05-24 18:47  skiler  阅读(147)  评论(0编辑  收藏  举报