1. 程式人生 > >數據類型方法

數據類型方法

表示 slow digi gets 副本 xrange contains == 它的

字符串是一個類,"hello world"是它的對象

常用方法:

  • 移除空白
  • 分割
  • 長度
  • 索引
  • 切片
技術分享
class str(basestring):

    def capitalize(self):  
        """ 首字母變大寫 """
             (返回副本)

    def center(self, width, fillchar=None):  
        """ 內容居中,width:總長度;fillchar:空白處填充內容,默認無 """
       
    def count(self, sub, start=None, end=None):  
        
""" 子序列個數 """ def decode(self, encoding=None, errors=None): """ 解碼 """ def encode(self, encoding=None, errors=None): """ 編碼,針對unicode """ def endswith(self, suffix, start=None, end=None): """ 是否以 xxx 結束 """ def expandtabs(self, tabsize=None):
""" 將tab轉換成空格,默認一個tab轉換成8個空格 """ (返回副本) def find(self, sub, start=None, end=None): """ 尋找子序列位置,如果沒找到,返回 -1 """ def format(*args, **kwargs): # known special case of str.format """ 字符串格式化,動態參數,將函數式編程時細說 """ def index(self, sub, start=None, end=None):
""" 子序列位置,如果沒找到,報錯 """ def isalnum(self): """ 是否是字母和數字 """ def isalpha(self): """ 是否是字母 """ def isdigit(self): """ 是否是數字 """ def islower(self): """ 是否小寫 """ def isspace(self): def istitle(self): def isupper(self): def join(self, iterable): """ 連接 """ def ljust(self, width, fillchar=None): """ 內容左對齊,右側填充 """ def lower(self): """ 變小寫 """ (返回副本) def lstrip(self, chars=None): """ 移除左側空白 """ (返回副本) def partition(self, sep): """ 分割,前,中,後三部分 """ def replace(self, old, new, count=None): """ 替換 """ (返回副本) def rfind(self, sub, start=None, end=None): def rindex(self, sub, start=None, end=None): def rjust(self, width, fillchar=None): def rpartition(self, sep): def rsplit(self, sep=None, maxsplit=None): def rstrip(self, chars=None): (返回副本) def split(self, sep=None, maxsplit=None): """ 分割, maxsplit最多分割幾次 """ def splitlines(self, keepends=False): """ 根據換行分割 """ def startswith(self, prefix, start=None, end=None): """ 是否起始 """ def strip(self, chars=None): """ 移除兩端空白 """ (返回副本) def swapcase(self): """ 大寫變小寫,小寫變大寫 """ def title(self): def translate(self, table, deletechars=None): """ 轉換,需要先做一個對應表,最後一個表示刪除字符集合 intab = "aeiou" outtab = "12345" trantab = maketrans(intab, outtab) str = "this is string example....wow!!!" print str.translate(trantab, ‘xm‘) """ (返回副本) def upper(self): (返回副本) def zfill(self, width): """方法返回指定長度的字符串,原字符串右對齊,前面填充0。""" def _formatter_field_name_split(self, *args, **kwargs): # real signature unknown pass def _formatter_parser(self, *args, **kwargs): # real signature unknown pass def __add__(self, y): """ x.__add__(y) <==> x+y """ pass def __contains__(self, y): """ x.__contains__(y) <==> y in x """ pass def __eq__(self, y): """ x.__eq__(y) <==> x==y """ pass def __format__(self, format_spec): """ S.__format__(format_spec) -> string Return a formatted version of S as described by format_spec. """ return "" def __getattribute__(self, name): """ x.__getattribute__(‘name‘) <==> x.name """ pass def __getitem__(self, y): """ x.__getitem__(y) <==> x[y] """ pass def __getnewargs__(self, *args, **kwargs): # real signature unknown pass def __getslice__(self, i, j): """ x.__getslice__(i, j) <==> x[i:j] Use of negative indices is not supported. """ pass def __ge__(self, y): """ x.__ge__(y) <==> x>=y """ pass def __gt__(self, y): """ x.__gt__(y) <==> x>y """ pass def __hash__(self): """ x.__hash__() <==> hash(x) """ pass def __init__(self, string=‘‘): # known special case of str.__init__ """ str(object=‘‘) -> string Return a nice string representation of the object. If the argument is a string, the return value is the same object. # (copied from class doc) """ pass def __len__(self): """ x.__len__() <==> len(x) """ pass def __le__(self, y): """ x.__le__(y) <==> x<=y """ pass def __lt__(self, y): """ x.__lt__(y) <==> x<y """ pass def __mod__(self, y): """ x.__mod__(y) <==> x%y """ pass def __mul__(self, n): """ x.__mul__(n) <==> x*n """ pass @staticmethod # known case of __new__ def __new__(S, *more): """ T.__new__(S, ...) -> a new object with type S, a subtype of T """ pass def __ne__(self, y): """ x.__ne__(y) <==> x!=y """ pass def __repr__(self): """ x.__repr__() <==> repr(x) """ pass def __rmod__(self, y): """ x.__rmod__(y) <==> y%x """ pass def __rmul__(self, n): """ x.__rmul__(n) <==> n*x """ pass def __sizeof__(self): """ S.__sizeof__() -> size of S in memory, in bytes """ pass def __str__(self): """ x.__str__() <==> str(x) """ pass str
str方法

list方法:

基本操作:

  • 索引
  • 切片
  • 追加
  • 刪除
  • 長度
  • 切片
  • 循環
  • 包含
技術分享
class list(object):
    """
    list() -> new empty list
    list(iterable) -> new list initialized from iterable‘s items
    """
    def append(self, p_object): # real signature unknown; restored from __doc__
        """ L.append(object) -- append object to end """
        pass

    def count(self, value): # real signature unknown; restored from __doc__
        """ L.count(value) -> integer -- return number of occurrences of value """
        return 0

    def extend(self, iterable): # real signature unknown; restored from __doc__
        """ L.extend(iterable) -- extend list by appending elements from the iterable """
        pass

    def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
        """
        L.index(value, [start, [stop]]) -> integer -- return first index of value.
        Raises ValueError if the value is not present.
        """
        return 0

    def insert(self, index, p_object): # real signature unknown; restored from __doc__
        """ L.insert(index, object) -- insert object before index """
        pass

    def pop(self, index=None): # real signature unknown; restored from __doc__
        """
        L.pop([index]) -> item -- remove and return item at index (default last).
        Raises IndexError if list is empty or index is out of range.
        """
        pass

    def remove(self, value): # real signature unknown; restored from __doc__
        """
        L.remove(value) -- remove first occurrence of value.
        Raises ValueError if the value is not present.
        """
        pass

    def reverse(self): # real signature unknown; restored from __doc__
        """ L.reverse() -- reverse *IN PLACE* """
        pass

    def sort(self, cmp=None, key=None, reverse=False): # real signature unknown; restored from __doc__
        """
        L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
        cmp(x, y) -> -1, 0, 1
        """
        pass

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

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

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

    def __delslice__(self, i, j): # real signature unknown; restored from __doc__
        """
        x.__delslice__(i, j) <==> del x[i:j]
                   
                   Use of negative indices is not supported.
        """
        pass

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

    def __getattribute__(self, name): # real signature unknown; restored from __doc__
        """ x.__getattribute__(‘name‘) <==> x.name """
        pass

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

    def __getslice__(self, i, j): # real signature unknown; restored from __doc__
        """
        x.__getslice__(i, j) <==> x[i:j]
                   
                   Use of negative indices is not supported.
        """
        pass

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

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

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

    def __imul__(self, y): # real signature unknown; restored from __doc__
        """ x.__imul__(y) <==> x*=y """
        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): # real signature unknown; restored from __doc__
        """ x.__iter__() <==> iter(x) """
        pass

    def __len__(self): # real signature unknown; restored from __doc__
        """ x.__len__() <==> len(x) """
        pass

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

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

    def __mul__(self, n): # real signature unknown; restored from __doc__
        """ x.__mul__(n) <==> x*n """
        pass

    @staticmethod # known case of __new__
    def __new__(S, *more): # real signature unknown; restored from __doc__
        """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
        pass

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

    def __repr__(self): # real signature unknown; restored from __doc__
        """ x.__repr__() <==> repr(x) """
        pass

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

    def __rmul__(self, n): # real signature unknown; restored from __doc__
        """ x.__rmul__(n) <==> n*x """
        pass

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

    def __setslice__(self, i, j, y): # real signature unknown; restored from __doc__
        """
        x.__setslice__(i, j, y) <==> x[i:j]=y
                   
                   Use  of negative indices is not supported.
        """
        pass

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

    __hash__ = None

list
列表方法

元組:帶上枷鎖的列表,方法同列表,但元組不可更改

如果想更新元組:temp=(1,2,3,4) temp=temp(:2)+(‘hello‘,)+temp(2:)

字典:{鍵:值,鍵:值。。。}

創建字典:
person = {"name": "mr.wu", ‘age‘: 18}
或
person = dict({"name": "mr.wu", ‘age‘: 18})

常用操作:

  • 索引
  • 新增
  • 刪除 del dict[key]
  • 鍵、值、鍵值對
  • 循環
  • 長度
for i in 字典:
    print(i) #默認輸出key
for k,v in 字典:
    print(k)
    print(v)

其他:

enumrate 為可叠代的對象添加序號
li = [11,22,33]
for k,v in enumerate(li, 1):   #默認從0開始自增,這裏是從1開始自增
    print(k,v)  #打印 1,11    2,22    3,33

range和xrange

指定範圍,生成指定的數字 2.7中range(1,10),直接就創建1-9,xrange(1,10),只有在叠代的時候才創建1-9,提高效率
print range(1, 10)
# 結果:[1, 2, 3, 4, 5, 6, 7, 8, 9]
 
print range(1, 10, 2)
# 結果:[1, 3, 5, 7, 9]
 
print range(30, 0, -2)
# 結果:[30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2]  

3.5中的range就相當於2.7中的xrange


  

數據類型方法