首页 > 编程 > Python > 正文

Python序列

2019-11-08 02:26:13
字体:
来源:转载
供稿:网友

一、列表

Python序列类型有7种:字符串、Unicode字符串、列表、元组、字节数组、buffer对象、xrange对象

1. 列表初始化

空列表In [51]: bOut[51]: []初始化时指定列表空间长度(0 或 None)In [42]: b = [None] * 10In [43]: bOut[43]: [None, None, None, None, None, None, None, None, None, None]In [54]: b[0]In [55]: In [48]: bOut[48]: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]In [49]: b[0]Out[49]: 0

None 是Python的内建值,表示什么都没有

2. 列表成员资格

检查一个值是否在序列中,使用 in 运算符

In [56]: bOut[56]: [None, None, None, None, None, None, None, None, None, None]In [57]: None in bOut[57]: TrueIn [58]: 'None' in bOut[58]: False

3. list()函数

将Python序列(字符串、列表、元组、Unicode字符串、buffer对象、xrange对象)转换为列表类型.

In [1]: list('hello')Out[1]: ['h', 'e', 'l', 'l', 'o']In [6]: list((1,2,3))Out[6]: [1, 2, 3]

4. 常用列表方法

count   统计某个元素在列表中出现的次数In [7]: a = [1,2,3,1,2,3,4,5,6]In [8]: a.count(1)Out[8]: 2extend   在列表的末尾一次性追加另一个序列In [7]: a = [1,2,3,1,2,3,4,5,6]In [9]: a.extend('test')In [10]: aOut[10]: [1, 2, 3, 1, 2, 3, 4, 5, 6, 't', 'e', 's', 't']In [11]: b = [10,11,12]In [12]: a.extend(b)In [13]: aOut[13]: [1, 2, 3, 1, 2, 3, 4, 5, 6, 't', 'e', 's', 't', 10, 11, 12]index   从列表中找出某个值的位置,这个位置是第一次匹配某个值时的位置In [16]: b = [1,2,1]In [17]: b.index(1)Out[17]: 0

二、字符串

1. 常用字符串方法

find   在字符串中查找子串。返回值是子串所在位置的最左端索引;如果没有找到则返回 -1In [25]: a = 'this is a test of chars'In [26]: a.find('this')Out[26]: 0In [27]: a.find('that')Out[27]: -1join   连接序列中的元素。’[mark]’.join(seq)In [35]: dirs = '','usr','local','bin'In [36]: dirsOut[36]: ('', 'usr', 'local', 'bin')In [37]: '/'.join(dirs)Out[37]: '/usr/local/bin'In [48]: dirs = 'usr', 'local', 'bin'In [50]: dirsOut[50]: ('usr', 'local', 'bin')In [51]: '/'.join(dirs)Out[51]: 'usr/local/bin'split   将字符串分割为列表。join方法的逆方法In [62]: a = '/usr/local/bin/'In [63]: aOut[63]: '/usr/local/bin/'In [64]: a.split('/')Out[64]: ['', 'usr', 'local', 'bin', '']strip   去除字符串左右两边的空格字符串(包括换行符号等)In [72]: PRint ' hello/n' helloIn [73]: print 'hello/n'.strip()helloreplace   字符串替换。In [74]: aOut[74]: 'this is a test'In [75]: a.replace(' ', '/')Out[75]: 'this/is/a/test'

三、元组

元素通过逗号操作符来构造,带或者不带圆括号都行。但是空元组必须带圆括号。单个元素的元组尾部必须还有一个逗号。

In [9]: tup = 1,2,3In [10]: tupOut[10]: (1, 2, 3)In [11]: tup1 = 1,In [12]: tup1Out[12]: (1,)

四、序列解包

序列解包:又叫递归解包。将多个值的序列解开,然后放到变量的序列中。注意:序列解包中左边的变量数必须与右边的元素数量完全一致,否则会在赋值时引发一场

In [97]: x,y,z = 1,2,3In [98]: xOut[98]: 1In [99]: x,yOut[99]: (1, 2)In [100]: x,y = y,xIn [101]: x,yOut[101]: (2, 1)In [91]: values = 1,2,3In [92]: valuesOut[92]: (1, 2, 3)In [93]: a,b,c = valuesIn [94]: aOut[94]: 1

当函数或者方法返回元组、列表等序列时,同样可以利用这一个特性,如下:

In [102]: def seq_pack(): .....: return (1,2,3) .....: In [103]: A,B,C = seq_pack()In [117]: AOut[117]: 1

enumerate 有时需要得到序列的元素和位置,一般我们用到的是len() + range() + for循环处理。这样写就显得十分拖沓。此时就可以利用Python的内置函数enumrate()

In [198]: aOut[198]: [1, 2, 3, 4]In [199]: for position,value in enumerate(a): print position,value .....: 0 11 22 33 4

字典也同样可以使用enumrate

In [200]: a = {'name':'yantao', 'age':22}In [201]: for k,v in enumerate(a): .....: print k,v .....: 0 age1 name
上一篇:Python字典

下一篇:Python:win8安装scrapy

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表