python常用数据类型内置方法介绍

  熟练掌握python常用数据类型内置方法是每个初学者必须具备的内功。

  下面介绍了python常用的集中数据类型及其方法,点开源代码,其中对主要方法都进行了中文注释。

一、整型

a = 100

a.xxx()

class int(object):

    def bit_length(self):
        ##如果将某个整数用2进制表示,返回这个2进制所占bit位数。
        return 0

    def conjugate(self, *args, **kwargs):
        ##共轭复数

    @classmethod # known case
    def from_bytes(cls, bytes, byteorder, *args, **kwargs):
        ##好像进制转换有关,傻傻搞不懂,百度也没有答案
        pass

    def to_bytes(self, length, byteorder, *args, **kwargs):
        ##上面那个的反向方法
        pass

    def __abs__(self, *args, **kwargs):
        ##数学函数:绝对值
        """ abs(self) """
        pass

    def __add__(self, *args, **kwargs):
        ##加法
        """ Return self+value. """
        pass

    def __and__(self, *args, **kwargs):
        ##逻辑与运算
        """ Return self&value. """
        pass

    def __bool__(self, *args, **kwargs):
        ##
        """ self != 0 """
        pass

    def __ceil__(self, *args, **kwargs):
        """ Ceiling of an Integral returns itself. """
        pass

    def __divmod__(self, *args, **kwargs):
        """ Return divmod(self, value). """
        pass

    def __eq__(self, *args, **kwargs):
        """ Return self==value. """
        pass

    def __float__(self, *args, **kwargs):
        """ float(self) """
        pass

    def __floordiv__(self, *args, **kwargs):
        """ Return self//value. """
        pass

    def __floor__(self, *args, **kwargs):
        """ Flooring an Integral returns itself. """
        pass

    def __format__(self, *args, **kwargs):
        pass

    def __getattribute__(self, *args, **kwargs):
        """ Return getattr(self, name). """
        pass

    def __getnewargs__(self, *args, **kwargs):
        pass

    def __ge__(self, *args, **kwargs):
        """ Return self>=value. """
        pass

    def __gt__(self, *args, **kwargs):
        """ Return self>value. """
        pass

    def __hash__(self, *args, **kwargs):
        """ Return hash(self). """
        pass

    def __index__(self, *args, **kwargs):
        """ Return self converted to an integer, if self is suitable for use as an index into a list. """
        pass

    def __init__(self, x, base=10):
       ##构造方法,可以指定进制,采用base = 进制数的形式。

    def __int__(self, *args, **kwargs):
        """ int(self) """
        pass

    def __invert__(self, *args, **kwargs):
        """ ~self """
        pass

    def __le__(self, *args, **kwargs):
        """ Return self<=value. """
        pass

    def __lshift__(self, *args, **kwargs):
        """ Return self<<value. """
        pass

    def __lt__(self, *args, **kwargs):
        """ Return self<value. """
        pass

    def __mod__(self, *args, **kwargs):
        """ Return self%value. """
        pass

    def __mul__(self, *args, **kwargs):
        """ Return self*value. """
        pass

    def __neg__(self, *args, **kwargs):
        """ -self """
        pass

    @staticmethod # known case of __new__
    def __new__(*args, **kwargs):
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass

    def __ne__(self, *args, **kwargs):
        """ Return self!=value. """
        pass

    def __or__(self, *args, **kwargs):
        """ Return self|value. """
        pass

    def __pos__(self, *args, **kwargs):
        """ +self """
        pass

    def __pow__(self, *args, **kwargs):
        """ Return pow(self, value, mod). """
        pass

    def __radd__(self, *args, **kwargs): #
        """ Return value+self. """
        pass

    def __rand__(self, *args, **kwargs):
        """ Return value&self. """
        pass

    def __rdivmod__(self, *args, **kwargs):
        """ Return divmod(value, self). """
        pass

    def __repr__(self, *args, **kwargs):
        """ Return repr(self). """
        pass

    def __rfloordiv__(self, *args, **kwargs):
        """ Return value//self. """
        pass

    def __rlshift__(self, *args, **kwargs):
        """ Return value<<self. """
        pass

    def __rmod__(self, *args, **kwargs):
        """ Return value%self. """
        pass

    def __rmul__(self, *args, **kwargs):
        """ Return value*self. """
        pass

    def __ror__(self, *args, **kwargs):
        """ Return value|self. """
        pass

    def __round__(self, *args, **kwargs):
        """
        Rounding an Integral returns itself.
        Rounding with an ndigits argument also returns an integer.
        """
        pass

    def __rpow__(self, *args, **kwargs):
        """ Return pow(value, self, mod). """
        pass

    def __rrshift__(self, *args, **kwargs):
        ##左移
        """ Return value>>self. """
        pass

    def __rshift__(self, *args, **kwargs):
        """ Return self>>value. """
        pass

    def __rsub__(self, *args, **kwargs):
        """ Return value-self. """
        pass

    def __rtruediv__(self, *args, **kwargs):
        """ Return value/self. """
        pass

    def __rxor__(self, *args, **kwargs):
        """ Return value^self. """
        pass

    def __sizeof__(self, *args, **kwargs):
        """ Returns size in memory, in bytes """
        pass

    def __str__(self, *args, **kwargs):
        """ Return str(self). """
        pass

    def __sub__(self, *args, **kwargs):
        """ Return self-value. """
        pass

    def __truediv__(self, *args, **kwargs):
        """ Return self/value. """
        pass

    def __trunc__(self, *args, **kwargs):
        """ Truncating an Integral returns itself. """
        pass

    def __xor__(self, *args, **kwargs):
        """ Return self^value. """
        pass

    denominator = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
    """the denominator of a rational number in lowest terms"""

    imag = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
    """the imaginary part of a complex number"""

    numerator = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
    """the numerator of a rational number in lowest terms"""

    real = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
    """the real part of a complex number"""

整数

INT类型的内置方法多数为数学运算使用,不需太多记忆,随用随查。

二、浮点型(float)

a = 1.424242

class float(object):

    def as_integer_ratio(self):
        ##将一个浮点数表示为最大近似的两个整数的除,例如1.5可以表示为3/2
        pass

    def conjugate(self, *args, **kwargs):
        ##共轭复数
        pass

    def fromhex(self, string):
        ##将一个十六进制的字符串转换为浮点数
        return 0.0

    def hex(self):
        ##将一个浮点数表示为十六进制的字符串
        return ""

    def is_integer(self, *args, **kwargs):
        ##判断某个浮点数是否同时也是整型
        pass

    def __abs__(self, *args, **kwargs):
        """ abs(self) """
        pass

    def __add__(self, *args, **kwargs):
        """ Return self+value. """
        pass

    def __bool__(self, *args, **kwargs):
        """ self != 0 """
        pass

    def __divmod__(self, *args, **kwargs):
        """ Return divmod(self, value). """
        pass

    def __eq__(self, *args, **kwargs):
        """ Return self==value. """
        pass

    def __float__(self, *args, **kwargs):
        """ float(self) """
        pass

    def __floordiv__(self, *args, **kwargs): #
        """ Return self//value. """
        pass

    def __format__(self, format_spec):
        """
        float.__format__(format_spec) -> string

        Formats the float according to format_spec.
        """
        return ""

    def __getattribute__(self, *args, **kwargs):
        """ Return getattr(self, name). """
        pass

    def __getformat__(self, typestr):
        ##这是一个连python官方都不知道干什么的方法,忘记它吧
        return ""

    def __getnewargs__(self, *args, **kwargs):
        pass

    def __ge__(self, *args, **kwargs):
        """ Return self>=value. """
        pass

    def __gt__(self, *args, **kwargs):
        """ Return self>value. """
        pass

    def __hash__(self, *args, **kwargs):
        """ Return hash(self). """
        pass

    def __init__(self, x):
        pass

    def __int__(self, *args, **kwargs):
        """ int(self) """
        pass

    def __le__(self, *args, **kwargs):
        """ Return self<=value. """
        pass

    def __lt__(self, *args, **kwargs):
        """ Return self<value. """
        pass

    def __mod__(self, *args, **kwargs):
        """ Return self%value. """
        pass

    def __mul__(self, *args, **kwargs):
        """ Return self*value. """
        pass

    def __neg__(self, *args, **kwargs):
        """ -self """
        pass

    @staticmethod
    def __new__(*args, **kwargs):
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass

    def __ne__(self, *args, **kwargs):
        """ Return self!=value. """
        pass

    def __pos__(self, *args, **kwargs):
        """ +self """
        pass

    def __pow__(self, *args, **kwargs):
        """ Return pow(self, value, mod). """
        pass

    def __radd__(self, *args, **kwargs):
        """ Return value+self. """
        pass

    def __rdivmod__(self, *args, **kwargs):
        """ Return divmod(value, self). """
        pass

    def __repr__(self, *args, **kwargs):
        """ Return repr(self). """
        pass

    def __rfloordiv__(self, *args, **kwargs):
        """ Return value//self. """
        pass

    def __rmod__(self, *args, **kwargs):
        """ Return value%self. """
        pass

    def __rmul__(self, *args, **kwargs):
        """ Return value*self. """
        pass

    def __round__(self, *args, **kwargs):
        """
        Return the Integral closest to x, rounding half toward even.
        When an argument is passed, work like built-in round(x, ndigits).
        """
        pass

    def __rpow__(self, *args, **kwargs):
        """ Return pow(value, self, mod). """
        pass

    def __rsub__(self, *args, **kwargs):
        """ Return value-self. """
        pass

    def __rtruediv__(self, *args, **kwargs):
        """ Return value/self. """
        pass

    def __setformat__(self, typestr, fmt):
        ##不要用这个方法
        pass

    def __str__(self, *args, **kwargs):
        """ Return str(self). """
        pass

    def __sub__(self, *args, **kwargs):
        """ Return self-value. """
        pass

    def __truediv__(self, *args, **kwargs):
        """ Return self/value. """
        pass

    def __trunc__(self, *args, **kwargs):
        """ Return the Integral closest to x between 0 and x. """
        pass

    imag = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
    """the imaginary part of a complex number"""

    real = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
    """the real part of a complex number"""

float

float和int类似,基本都是数学运算方法,他们都有语法糖,不需要这些冗长的方法。

三、字符串

a = 'hello world'

class str(object):

    def capitalize(self):
        ##首字母大写
        return ""

    def casefold(self):
        ##不知道干啥用
        return ""

    def center(self, width, fillchar=None):
        ##设定宽度,让字符串打印的时候居中,可以设定填补的字符,例如:
        ##———————商品展示—————————
        return ""

    def count(self, sub, start=None, end=None):
        ##在字符串中统计子串的个数,可以设定起始和结束位置。
        return 0

    def encode(self, encoding='utf-8', errors='strict'):
        ##将字符串编码成指定的编码格式,默认为utf-8
        return b""

    def endswith(self, suffix, start=None, end=None):
        ##测试字符串是否以指定的字符串结尾,可以指定判断的起始和结束位置。
        return False

    def expandtabs(self, tabsize=8):
        ##将tab转换成空格,默认一个tab转换成8个空格,可以自己指定转换数量。
        return ""

    def find(self, sub, start=None, end=None):
        ##查找子串在原字符串中的最近的位置下标,可以指定起始位置。,如果没找到,返回 -1
        return 0

    def format(self, *args, **kwargs):
        ##字符串格式化,接收动态参数。例如:
        '''
        s = 'hello {0},this is {1}'
        s.format('andy','jack')
        或者:
        s = 'hello {name1},this is {name2}'
        s.format(name2='jack',name1='andy')
        '''
        pass

    def format_map(self, mapping):
        """
        S.format_map(mapping) -> str

        Return a formatted version of S, using substitutions from mapping.
        The substitutions are identified by braces ('{' and '}').
        """
        return ""

    def index(self, sub, start=None, end=None):
        ##和find方法一样也是查找子串的位置。不同的是,如果没找到会跳出异常。
        return 0

    def isalnum(self):
        ##测试字符串是否全由数字和字母组成,返回True或False。
        return False

    def isalpha(self):
        #测试字符串是否全由字母组成,返回True或False。
        return False

    def isdecimal(self):
        ##测试字符串是否全由10进制数字组成,返回True或False。
        return False

    def isdigit(self):
        ##测试字符串是否全由数字组成,返回True或False。
        return False

    def isidentifier(self):
        ##测试字符串是否由标识符组成,返回True或False。
        return False

    def islower(self):
        ##测试字符串是否全由小写字母组成,返回True或False。
        return False

    def isnumeric(self):

        return False

    def isprintable(self):
        ##判断字符串是否可打印
        return False

    def isspace(self):
        ##判断字符串是否为空白
        return False

    def istitle(self):
        ##判断字符串是否是标题
        return False

    def isupper(self):
         ##判断字符串是否全是大写
        return False

    def join(self, iterable):
        ##极为重要的字符串方法,可以将对象的元素连接起来。例如:
        ##s = ''.join(['a','b','c'])
        ##s = 'abc'
        return ""

    def ljust(self, width, fillchar=None):
        ##字符串左对齐的同时,右边用指定的字符填充指定的宽度。
        return ""

    def lower(self):
        ##将字符串全部变成小写
        return ""

    def lstrip(self, chars=None):
        ##去除字符串左边的东东
        return ""

    def maketrans(self, *args, **kwargs):
        ##一个复杂的字符替换方法
        pass

    def partition(self, sep):
        ##以某个指定的子串为分割处,将字符串分割成各个部分,并返回一个元组。
        pass

    def replace(self, old, new, count=None):
        ##用新的子串替换旧的子串,可以指定数量
        return ""

    def rfind(self, sub, start=None, end=None):
        ##从右往左查找子串
        return 0

    def rindex(self, sub, start=None, end=None):
        ##从右往左查找子串的下标
        return 0

    def rjust(self, width, fillchar=None):
        ##字符串右对齐的同时,左边用指定字符填充指定宽度。
        return ""

    def rpartition(self, sep):
        ##从右往左分割字符串
        pass

    def rsplit(self, sep=None, maxsplit=-1):
        ##从右往左分割
        return []

    def rstrip(self, chars=None):
        ##去除字符串右边的东东
        return ""

    def split(self, sep=None, maxsplit=-1):
        ##极为重要的字符串方法。将字符串以指定字符为标志进行分割,默认是空格。返回一个列表。
        return []

    def splitlines(self, keepends=None):
        ##以行为单位分割字符串
        return []

    def startswith(self, prefix, start=None, end=None):
        ##判断字符串是否由指定的子串开始,可以指定起始位置。
        return False

    def strip(self, chars=None):
        ##极为重要的字符串方法,将字符串的前后指定字符去除,默认是去空格和换行符。
        return ""

    def swapcase(self):
        ##字符串大小写反转
        return ""

    def title(self):
        ##将字符串转换成标题格式
        return ""

    def translate(self, table):
        '''
        借用武神的例子:
        转换,与上面的maketrans方法配合,需要先做一个对应表,
        最后一个表示删除字符集合
        intab = "aeiou"
        outtab = "12345"
        trantab = maketrans(intab, outtab)
        str = "this is string example....wow!!!"
        print str.translate(trantab, 'xm')
        '''
        return ""

    def upper(self):
        ##将字符串全部转换成大写
        return ""

    def zfill(self, width):
        ##返回指定长度的字符串,原字符串右对齐,前面填充0
        return ""

    def __add__(self, *args, **kwargs): # real signature unknown
        """ Return self+value. """
        pass

    def __contains__(self, *args, **kwargs): # real signature unknown
        """ Return key in self. """
        pass

    def __eq__(self, *args, **kwargs): # real signature unknown
        """ Return self==value. """
        pass

    def __format__(self, format_spec): # real signature unknown; restored from __doc__
        """
        S.__format__(format_spec) -> str

        Return a formatted version of S as described by format_spec.
        """
        return ""

    def __getattribute__(self, *args, **kwargs): # real signature unknown
        """ Return getattr(self, name). """
        pass

    def __getitem__(self, *args, **kwargs): # real signature unknown
        """ Return self[key]. """
        pass

    def __getnewargs__(self, *args, **kwargs): # real signature unknown
        pass

    def __ge__(self, *args, **kwargs): # real signature unknown
        """ Return self>=value. """
        pass

    def __gt__(self, *args, **kwargs): # real signature unknown
        """ Return self>value. """
        pass

    def __hash__(self, *args, **kwargs):
        """ Return hash(self). """
        pass

    def __init__(self, value='', encoding=None, errors='strict'):
        ##字符串构造方法
        """
        str(object='') -> str
        str(bytes_or_buffer[, encoding[, errors]]) -> str

        Create a new string object from the given object. If encoding or
        errors is specified, then the object must expose a data buffer
        that will be decoded using the given encoding and error handler.
        Otherwise, returns the result of object.__str__() (if defined)
        or repr(object).
        encoding defaults to sys.getdefaultencoding().
        errors defaults to 'strict'.
        # (copied from class doc)
        """
        pass

    def __iter__(self, *args, **kwargs): # real signature unknown
        """ Implement iter(self). """
        pass

    def __len__(self, *args, **kwargs): # real signature unknown
        """ Return len(self). """
        pass

    def __le__(self, *args, **kwargs): # real signature unknown
        """ Return self<=value. """
        pass

    def __lt__(self, *args, **kwargs): # real signature unknown
        """ Return self<value. """
        pass

    def __mod__(self, *args, **kwargs): # real signature unknown
        """ Return self%value. """
        pass

    def __mul__(self, *args, **kwargs): # real signature unknown
        """ Return self*value.n """
        pass

    @staticmethod # known case of __new__
    def __new__(*args, **kwargs): # real signature unknown
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass

    def __ne__(self, *args, **kwargs): # real signature unknown
        """ Return self!=value. """
        pass

    def __repr__(self, *args, **kwargs): # real signature unknown
        """ Return repr(self). """
        pass

    def __rmod__(self, *args, **kwargs): # real signature unknown
        """ Return value%self. """
        pass

    def __rmul__(self, *args, **kwargs): # real signature unknown
        """ Return self*value. """
        pass

    def __sizeof__(self): # real signature unknown; restored from __doc__
        """ S.__sizeof__() -> size of S in memory, in bytes """
        pass

    def __str__(self, *args, **kwargs): # real signature unknown
        """ Return str(self). """
        pass

字符串的方法

  对于字符串而言,最重要的方法莫过于index    split   join   strip     replace   center    isdigit   count   find   formate   encode 这几个了。字符串如果用得得心应手,那么python数据结构的三分之一你就掌握了。

四、字节类型(byte)

  在Python3以后,字符串和字节类型彻底分开了。两者的区别简单的理解就是:字符串是以字符为单位进行处理的,字节类型是以字节为单位处理的。

  两者在内置方法上几乎一模一样,在使用上只需注意处理的单位不一样即可。

  创建方法:a = bytes('string',encoding='编码类型')

class bytes(object):
    """
    bytes(iterable_of_ints) -> bytes
    bytes(string, encoding[, errors]) -> bytes
    bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer
    bytes(int) -> bytes object of size given by the parameter initialized with null bytes
    bytes() -> empty bytes object

    Construct an immutable array of bytes from:
      - an iterable yielding integers in range(256)
      - a text string encoded using the specified encoding
      - any object implementing the buffer API.
      - an integer
    """
    def capitalize(self):
        # 首字母大写
        pass

    def center(self, width, fillchar=None):
        # 字节居中,两侧填充
        pass

    def count(self, sub, start=None, end=None):
        # 统计个数
        return 0

    def decode(self, *args, **kwargs):
        # 解码
        """
        Decode the bytes using the codec registered for encoding.

          encoding
            The encoding with which to decode the bytes.
          errors
            The error handling scheme to use for the handling of decoding errors.
            The default is 'strict' meaning that decoding errors raise a
            UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
            as well as any other name registered with codecs.register_error that
            can handle UnicodeDecodeErrors.
        """
        pass

    def endswith(self, suffix, start=None, end=None):
        # 判断是否由子串结尾
        return False

    def expandtabs(self, tabsize=8):
        # 替换制表符为空格
        pass

    def find(self, sub, start=None, end=None):
        # 查找子串
        return 0

    @classmethod # known case
    def fromhex(cls, *args, **kwargs):
        # 由十六进制转换过来
        pass

    def hex(self):
        # 转换为16进制
        return ""

    def index(self, sub, start=None, end=None):
        # 查找下标
        return 0

    def isalnum(self):
        # 是否都由字母和数字组成
        return False

    def isalpha(self):
        # 判断是否都由字母组成
        return False

    def isdigit(self):
        # 判断是否都由数字组成
        return False

    def islower(self):
        # 小写判断
        return False

    def isspace(self):
        # 空格判断
        return False

    def istitle(self):
        # 标题判断
        return False

    def isupper(self):
        #大写判断
        return False

    def join(self, *args, **kwargs):
        # 拼接字节
        pass

    def ljust(self, width, fillchar=None):
        # 左对齐
        pass

    def lower(self): 

        # 全部转换为小写
        pass

    def lstrip(self, *args, **kwargs):
        # 左边去空格或指定的字符
        pass

    @staticmethod # known case
    def maketrans(*args, **kwargs):
        # 与另外一个方法配合,转换字节
        pass

    def partition(self, *args, **kwargs):
        # 分割字节
        pass

    def replace(self, *args, **kwargs):
        # 替换字节内容
        pass

    def rfind(self, sub, start=None, end=None):
        # 从右开始查找子串
        return 0

    def rindex(self, sub, start=None, end=None):
        # 从右开始查找下标
        return 0

    def rjust(self, width, fillchar=None):
        # 右对齐
        pass

    def rpartition(self, *args, **kwargs):
        # 从右开始分部
        pass

    def rsplit(self, *args, **kwargs):
        # 从右开始分割
        pass

    def rstrip(self, *args, **kwargs):
        # 脱去字节右边的空格或指定字符
        pass

    def split(self, *args, **kwargs):
        # 分割字符
        pass

    def splitlines(self, *args, **kwargs):
        # 按行分割
        pass

    def startswith(self, prefix, start=None, end=None):
        # 判断是否由子串开始
        return False

    def strip(self, *args, **kwargs):
        # 脱去左右两侧的空格或指定字节
        pass

    def swapcase(self):
        # 大小写转换
        pass

    def title(self):
        # 设置为标题
        pass

    def translate(self, table, deletechars=None):
        # 转换
        pass

    def upper(self):
        # 全部大写
        pass

    def zfill(self, width): # real signature unknown; restored from __doc__
        """
        B.zfill(width) -> copy of B

        Pad a numeric string B with zeros on the left, to fill a field
        of the specified width.  B is never truncated.
        """
        pass

    def __add__(self, *args, **kwargs): # real signature unknown
        """ Return self+value. """
        pass

    def __contains__(self, *args, **kwargs): # real signature unknown
        """ Return key in self. """
        pass

    def __eq__(self, *args, **kwargs): # real signature unknown
        """ Return self==value. """
        pass

    def __getattribute__(self, *args, **kwargs): # real signature unknown
        """ Return getattr(self, name). """
        pass

    def __getitem__(self, *args, **kwargs): # real signature unknown
        """ Return self[key]. """
        pass

    def __getnewargs__(self, *args, **kwargs): # real signature unknown
        pass

    def __ge__(self, *args, **kwargs): # real signature unknown
        """ Return self>=value. """
        pass

    def __gt__(self, *args, **kwargs): # real signature unknown
        """ Return self>value. """
        pass

    def __hash__(self, *args, **kwargs): # real signature unknown
        """ Return hash(self). """
        pass

    def __init__(self, value=b'', encoding=None, errors='strict'): # known special case of bytes.__init__
        """
        bytes(iterable_of_ints) -> bytes
        bytes(string, encoding[, errors]) -> bytes
        bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer
        bytes(int) -> bytes object of size given by the parameter initialized with null bytes
        bytes() -> empty bytes object

        Construct an immutable array of bytes from:
          - an iterable yielding integers in range(256)
          - a text string encoded using the specified encoding
          - any object implementing the buffer API.
          - an integer
        # (copied from class doc)
        """
        pass

    def __iter__(self, *args, **kwargs): # real signature unknown
        """ Implement iter(self). """
        pass

    def __len__(self, *args, **kwargs): # real signature unknown
        """ Return len(self). """
        pass

    def __le__(self, *args, **kwargs): # real signature unknown
        """ Return self<=value. """
        pass

    def __lt__(self, *args, **kwargs): # real signature unknown
        """ Return self<value. """
        pass

    def __mod__(self, *args, **kwargs): # real signature unknown
        """ Return self%value. """
        pass

    def __mul__(self, *args, **kwargs): # real signature unknown
        """ Return self*value.n """
        pass

    @staticmethod # known case of __new__
    def __new__(*args, **kwargs): # real signature unknown
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass

    def __ne__(self, *args, **kwargs): # real signature unknown
        """ Return self!=value. """
        pass

    def __repr__(self, *args, **kwargs): # real signature unknown
        """ Return repr(self). """
        pass

    def __rmod__(self, *args, **kwargs): # real signature unknown
        """ Return value%self. """
        pass

    def __rmul__(self, *args, **kwargs): # real signature unknown
        """ Return self*value. """
        pass

    def __str__(self, *args, **kwargs): # real signature unknown
        """ Return str(self). """
        pass

byte

五、列表(list)

  Python的列表在其他的语言一般称为数组,它是一个有序可重复的元素集合,可嵌套、可迭代、可修改、可分片、可追加、可删除,因此它的内置方法很多。

  创建方式:li1 = [1,2,3,4]或li = []或li = list(xxx)

class list(object):

    def append(self, p_object):
        # 在列表的最后添加一个元素
        pass

    def clear(self):
        # 清空列表内的所有元素
        pass

    def copy(self):
        # 列表的浅拷贝
        return []

    def count(self, value):
        # 统计某个元素在列表内的个数
        return 0

    def extend(self, iterable):
        # 从某个迭代器内获取元素并添加到列表内
        pass

    def index(self, value, start=None, stop=None):
        # 找出第一个匹配的元素的下标,如果列表内没有该元素,触发异常。可以设置
        # 搜索的起始和结束范围
        return 0

    def insert(self, index, p_object):
        # 在指定的下标之前插入元素
        pass

    def pop(self, index=None):
        # 删除指定下标的元素(默认是最后1个元素),并将这个元素作为返回值。
        # 如果列表为空或者下标超出范围,触发异常。
        pass

    def remove(self, value):
        # 删除列表内第一个匹配的元素,如果元素不存在则触发异常。
        pass

    def reverse(self):
        # 反转列表的顺序
        pass

    def sort(self, key=None, reverse=False):
        # 对列表内的元素进行排序,可指定排序关键字,可指定是否逆序
        pass
    # 以下为内部方法
    def __add__(self, *args, **kwargs): # real signature unknown
        """ Return self+value. """
        pass

    def __contains__(self, *args, **kwargs): # real signature unknown
        """ Return key in self. """
        pass

    def __delitem__(self, *args, **kwargs): # real signature unknown
        """ Delete self[key]. """
        pass

    def __eq__(self, *args, **kwargs): # real signature unknown
        """ Return self==value. """
        pass

    def __getattribute__(self, *args, **kwargs): # real signature unknown
        """ Return getattr(self, name). """
        pass

    def __getitem__(self, y): # real signature unknown; restored from __doc__
        """ x.__getitem__(y) <==> x[y] """
        pass

    def __ge__(self, *args, **kwargs): # real signature unknown
        """ Return self>=value. """
        pass

    def __gt__(self, *args, **kwargs): # real signature unknown
        """ Return self>value. """
        pass

    def __iadd__(self, *args, **kwargs): # real signature unknown
        """ Implement self+=value. """
        pass

    def __imul__(self, *args, **kwargs): # real signature unknown
        """ Implement self*=value. """
        pass

    def __init__(self, seq=()): # known special case of list.__init__
        """
        list() -> new empty list
        list(iterable) -> new list initialized from iterable's items
        # (copied from class doc)
        """
        pass

    def __iter__(self, *args, **kwargs): # real signature unknown
        """ Implement iter(self). """
        pass

    def __len__(self, *args, **kwargs): # real signature unknown
        """ Return len(self). """
        pass

    def __le__(self, *args, **kwargs): # real signature unknown
        """ Return self<=value. """
        pass

    def __lt__(self, *args, **kwargs): # real signature unknown
        """ Return self<value. """
        pass

    def __mul__(self, *args, **kwargs): # real signature unknown
        """ Return self*value.n """
        pass

    @staticmethod # known case of __new__
    def __new__(*args, **kwargs): # real signature unknown
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass

    def __ne__(self, *args, **kwargs): # real signature unknown
        """ Return self!=value. """
        pass

    def __repr__(self, *args, **kwargs): # real signature unknown
        """ Return repr(self). """
        pass

    def __reversed__(self): # real signature unknown; restored from __doc__
        """ L.__reversed__() -- return a reverse iterator over the list """
        pass

    def __rmul__(self, *args, **kwargs): # real signature unknown
        """ Return self*value. """
        pass

    def __setitem__(self, *args, **kwargs): # real signature unknown
        """ Set self[key] to value. """
        pass

    def __sizeof__(self): # real signature unknown; restored from __doc__
        """ L.__sizeof__() -- size of L in memory, in bytes """
        pass

    __hash__ = None

list

  列表除了内置方法,经常性的操作还有分片,循环等等,使用方法灵活多变。

六、元组(tuple)

  元组是有序可重复,但不可修改的元素集合,内置方法较少,通常用于储存一些固定的值。

  创建方式: tup = ()或tup = (1,2)或tup = tuple(xxx)。特别需要注意的是,如果元组只有1个元素,则要写成(a,)的形式,要在最后加个逗号。

class tuple(object):

    def count(self, value):
        #  统计某个元素在元组内出现的次数
        return 0

    def index(self, value, start=None, stop=None):
        # 查找第一个匹配到的元素的下标,可以设置查找起始位置。
        # 如果没找到,会触发异常
        return 0
    # 以下为内置方法
    def __add__(self, *args, **kwargs): # real signature unknown
        """ Return self+value. """
        pass

    def __contains__(self, *args, **kwargs): # real signature unknown
        """ Return key in self. """
        pass

    def __eq__(self, *args, **kwargs): # real signature unknown
        """ Return self==value. """
        pass

    def __getattribute__(self, *args, **kwargs): # real signature unknown
        """ Return getattr(self, name). """
        pass

    def __getitem__(self, *args, **kwargs): # real signature unknown
        """ Return self[key]. """
        pass

    def __getnewargs__(self, *args, **kwargs): # real signature unknown
        pass

    def __ge__(self, *args, **kwargs): # real signature unknown
        """ Return self>=value. """
        pass

    def __gt__(self, *args, **kwargs): # real signature unknown
        """ Return self>value. """
        pass

    def __hash__(self, *args, **kwargs): # real signature unknown
        """ Return hash(self). """
        pass

    def __init__(self, seq=()): # known special case of tuple.__init__
        """
        tuple() -> empty tuple
        tuple(iterable) -> tuple initialized from iterable's items

        If the argument is a tuple, the return value is the same object.
        # (copied from class doc)
        """
        pass

    def __iter__(self, *args, **kwargs): # real signature unknown
        """ Implement iter(self). """
        pass

    def __len__(self, *args, **kwargs): # real signature unknown
        """ Return len(self). """
        pass

    def __le__(self, *args, **kwargs): # real signature unknown
        """ Return self<=value. """
        pass

    def __lt__(self, *args, **kwargs): # real signature unknown
        """ Return self<value. """
        pass

    def __mul__(self, *args, **kwargs): # real signature unknown
        """ Return self*value.n """
        pass

    @staticmethod # known case of __new__
    def __new__(*args, **kwargs): # real signature unknown
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass

    def __ne__(self, *args, **kwargs): # real signature unknown
        """ Return self!=value. """
        pass

    def __repr__(self, *args, **kwargs): # real signature unknown
        """ Return repr(self). """
        pass

    def __rmul__(self, *args, **kwargs): # real signature unknown
        """ Return self*value. """
        pass

tuple

七、字典(dict)

  字典是无序的,不可重复的键值对的集合。其键必须为不可修改的类型,不能是列表或字典,通常用字符串做键,而其键值则可以为任意类型。

  创建方法:dic = {}或dic = {"k1":"v1"}或dic = dict(iterator)

class dict(object):

    def clear(self):
        # 删除字典中的所有元素
        pass

    def copy(self):
        # 字典的浅拷贝
        pass

    @staticmethod
    def fromkeys(*args, **kwargs):
        # 从迭代器中获取键值对用于生成字典
        pass

    def get(self, k, d=None):
        # 获取字典中某个键的值。如果该键不存在,不会触发异常。
        pass

    def items(self):
        # 字典所有键值对的组合
        pass

    def keys(self):
        # 字典中所有键的集合
        pass

    def pop(self, k, d=None):
        # 删除指定的键,并返回它,如果该键不存在,触发异常
        pass

    def popitem(self):
        # 随机删除键值对,并返回它
        pass

    def setdefault(self, k, d=None):
        # 如果key不存在,则创建,如果存在,则返回已存在的值且不修改
        pass

    def update(self, E=None, **F):
        # 用某个新的字典或迭代器来更新当前的字典。如果新字典的键在
        # 旧的字典中不存在,则直接添加进去;如果新字典的键在旧的字典中
        # 已经存在,则用新的键值去覆盖,注意是覆盖旧的键值,旧的键值中的数据
        # 结构等等都将发生变化,这个过程是重写而不是融合。
        pass

    def values(self):
        # 字典中的键值的集合
        pass

    def __contains__(self, *args, **kwargs): # real signature unknown
        """ True if D has a key k, else False. """
        pass

    def __delitem__(self, *args, **kwargs): # real signature unknown
        """ Delete self[key]. """
        pass

    def __eq__(self, *args, **kwargs): # real signature unknown
        """ Return self==value. """
        pass

    def __getattribute__(self, *args, **kwargs): # real signature unknown
        """ Return getattr(self, name). """
        pass

    def __getitem__(self, y): # real signature unknown; restored from __doc__
        """ x.__getitem__(y) <==> x[y] """
        pass

    def __ge__(self, *args, **kwargs): # real signature unknown
        """ Return self>=value. """
        pass

    def __gt__(self, *args, **kwargs): # real signature unknown
        """ Return self>value. """
        pass

    def __init__(self, seq=None, **kwargs): # known special case of dict.__init__
        """
        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)
        # (copied from class doc)
        """
        pass

    def __iter__(self, *args, **kwargs): # real signature unknown
        """ Implement iter(self). """
        pass

    def __len__(self, *args, **kwargs): # real signature unknown
        """ Return len(self). """
        pass

    def __le__(self, *args, **kwargs): # real signature unknown
        """ Return self<=value. """
        pass

    def __lt__(self, *args, **kwargs): # real signature unknown
        """ Return self<value. """
        pass

    @staticmethod # known case of __new__
    def __new__(*args, **kwargs): # real signature unknown
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass

    def __ne__(self, *args, **kwargs): # real signature unknown
        """ Return self!=value. """
        pass

    def __repr__(self, *args, **kwargs): # real signature unknown
        """ Return repr(self). """
        pass

    def __setitem__(self, *args, **kwargs): # real signature unknown
        """ Set self[key] to value. """
        pass

    def __sizeof__(self): # real signature unknown; restored from __doc__
        """ D.__sizeof__() -> size of D in memory, in bytes """
        pass

    __hash__ = None

dict

八、集合(SET)

  python中的集合是无序,不重复的元素集合
  创建方法:
    se1 = {'123','ab'}
    se2 = set('123','ab')

  注意了:s = {}默认的是创建空的字典,不是创建集合。

class set(object):

    def add(self, *args, **kwargs):
        #为集合增加元素
        pass

    def clear(self, *args, **kwargs):
        ##清空集合
        pass

    def copy(self, *args, **kwargs):
        ##浅拷贝一个集合
        pass

    def difference(self, *args, **kwargs):
        ##求差集。a中存在,b中不存在 ,数学符号为减号-。
        ##a.difference(b)或set(a) - set(b)
        pass

    def difference_update(self, *args, **kwargs):
        ##求差集,并更新。
        ##方法后面带有update的是将求得的结果更新原来的集合
        pass

    def discard(self, *args, **kwargs):
        ##移除指定元素,但不报错,不弹出异常。
        pass

    def intersection(self, *args, **kwargs):
        ##求交集,数学符号为&。
        pass

    def intersection_update(self, *args, **kwargs):
        ##求交集,并更新。
        pass

    def isdisjoint(self, *args, **kwargs):
        ##如果两个集合没有交集,那么返回True,否则False
        pass

    def issubset(self, *args, **kwargs):
        ##判断是否是子集
        pass

    def issuperset(self, *args, **kwargs):
        ##判断是否是父集
        pass

    def pop(self, *args, **kwargs):
        ##随机删除一个元素,并返回此元素,慎用。
        pass

    def remove(self, *args, **kwargs):
        ##删除指定元素,元素不存在会报出异常
        pass

    def symmetric_difference(self, *args, **kwargs):
        ##(a中存在,b中不存在)与(a中不存在,b中存在),也就是对称差集
        pass

    def symmetric_difference_update(self, *args, **kwargs):
        ##对称差集,更新
        pass

    def union(self, *args, **kwargs):
        ##求并集,数学符号|
        pass

    def update(self, *args, **kwargs):
        ##接受一个迭代器,并用它更新集合
        pass
    ##以下是内部方法,基本不用。
    def __and__(self, *args, **kwargs): # real signature unknown
        """ Return self&value. """
        pass

    def __contains__(self, y): # real signature unknown; restored from __doc__
        """ x.__contains__(y) <==> y in x. """
        pass

    def __eq__(self, *args, **kwargs): # real signature unknown
        """ Return self==value. """
        pass

    def __getattribute__(self, *args, **kwargs): # real signature unknown
        """ Return getattr(self, name). """
        pass

    def __ge__(self, *args, **kwargs): # real signature unknown
        """ Return self>=value. """
        pass

    def __gt__(self, *args, **kwargs): # real signature unknown
        """ Return self>value. """
        pass

    def __iand__(self, *args, **kwargs): # real signature unknown
        """ Return self&=value. """
        pass

    def __init__(self, seq=()): # known special case of set.__init__
        """
        set() -> new empty set object
        set(iterable) -> new set object

        Build an unordered collection of unique elements.
        # (copied from class doc)
        """
        pass

    def __ior__(self, *args, **kwargs): # real signature unknown
        """ Return self|=value. """
        pass

    def __isub__(self, *args, **kwargs): # real signature unknown
        """ Return self-=value. """
        pass

    def __iter__(self, *args, **kwargs): # real signature unknown
        """ Implement iter(self). """
        pass

    def __ixor__(self, *args, **kwargs): # real signature unknown
        """ Return self^=value. """
        pass

    def __len__(self, *args, **kwargs): # real signature unknown
        """ Return len(self). """
        pass

    def __le__(self, *args, **kwargs): # real signature unknown
        """ Return self<=value. """
        pass

    def __lt__(self, *args, **kwargs): # real signature unknown
        """ Return self<value. """
        pass

    @staticmethod # known case of __new__
    def __new__(*args, **kwargs): # real signature unknown
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass

    def __ne__(self, *args, **kwargs): # real signature unknown
        """ Return self!=value. """
        pass

    def __or__(self, *args, **kwargs): # real signature unknown
        """ Return self|value. """
        pass

    def __rand__(self, *args, **kwargs): # real signature unknown
        """ Return value&self. """
        pass

    def __reduce__(self, *args, **kwargs): # real signature unknown
        """ Return state information for pickling. """
        pass

    def __repr__(self, *args, **kwargs): # real signature unknown
        """ Return repr(self). """
        pass

    def __ror__(self, *args, **kwargs): # real signature unknown
        """ Return value|self. """
        pass

    def __rsub__(self, *args, **kwargs): # real signature unknown
        """ Return value-self. """
        pass

    def __rxor__(self, *args, **kwargs): # real signature unknown
        """ Return value^self. """
        pass

    def __sizeof__(self): # real signature unknown; restored from __doc__
        """ S.__sizeof__() -> size of S in memory, in bytes """
        pass

    def __sub__(self, *args, **kwargs): # real signature unknown
        """ Return self-value. """
        pass

    def __xor__(self, *args, **kwargs): # real signature unknown
        """ Return self^value. """
        pass

    __hash__ = None

set集合

上一篇:Python入门-数据类型


下一篇:python 基本数据类型分析