1、序列概览
序列:列表,元组 → 每个元素都有编号
序列可包含其他序列,例如
>>> edward = ['edward gumby',42] >>> john = ['john smith',50] >>> database = [edward,john] >>> database [['edward gumby', 42], ['john smith', 50]]
2、通用序列操作:索引、分片、加、乘、成员资格
(1)索引:元素编号,通过索引访问单个元素。
*索引0指向第一个元素
*负数索引:最后一个元素的位置编号是-1非0
>>> greeting = 'hello' >>> greeting[0] 'h' >>> 'hello'[0] 'h'
函数调用返回一个序列:可直接对返回结果进行索引操作
例如,只对输入年份的第四个数字感兴趣
>>> fourth = input('year:')[3] year:1994 >>> fourth '4' >>>
索引示例:输入年月日,打印相对应日期的月份名称
#根据给定的年月日以数字形式打印日期 months = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ] #以1—31的数字作为结尾的列表 endings = ['st','nd','rd',] + 17 * ['th']\ + ['st','nd','rd',] + 17 * ['th']\ + ['st'] year = input ('Year:') month = input ('Month(1-12):') day = 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)
打印结果
Year:1994 Month(1-12):7 Day(1-31):4 July 4th,1994
分片:访问一定范围内的元素,通过冒号相隔的两个索引实现
*第一个索引是提取部分的第一个元素编号,第二个索引是分片之后剩下部分的第一个元素的编号(第1个索引的元素在分片内,第2个不在)
>>> numbers = [1,2,3,4,5,6,7,8,9,10]
>>> numbers[3:6]
[4, 5, 6]
>>> numbers[0:1]
[1]
>>>
1)优雅的捷径:
如果分片部分包括序列结尾的元素,那么只需置空最后一个索引
>>> numbers = [1,2,3,4,5,6,7,8,9,10] >>> numbers[3:] [4, 5, 6, 7, 8, 9, 10] >>>
序列开始的元素同样适用
>>> numbers = [1,2,3,4,5,6,7,8,9,10] >>> numbers[:3] [1, 2, 3] >>>
若要复制整个序列,则两个索引都置空
>>> numbers = [1,2,3,4,5,6,7,8,9,10] >>> numbers[:] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>>
2)更大的步长:捷径依然可用,步长不能为0,但是可为负数
例如,提取每4个元素 的第1个
>>> numbers = [1,2,3,4,5,6,7,8,9,10] >>> numbers[::4] [1, 5, 9] >>>
步长为负数表示从右到左提取元素
例如,
>>> numbers = [1,2,3,4,5,6,7,8,9,10] >>> numbers[8:3:-1] [9, 8, 7, 6, 5] >>>
(2)序列相加:使用加号进行序列的连接操作
*列表和字符串是无法连接在一起的,两种相同类型的序列才能进行连接操作
例如,
>>> [1,2,3]+[4,5,6] [1, 2, 3, 4, 5, 6] >>> 'hello,' + 'world!' 'hello,world!' >>> [1,2,3] + 'world!' Traceback (most recent call last): File "<pyshell#48>", line 1, in <module> [1,2,3] + 'world!' TypeError: can only concatenate list (not "str") to list >>>
(3)乘法:乘法是重复序列
例如,
>>> 'python' * 5 'pythonpythonpythonpythonpython' >>> [7]*10 [7, 7, 7, 7, 7, 7, 7, 7, 7, 7] >>>
(4)成员资格:in运算符(布尔运算符),条件为真返回True,条件为假返回False
例如,
>>> permissions = 'rw' >>> 'w' in permissions True >>> 'x' in permissions False
>>> users = ['mlh','foo','bar']
>>> input ('Enter your user name:') in users
Enter your user name:mlh
True
>>> subject = '$$$ Get rich now!$$$'
>>> '$$$' in subject
True
>>>
序列成员资格示例
#检查用户名和PIN码 database = [ ['albert','1234'], ['dilbert','4242'], ['smith','7524'], ['jones','9843'] ] username = input ('Username:') pin = input ('PIN code:') if [username,pin] in database: print ('Access granted')
打印结果
=============== RESTART: D:/Python37/python_test/membership.py =============== Username:albert PIN code:1234 Access granted >>> =============== RESTART: D:/Python37/python_test/membership.py =============== Username:chenhuimin PIN code:0704 >>>
(5)长度、最小值和最大值:len,min,max
>>> numbers = [100,34,678] >>> len(numbers) 3 >>> max(numbers) 678 >>> min(numbers) 34 >>> max(2,3) 3 >>> min(9,3,2,5) 2 >>>