序列概述
列表和元组的主要区别在于,列表可以修改,元组则不能。在需要操作一组数值的时候,需序列很好用。可以用序列表示数据库中一个的信息--第1个无素是姓名,第2个元素是年龄。根据上述内容编写一个列表,如下例所示:
>>> edward =['Edward Utimes', 50]同时,序列页可以有其他的序列,因此,构建如下一个人员信息的列表也是可以的,这个列表就是你的数据库:
>>> edward = ['EdWAARD UTIMES', 50]
>>> john = ['John Smith', 30]
>>> database =[edward,john]
>>> database
[['EdWAARD UTIMES', 50], ['John Smith', 30]]
通用序列操作
所有序列类型都可以进行某些特定的操作。如索引(indexing)、分片(sliceing)、加(adding)、乘(multiplying)以及检查某个元素是否属于序列的成员。
索引
序列中的所有元素都是有编号的从0开始递增。如下例所示:
>>> greeting = 'hello'
>>> greeting[0]
'h'代码清单 索引示例
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)
month_name = months[month_number-1]
ordinal = day +endings[day_number-1]
print month_name + ' '+ ordinal + ' .'+year以下是程序的结果:
>>> ================================ RESTART ================================
>>>
Year: 1974
Month(1-12): 8
Day(1-31): 16
August 16th .1974
>>>
分片
与使用索引来访问单个元素类似,可以使用分片操作来访问一定范围内的元素。分片通过冒号相隔的两个索引来实现:
>>> tag='<a href ="http://blog.csdn.net/utimes">SongZiTea web site</a>'
>>> tag[9:38]
'"http://blog.csdn.net/utimes"'
>>> tag[30:-4]
'/utimes">SongZiTea web site'
>>> tag[39:-4]
'SongZiTea web site'分片操作对于提取序列的一部分是很有用的。而编号在这里显得尤为重要。第1个索引是需要提取部分的第1个元素的编号,而最后的索引则分片之后剩下部分的第1个元素的编号。
>>> numbers =[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
>>> numbers[3:7]
[4, 5, 6, 7]
>>> numbers[-1:-9]
[]
>>> numbers[0:1]
[1]
>>> numbers[:-1]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> numbers[-2]
14
>>> numbers[:-2]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
>>> numbers[-10:]
[6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
>>> numbers[-2:]
[14, 15]
乘
sentence = raw_input("Sentence: ")
screen_width = 80
text_width = len(sentence)
box_width = text_width+6
left_margin =(screen_width-box_width)//2
print
print' '*left_margin+'+'+'-'*(box_width-2) +'+'
print' '*left_margin+'|'+' '*text_width +'|'
print' '*left_margin+'|'+ sentence +'|'
print' '*left_margin+'|'+' '*text_width +'|'
print' '*left_margin+'+'+'-'*(box_width-2) +'+'
print结果
>>> ================================ RESTART ================================
>>>
Sentence: He is a very naughty boy!
+-----------------------------+
| |
|He is a very naughty boy!|
| |
+-----------------------------+
列表
list函数
>>> list('Hello')
['H', 'e', 'l', 'l', 'o']元素赋值
>>> x=[1,2,3,4]
>>> x[1] =5
>>> x
[1, 5, 3, 4]删除元素
>>> x =[1,2,3,4,5,6,7]
>>> del x[3]
>>> x
[1, 2, 3, 5, 6, 7]分片赋值
>>> name = list('songzitea')
>>> name
['s', 'o', 'n', 'g', 'z', 'i', 't', 'e', 'a']
>>> name[3:]=list('utimes')
>>> name
['s', 'o', 'n', 'u', 't', 'i', 'm', 'e', 's']插入新元素
>>> name[3:3] =list('gzitea')
>>> name
['s', 'o', 'n', 'g', 'z', 'i', 't', 'e', 'a', 'u', 't', 'i', 'm', 'e', 's']
元组
元组与列表一样,也是一种序列。唯一的不同是元组不能修改。
>>> 1,2,3,4
(1, 2, 3, 4)
>>> (1,2,3,4)
(1, 2, 3, 4)
>>> ()
()
>>> 52
52
>>> 52.
52.0
>>> type(52.)
<type 'float'>
>>> (42.)
42.0 tuple函数
>>> tuple([1,2,3,4,5,6])
(1, 2, 3, 4, 5, 6)
>>> tuple('abc')
('a', 'b', 'c')
>>> tuple((1,2,3,4,5,6))
(1, 2, 3, 4, 5, 6)
>>> tuple(1,2,3,4,5,6)
Traceback (most recent call last):
File "<pyshell#51>", line 1, in <module>
tuple(1,2,3,4,5,6)
TypeError: tuple() takes at most 1 argument (6 given)基本元组操作
>>> x = 1,2,3
>>> x[1]
2
>>> x[0:2]
(1, 2)
>>> x
(1, 2, 3)
===========================================================
转载请注明出处:http://blog.csdn.net/utimes/article/details/9258389===========================================================
5万+

被折叠的 条评论
为什么被折叠?



