hmzone 发表于 2018-8-3 11:36:21

python学习总结五(python序列)

  一 序列
  这些类型是由一些成员共同组成的一个序列整体,所以统称为序列。
  元组 ,列表,字符串。
  1 .标准的操作符 都能应用序列。
  2.序列类的操作符
  ①成员关系操作符(in not in)
  成员关系符就是判断一个字符是否属于这个字符串,再就是这个字符串是否属于这个元组,或者列表。返回值也是布尔值(True,Flase)。
  ②连接操作符(“+”)
  序列 + 序列
  可以把2个序列组合到一个新的序列中去。
  >>> ["a",2,5,"hou"] + ["zai", " cun"]
  
['a', 2, 5, 'hou', 'zai', ' cun']
  ③重复操作符(“*”)
  序列 * 整数
  >>> ["a",2,5,"hou"]* 3
  
['a', 2, 5, 'hou', 'a', 2, 5, 'hou', 'a', 2, 5, 'hou']
  ④切片操作符([][:][::])
  序列[索引]

倒索引
-5
-4
-3
-2
-1
数值
5
6
7
8
10
正索引
0
1
2
3
4  >>> list=["hou",2,"22",'2222']
  
>>> list   正向 0开始
  
2
  
>>> list[-1]      -1 结束
  
'2222'
  
>>> list[-4]
  
'hou'
  
>>> list[::-4]反向数值的反 查 2222,22,2,hou    -1 为hou
  
['2222']
  
>>> list从 索引 1到4每次 跳2个 默认不输入为1
  

  3 序列的内建 函数
  list( ) 把一个元组 ,或者字符串转换为一个列表
  tuple() 把一个列表。。。相反。
  max()
  min()    最大最小 没啥可说
  len () 统计长度的
  注意 :len(list) - 1 ==list [-1](正向最后)那个索引
  

倒索引
-5
-4
-3
-2
-1
len( )=5
5
6
7
8
10
正索引
0
1
2
3
4  reversed()
  sum()
  zip()
  二 字符串
  字符串是python 最常见的类型。可以用引号 来创建 python的双引号和单引号一样的 ,shell 是 转义的只能在双引号里面表示。
  收集常用的Python内置的各种字符串处理 函数的使用方法
  str='python String function'


[*]生成字符串变量str='python String function'  


[*]字符串长度获取:len(str)  
例:print '%s length=%d' % (str,len(str))
  


[*]字母处理  
全部大写:str.upper()
  
全部小写:str.lower()
  
大小写互换:str.swapcase()
  
首字母大写,其余小写:str.capitalize()
  
首字母大写:str.title()
  
print '%s lower=%s' % (str,str.lower())
  
print '%s upper=%s' % (str,str.upper())
  
print '%s swapcase=%s' % (str,str.swapcase())
  
print '%s capitalize=%s' % (str,str.capitalize())

  
print '%s>
[*]  格式化相关
  
获取固定长度,右对齐,左边不够用空格补齐:str.ljust(width)
  
获取固定长度,左对齐,右边不够用空格补齐:str.ljust(width)
  
获取固定长度,中间对齐,两边不够用空格补齐:str.ljust(width)
  
获取固定长度,右对齐,左边不足用0补齐
  
print '%s ljust=%s' % (str,str.ljust(20))
  
print '%s rjust=%s' % (str,str.rjust(20))
  
print '%s center=%s' % (str,str.center(20))
  
print '%s zfill=%s' % (str,str.zfill(20))

[*]  字符串搜索相关
  
搜索指定字符串,没有返回-1:str.find('t')
  
指定起始位置搜索:str.find('t',start)
  
指定起始及结束位置搜索:str.find('t',start,end)
  
从右边开始查找:str.rfind('t')
  
搜索到多少个指定字符串:str.count('t')
  
上面所有方法都可用index代替,不同的是使用index查找不到会抛异常,而find返回-1
  
print '%s find nono=%d' % (str,str.find('nono'))
  
print '%s find t=%d' % (str,str.find('t'))
  
print '%s find t from %d=%d' % (str,1,str.find('t',1))
  
print '%s find t from %d to %d=%d' % (str,1,2,str.find('t',1,2))
  
#print '%s index nono ' % (str,str.index('nono',1,2))
  
print '%s rfind t=%d' % (str,str.rfind('t'))
  
print '%s count t=%d' % (str,str.count('t'))

[*]  字符串替换相关
  
替换old为new:str.replace('old','new')
  
替换指定次数的old为new:str.replace('old','new',maxReplaceTimes)
  
print '%s replace t to *=%s' % (str,str.replace('t', '*'))
  
print '%s replace t to *=%s' % (str,str.replace('t', '*',1))

[*]  字符串去空格及去指定字符
  
去两边空格:str.strip()
  
去左空格:str.lstrip()
  
去右空格:str.rstrip()
  
去两边字符串:str.strip('d'),相应的也有lstrip,rstrip
  
str=' python String function '
  
print '%s strip=%s' % (str,str.strip())
  
str='python String function'
  
print '%s strip=%s' % (str,str.strip('d'))

[*]  按指定字符分割字符串为数组:str.split(' ')
  
默认按空格分隔
  
str='a b c de'
  
print '%s strip=%s' % (str,str.split())
  
str='a-b-c-de'
  
print '%s strip=%s' % (str,str.split('-'))

[*]  字符串判断相关
  
是否以start开头:str.startswith('start')
  
是否以end结尾:str.endswith('end')
  
是否全为字母或数字:str.isalnum()
  
是否全字母:str.isalpha()
  
是否全数字:str.isdigit()
  
是否全小写:str.islower()
  
是否全大写:str.isupper()
  
str='python String function'
  
print '%s startwith t=%s' % (str,str.startswith('t'))
  
print '%s endwith d=%s' % (str,str.endswith('d'))
  
print '%s isalnum=%s' % (str,str.isalnum())
  
str='pythonStringfunction'
  
print '%s isalnum=%s' % (str,str.isalnum())
  
print '%s isalpha=%s' % (str,str.isalpha())
  
print '%s isupper=%s' % (str,str.isupper())
  
print '%s islower=%s' % (str,str.islower())
  
print '%s isdigit=%s' % (str,str.isdigit())
  
str='3423'
  
print '%s isdigit=%s' % (str,str.isdigit())

  三 列表
  列表和字符串一样是序列的数据类型,可以通过下标和切片访问某一个或者某一连续的元素。与其不通,字符串只能由字符组成且不能改变,然而列表是一个容器,能保留任何python对象。既然是一个容器当然 他就可以排序,以及对单独元素 插入 或者删除。
  1.创建列表
  列表由[ ] 来定义 以,分割元素。
  a= 567
  alist = , a,('uu','jj'')]
  blist = [ ]
  列表的元素可以是任何元素 上面是例子:数字 变量 子元组 字符串 子列表 。
  2访问列表
  和字符串一样 索引的方式访问也可以 内置函数 pop() 最后一个元素
  >>>alist
  123
  >>>alist [ :3]
  
  3更新列表
  alist =1
  >>> alist = , a,('uu','jj'')]
  append() 方法也可以在 列表最后索引加如
  alist.append( 'ca')
  >>> alist = , a,('uu','jj'),'ca']
  4列表的操作符
  和字符串基本一样(前面都有例子这不说了)
  < >=
  [ ] [ : ]
  in not in
  连接 +    重复 *
  5列表解析
  alist=
  >>>
  
  >>>[ ifor i in range(8)if i%3 ==0]
  
  6。列表内建函数
  

cmp()  列表的比较 ,一次对对2个列表的 的元素。
  len()
  >>>alist=
  >>>len(alist)
  4
  sorted() 排序
  reversed() 倒排
  enumerate()
  

for i in range(len(alist)):  
print i, alist
  

  

  但利用enumerate,可以这样:
  

for i, x in enumerate(alist):  
print i, x
  

  

  zip()
  >>> a =
  >>> b = [4,5,6
  ] >>> c =
  >>> zipped = zip(a,b) [(1, 4), (2, 5), (3, 6)]
  a=
  >>>sum(a)
  6
  list() tup()
  互换函数
  7 列表的方法
  dir(list) or dir([ ])
  三。元组
  元组和列表 基本差不多 唯一区别 元组的元素不可更改,
  操作符 * +in not in> < = 等都可通用
页: [1]
查看完整版本: python学习总结五(python序列)