2.1 序列概览 Python 包含6种内建的序列,本章重点讨论最常用的两种类型:列表和元组。 列表和元组的主要区别在于,列表可以修改,元组则不能。 edward=['a1','b2','c3'] print edward[0]; print edward[1]; print edward[2]; C:Python27python.exe C:/Users/Administrator/PycharmProjects/untitled1/a2.py a1 b2 c3 修改序列的元素: edward=['a1','b2','c3'] print edward[0]; print edward[1]; print edward[2]; edward[0]='scan' print edward C:Python27python.exe C:/Users/Administrator/PycharmProjects/untitled1/a2.py a1 b2 c3 ['scan', 'b2', 'c3'] 2.2 通用序列操作: 注意 字符串就是一个由字符组成的序列,索引0指向第一个元素 /*** 普通字符串也可以跨行,如果一行之中最后一个字符是反斜线,那么,换行符本身就是转义了,也就是被忽略了。 print "Hello" "world" 2.2.2 分片: numbers=[1,2,3,4,5,6,7,8,9,10] var=numbers[3:6] print var numbers=[1,2,3,4,5,6,7,8,9,10] var=numbers[3:6] print var tag='<a href="http://www.python.org">Python web site</a>' print tag[9:30] C:Python27python.exe C:/Users/Administrator/PycharmProjects/untitled1/a4.py http://www.python.org 下标从9到30 from math import sqrt months = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ] endings = ['st', 'nd', 'rd'] + 17 * ['th'] + ['st', 'nd', 'rd'] + 7 * ['th'] + ['st]'] year = raw_input('Year: ') month = raw_input('Month(1-12): ') day = raw_input('Day(1-31): ') month_number = int(month) day_number = int(day) ##记得要将月份和天数减去1,获得正确的索引 month_name = months[month_number - 1] ordinal = day + endings[day_number - 1] print month_name + '' + ordinal + ',' + year 切片: tag='<a href="http://www.python.org">Python web site</a>' print tag[9:30] C:Python27python.exe C:/Users/Administrator/PycharmProjects/untitled1/a4.py http://www.python.org 从下边9到下标30,不包含30 tag='<a href="http://www.python.org">Python web site</a>' print tag[32:-4] C:Python27python.exe C:/Users/Administrator/PycharmProjects/untitled1/a4.py Python web site 32开始不包含-4 简而言之,分片操作的实现需要提供两个索引作为边界,第一个索引的元素是包含在分片内的, 而第2个则不包含在分片内。 1. 优雅的捷径: 假设需要访问最后3个元素(根据先前的例子),那么当然可以显示的操作: numbers=[1,2,3,4,5,6,7,8,9,10] print numbers[7:10] C:Python27python.exe C:/Users/Administrator/PycharmProjects/untitled1/a3.py [8, 9, 10] 从索引8到索引10,索引10指向的是第11个元素,这个元素并不存在 numbers=[1,2,3,4,5,6,7,8,9,10] print numbers[-3:-1] C:Python27python.exe C:/Users/Administrator/PycharmProjects/untitled1/a3.py [8, 9] -3 代表8 -2 代表9 -1 代表10 numbers=[1,2,3,4,5,6,7,8,9,10] print numbers[-3] print numbers[-2] print numbers[-1] C:Python27python.exe C:/Users/Administrator/PycharmProjects/untitled1/a3.py 8 9 10 2.2.3 序列想加: numbers=[1,2,3,4,5,6,7,8,9,10] print [1,2,3] + [4,5,6] C:Python27python.exe C:/Users/Administrator/PycharmProjects/untitled1/a3.py [1, 2, 3, 4, 5, 6] 简单的说,两种相同类型的序列才能进行连接操作; 2.2.4 乘法: numbers=[1,2,3,4,5,6,7,8,9,10] print 'python'*5; C:Python27python.exe C:/Users/Administrator/PycharmProjects/untitled1/a3.py pythonpythonpythonpythonpython 空列表可以简单地通过两个中括号进行表示[]----里面什么东西都没有.但是,如果想创建一个占用10个元素空间, 确不包含任何有用内容的俩别哦,又该怎么办呢? 2.2.5 成员资格: permisions='rw' if 'w' in permisions:print 'aaa'; permisions = 'rw' if 'x' in permisions: print 'aaa' else:print 'bbbb' users=['a','b','c'] if 'a' in users: print 'aaa' else:print 'bbbb' C:Python27python.exe C:/Users/Administrator/PycharmProjects/untitled1/a3.py aaa 2.2.6 长度,最小值和最大值 numbers=[999,23,77] print len(numbers) print max(numbers) print min(numbers) C:Python27python.exe C:/Users/Administrator/PycharmProjects/untitled1/a5.py 3 999 23 2.3 列表:Python 的"苦力" 列表时可变的(mutable)---可以改变列表的内容,并且代表有很多有用的,专门的方法 2.3.1 list 函数: va=list('hello') print va C:Python27python.exe C:/Users/Administrator/PycharmProjects/untitled1/a5.py ['h', 'e', 'l', 'l', 'o'] va=list('12345689') print va C:Python27python.exe C:/Users/Administrator/PycharmProjects/untitled1/a5.py ['1', '2', '3', '4', '5', '6', '8', '9'] 2.3.2 基本的列表操作: 1.改变列表: 元素赋值 改变列表是很容易的,只需要使用在第一章提到的普通赋值语句即可。 x=[1,2,3,4] print x x[1]=299 print x C:Python27python.exe C:/Users/Administrator/PycharmProjects/untitled1/a5.py [1, 2, 3, 4] [1, 299, 3, 4] x=[1,2,3,4] print x x[1]=299 x[5]=300 print x Traceback (most recent call last): File "C:/Users/Administrator/PycharmProjects/untitled1/a5.py", line 4, in <module> x[5]=300 IndexError: list assignment index out of range [1, 2, 3, 4] 2.删除元素 x=[1,2,3,4] print x del x[1] print x 删除元素 3.分片赋值: 分片是一个非常强大的特性,分片赋值操作则更加现实它的强大: name=list('perl') print name C:Python27python.exe C:/Users/Administrator/PycharmProjects/untitled1/a5.py ['p', 'e', 'r', 'l'] name=list('perl') name[2:]=list('ar') print name 替换元素: 2.3.3 列表方法: perl中的方法调用: my $client=Client->new(); $client->get_qrcode(); 1.append方法用于在列表末尾追加新的对象: lst=[1,2,3] lst.append(4) print lst C:Python27python.exe C:/Users/Administrator/PycharmProjects/untitled1/a5.py [1, 2, 3, 4] 2.count count 方法统计某个元素在列表中出现的次数: 3.extentd 方法可以在列表的末尾一次性追加另外一个序列中的多个值,换句话说,可以用新列表: a=[1,2,3] b=[4,5,6] print a.extend(b) print a C:Python27python.exe C:/Users/Administrator/PycharmProjects/untitled1/a5.py None [1, 2, 3, 4, 5, 6] 4.index index 方法用于从列表中找出某个值第一个匹配项的索引位置: knights=['we','are','the','knights','who'] print knights.index('who') 数组越界: knights=['we','are','the','knights','who'] print knights[99] Traceback (most recent call last): File "C:/Users/Administrator/PycharmProjects/untitled1/a4.py", line 2, in <module> print knights[99] IndexError: list index out of range 5.insert insert 方法用于将对象插入到列表中: numbers=[1,2,3,4,5,6,7] numbers.insert(3,'four') print numbers C:Python27python.exe C:/Users/Administrator/PycharmProjects/untitled1/a4.py [1, 2, 3, 'four', 4, 5, 6, 7] 6. pop pop 方法会移除列表中的一个元素(默认是最后一个),并且返回该元素的值 x=[1,2,3,4,5,6,7] print x.pop(); print x C:Python27python.exe C:/Users/Administrator/PycharmProjects/untitled1/a4.py 7 [1, 2, 3, 4, 5, 6] Process finished with exit code 0 7.添加元素: x=[1,2,3,4,5,6,7] print x.append(8) print x 2.4 元组:不可变序列 元组与列表一样,也是一种序列。唯一的不同是元组不能修改, 创建元组的方法很简单,如果你用逗号分隔了一些值,那么你就自动创建了元组 元组也是通过圆括号括起来的: >>> x=(42) >>> print x 42 >>> x=(42,) >>> print x (42,) x=(1,2,3,4) print x[1] x[1]=299 print x 元组不能修改: C:Python27python.exe C:/Users/Administrator/PycharmProjects/untitled1/a4.py Traceback (most recent call last): File "C:/Users/Administrator/PycharmProjects/untitled1/a4.py", line 3, in <module> x[1]=299 TypeError: 'tuple' object does not support item assignment 2 2.4.1 tuple 函数; 2.4.2 基本元组操作 2.4.1 tuple 函数