02序列类型的各自方法


##序列类型 
li=[1,2,3] #用dir查看函数类型 dir(li) 如果有__iter__ 就是可迭代类型

li.append(4)#添加元素到末尾  返回 none
解释 #append(...)
 #   L.append(object) -> None -- append object to end

li.clear()#清除序列所有元素
#clear(...) method of builtins.list instance
#    L.clear() -> None -- remove all items from L

li.copy() #复制出来的id不一样了 用id(object)查询
#copy(...)
#      L.copy() -> list -- a shallow copy of L

li.count(1)#计算列表里面的元素出现的次数
#count(...) method of builtins.list instance
#   L.count(value) -> integer -- return number of occurrences of value

help(help(li.extend) #可迭代的参数,依次添加到末尾
#extend(...) method of builtins.list instance
#   L.extend(iterable) -> None -- extend list by appending elements from the iterable

li.index(3) #默认返回元素第一次出现的位子,可以添加一个查找范围
    #index(...) method of builtins.list instance
   #  L.index(value, [start, [stop]]) -> integer -- return first index of value.
    #  Raises ValueError if the value is not present.

 li.insert(3,'zhouxiaoyou')#指定索引插入元素
    
# insert(...) method of builtins.list instance
#    L.insert(index, object) -- insert object before index


li.pop() #默认删除最后一个元素,也可以指定索引删除


li.remove(2)#指定删除.

    
li.reverse()#翻转


li.sort()#默认以ascii码排序。
  #   sort(...) method of builtins.list instance
   # L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*

#sorted()
#reversed

    

##元组打的方法
tu=1,2
tu.count(1)
tu.index(1)   

##字符串的方法

>>> s='565655656988781216'
    
>>> s.count('5')
    
5
>>> s.count('0')
    
0

>>> s.endswith('6')#以什么结尾
    
True
>>> s.endswith('5')
    
False


>>> s.startswith('sd')#以什么开始
    
False
>>> s.startswith('sd\\56')
    
False
>>> s.startswith('56')
    
True


s.find('5') #查找元素的索引位置
>>> s.find('5')
    
0
>>> s.find('z')
    
-1
>>> s.find('5',3)
    
4

              
s.index()

 


s.isalpha()


s.isdigit () :测试是否全是数字,都是数字则返回 True 否则返回 False.
              
s.islower () :测试是否全是小写
              
s.isupper () :测试是否全是大写
              
s.lower () :将字符串转为小写
              
s.upper () :将字符串转为大写
              
s.replace (x,y) :子串替换,在字符串s中出现字符串x的任意位置都用y进行替换
              
s.split():返回一系列用空格分割的字符串列表
              
s.split(a,b):a,b为可选参数,a是将要分割的字符串,b是说明最多要分割几个
              
s='c d df r af d gd d gdg '
>>> s.split('d')
    
['c ', ' ', 'f r af ', ' g', ' ', ' g', 'g ']
>>> s.split(' ')
    
['c', 'd', 'df', 'r', 'af', 'd', 'gd', 'd', 'gdg', '']
>>> s.split('d',1)
    
['c ', ' df r af d gd d gdg ']
>>> s.split('d',-1)
    
['c ', ' ', 'f r af ', ' g', ' ', ' g', 'g ']
>>> s.split('d',-3)
    
['c ', ' ', 'f r af ', ' g', ' ', ' g', 'g ']

 

 

 

 

 

 

 

 

 


              

 

 

 


    

posted on 2018-02-09 23:08  建熊哟哟  阅读(113)  评论(0编辑  收藏  举报

导航