KoreaSeal

予人玫瑰,手留余香!

  :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

所有序列都包括以下基本操作:

索引(indexing)、分片(sliceing)、加(adding)、乘(multiplying)、检查某个元素是否属于序列、计算序列长度、找出最大元素、找出最小元素。

1、索引

>>> greeting="hello"
>>> greeting[0]
'h'
>>> greeting[-1]
'o'

2、分片

>>> numbers = [1,2,3,4,5,6,7,8,9,10]
>>> numbers[3:6]
[4, 5, 6]
>>> numbers[0:-1]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

 3、加

>>> [1,2,3]+[4,5,6]
[1, 2, 3, 4, 5, 6]
>>> 'hello'+'wolrd'
'hellowolrd'

 4、乘

>>> 'abc'*5
'abcabcabcabcabc'
>>> [42]*3
[42, 42, 42]

 5、in

>>> permit = 'rw'
>>> 'r' in permit
True
>>> 'x' in permit
False

 6、len、max、min

>>> numbers = [100,55,789]
>>> len(numbers)
3
>>> max(numbers)
789
>>> min(numbers)
55

列表的特殊操作:

1、元素赋值

>>> x = [1,1,1]
>>> x[1] = 2
>>> x
[1, 2, 1]

 2、删除元素

>>> string = ['a','b','c','d','e']
>>> del string[2]
>>> string
['a', 'b', 'd', 'e']

 3、分片赋值

>>> name = list('perl')
>>> name
['p', 'e', 'r', 'l']
>>> name[2:] = list('ar')
>>> name
['p', 'e', 'a', 'r']
>>> name[1:1] = [2,3,4]
>>> name
['p', 2, 3, 4, 'e', 'a', 'r']
>>> name[1:3] = []
>>> name
['p', 4, 'e', 'a', 'r']

 

posted on 2013-01-03 23:11  KoreaSeal  阅读(444)  评论(0编辑  收藏  举报