数据结构是通过某种方式组织在一起的数据元素的集合,这些数据元素可以是数字或字符,也可以是其它的数据结构。

序列:序列中每个元素被分配一个序号,即是元素的位置,称为索引,第一个索引是0,第二个是1,以此类推。

常见的序列有:列表,元组,字符串

序列通用的操作:

1.索引
1
2
3
4
5
6
    
>>> name
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> name[0]
0
>>> name[1]
1

2.分片
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    
>>> name
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> name[2:5]
[2, 3, 4]
>>> name[2:]
[2, 3, 4, 5, 6, 7, 8, 9]
>>> name[:5]
[0, 1, 2, 3, 4]
>>> name[:]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> name[-1]
9
>>> name[-2]
8
>>> name[2:7:2]
[2, 4, 6]


3,。序列的加和乘法运算
1
2
3
4
5
6
7
8
    
>>> zhu = [1,2,3]
>>> jiang = [4,5,6]
>>> zhu + jiang
[1, 2, 3, 4, 5, 6]
>>> zhu*3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> 'zhu' * 3
'zhuzhuzhu'

4.成员判断-in
1
2
3
4
5
6
7
8
9
10
11
12
13
    
>>> name = ['zhu','jiang','tao']
>>> name
['zhu', 'jiang', 'tao']
>>> if 'zhu' in name:
...     print "OK"
...
OK
>>> if 'zhuzhu' in name:
...     print "OK"
... else:
...     print "NO"
...
NO

5。长度,最小值,最大值
1
2
3
4
5
6
7
8
    
>>> name
[0, 1, 2, 3, 4]
>>> len(name)
5
>>> min(name)
0
>>> max(name)
4


10-05 06:05