数据结构的概念:
数据结构是通过某种方式(例如通过对元素进行编号)组织在一起的数据元素的集合。
最基本的数据结构是序列(sequence)
Python 6种内建的序列:列表、元组、字符串、Unicode字符串、buffer对象、xrange对象
序列基本的操作:
1. 索引
name = ['alex','alan','lee']
name[0] ==> 'alex'
age = (1,2,3,4)
age[0] ==> 1
str = 'abcdefg'
str[3] ==> 'd'
unicodestr = u'hello world'
unicodestr[4] ==> 'o'
2.分片
name_s = ['alex','alan','lee']
name_list[1:2] ===>alan
age_s = (1,2,3,4)
age_s[0:2] ===> (1,2)
str_s = 'oiuytr'
str_s[1:3] ===> 'iu'
unicodestr_s = u'hello world'
unicodestr_s[1:4] ==> u'ell'
3.序列相加
name_add = ['alex','alan','lee']
f_add = ['cool', 'Coiu']
name_add + f_add ===> ['alex', 'alan' , 'lee', 'cool', 'Coiu']
age_add1 = (1,2)
age_add2 = (3,4)
age_add1 + age_add2 ===> (1,2,3,4)
'hello, ' + ‘world’ ===> 'hello, world'
u"hello, " + u"cool" ===> u'hello, cool'
4.乘法
name_mul = ['alex', 'alan', 'lee']
name_mul * 2 ===> ['alex', 'alan', 'lee', 'alex', 'alan', 'lee']
age_mul = (1,2)
age_mul*2 ===> (1,2,1,2)
str_mul = 'nihao'
str_mul*2 ===> 'nihaonihao'
unicode_str_mul = u'hello '
unicode_str_mul *2 ===> u'hello hello'
5.成员资格
name_in = ['alex', 'alan', 'lee']
'alex' in name_in ===>True
age_in = (1, 2, 3)
4 in age_in ===> False
str_in = 'fuckyou'
'fc' in str_in ===> False
unicode_str_in = u'sb'
's' in unicode_str_in ===> True
6.长度、最小值、最大值
age_len = (1,2,3)
len(ahe_len) ===> 3
age_max = (1,2,3)
max(age_max) ===>3
age_min = (1,2,3)
min(age_min) ===> 1