一、序列概览
序列常用的两种类型:列表和元组
列表和元组的主要区别:列表可以修改,元组不可以
可以用序列表示数据库中一个人的信息:
>>> edword=['Edword Gumby',32] ##列表中的各个元素通过逗号隔开,写在方括号中
>>>
序列也可以包含其他的序列,因此可以构建一个数据库:
>>> edword=['Edword Gumby',32]
>>> John=['John Smith',21]
>>> database=[Edword,John] ##注意大小写
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Edword' is not defined
>>> database=[edword,John]
>>> database
[['Edword Gumby', 32], ['John Smith', 21]]
>>>
二、通用序列操作
1、索引
序列中的元素可以通过编号访问
>>> greeting='hello'
>>> greeting=[0]
>>> greeting[0]
0
字符串就是一个有字符组成的序列,索引0指向第一个元素
可以通过索引获取元素
当需要取后面一点的元素时,可以使用负数索引,python会从右边,也就是最后一个元素开始计数。最后一个元素的编号时-1
示例:
#/usr/bin/env python
#coding:utf-8
#根据给定的年月日以数字形式打印出日期
months=[
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
]
#以1~31的数字作为结尾的列表
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
结果:
/usr/bin/python2.7 /home/kiosk/Desktop/python/index_example.py
year: 1974
Month(1-12): 8
Day(1-31): 16
August 16th, 1974
Process finished with exit code 0
2、分片
分片通过冒号像个的两个索引来实现:
>>> numbers=[1,2,3,4,5,6,7,8,9,10]
>>> numbers[3:6]
[4, 5, 6]
>>> numbers[0:1]
[1]
>>>
(1)第一个索引的元素包含在分片内,第二个元素不包含在分片内
>>> numbers[7:10]
[8, 9, 10] ##正着可以取到最后一个元素
>>> numbers[-3:-1]
[8, 9] ##负数不能取到最后一个索引
>>> numbers[-3:0]
[] ##为空,因为0是第一个元素,-3比它后出现
>>>
那么怎么可以取的最后一个元素呢:
将第二个索引置为空(也就是第二个索引的元素比第一个索引的元素后出现)
>>> numbers[-3:]
[8, 9, 10]
>>>
用在开始也可以:
>>> numbers[:3]
[1, 2, 3]
>>>
也可以来嗯各都置空:
>>> numbers[:]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>>
这样可以取的整个序列
示例:
#!/usr/bin/env python
#coding:utf-8
#对http://www.something.com形式的URL进行分割
url=raw_input('Please enter the URL: ')
domain=url[11:-4]
print "Domain name: "+domain
结果:
/usr/bin/python2.7 "/home/kiosk/Desktop/python/fen pian.py"
Please enter the URL: http://www.something.com
Domain name: something
Process finished with exit code 0
(2)步长
像上面的分片,步长为1,此时的步长是隐式设置的,当然也可以显示设置:
>>> numbers[0:10:1]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> numbers[0:10:2]
[1, 3, 5, 7, 9]
>>> numbers[0:10:4]
[1, 5, 9]
>>> numbers[0::4]
[1, 5, 9]
>>> numbers[0::0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: slice step cannot be zero
>>> numbers[0::-2]
[1]
>>> numbers[::-2]
[10, 8, 6, 4, 2]
>>>
注意:步长可以自己设置,可以是负数,但不能是0
三、序列相加
>>> [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 "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "str") to list
>>>
注意:列表和字符串不能相加,只有两种相同类型的序列才可以相加
四、乘法
1、用数字x乘以一个序列会生成新的序列,而在新的序列中,原来的序列会被重复x次
>>> 'python'*5
'pythonpythonpythonpythonpython'
>>> [42]*5
[42, 42, 42, 42, 42]
>>>
2、初始化一个长度为10的列表:
>>> seq=[None]*10
>>> deq
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'deq' is not defined
>>> seq
[None, None, None, None, None, None, None, None, None, None]
>>>
None是一个python的内建值,表示什么也没有