.在python中,是最基本的数据结构是序列(sequence).
序列中的每个元素被分配一个序号----即元素的位置,也称为索引(第一个索引从开始,依次类推)
.序列
列表和元组区别在于,列表可以修改,元组不可修改。
test = ['test hello',]
print test[] #resuslt is 'test hello'
print test[] #result is 36
print test #result is ['test hello',36]
test = ['test hello',]
value = ['value hello',]
new = [test, value]
print new #restulst [['test hello',40],['value hello',30]]
备注:容器数据结构,容器基本包含其的对象的任意对象。序列和映射是两类容器。
序列中的每个元素都有自己的编号,而映射有一个名字(称为键)
.通用序列操作。
所有的序列都可以进行某种特定的操作,比如索引(index),分片(slicing),加(adding),乘(multplying)
以及检查某个元素的序列的成员。除此之外还可以计算序列的长度,找出最大元素和最小元素的内键函数
.索引(index)
test = 'hello'
print test[] #resutl is h
负数索引(从右边开始查询)
print test[-] #resuslt is o
直接对返回结果进行索引操作:
test = raw_input("Year: ")[] #input 2016
print test #返回结果是test序列的第四个元素 6
索引示列:
months = [
'January',
'Febuary',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'Decmber'
]
#以下1-31的数字作为结尾的列表
endings = ['st','nd','rd'] + * ['th']\
+['st','nd','rd'] + * ['th']\
+['st']
year = raw_input("Year: ")
month = raw_input("Month (1-12): ")
day = raw_input('Day(1-31): ')
#记得要将月份和天数-1,获得正确索引
month_name = months[month_number -]
ordinal = day + endings[day_number -]
print month_name +' '+ ordinal + ' '+ year
..分片
与使用索引来访问单个元素类似,可以使用分片来访问一定范围内的元素。
分片能过冒号隔开的两个索引来实现
test = 'http://www.perl.org'
print test[:] #reulst is perl
print test[:-] #result is perl ,可以使用负数来操作
test = [,,,,,,,,,]
print test[:] #resutl is 4,5,6
print test[-:-] #resutl is 8,9,10
按步长分片
test=[,,,,,,,,,]
print test[::] #按一个一个来分片范围0:10 result si 1,2,3,4,5,6,7,8,9,19
print test[::] #范围是0:10,每两步进行分片,result is 1,3,5,7,9
print test[::] # result is 1,5,9
从右边按步长分处
print test[::-] # 9,8,7,6,5
..序列相加
[,,] + [,,] #result is [1,2,4,5,6]
'hello' + 'world' #result is helloworld
..乘法(重复次数)
'perl' * #'perlperlperlperlperl'
[] * # result is [6,6,6,6,6]
空列表可以用[]表示
None是一个python内键值,表示这里什么都没有 #perl同类值是undef,代表这里空无一物,走开走开
..检测成员(布尔值)
test = 'hello'
'h' in test #result is True
'w' in test #result is False
test = ['laomeng','wang','lisi']
raw_input('Enter your name: ') in test
#laomeng result is True
..计算长度,最小值和最大值
len(),
min(),
max()
test=[,,,,,,]
len(test) #result is 7
minx(test) #result is 1
max(test) #result is 7
.列表
列表是可以变化的--可以改变列表的内容,并且列表有很多有用的,专门的方法
..list()函数
list()函数适用于所有类型的序列,而不只是字符串
创建列表
list('hello')
['h','e','l','l','o']
..删除元素
test =['abc','abd','abe','abf']
del test[] #删除第三个元素
..分片赋值
test = list('Perl') #'P','e','r','l'
test[:] = list('ar') #result is 'P','e','a','r'
test=[,]
test[:] =[,,] #result is ['1,2,3,4,5]
删除元素
test =[,,,,]
test[:]=[] #result is [1,5]
.列表方法
test =[,,]
..append方法用于在末尾增加新的元素
test = [,,,]
append.test[] #resut is [1,2,3,4,5]
在perl脚本有同样的功能
my @test = qw(1 2 3 4);
push @test,;
print "@test\n"; #result is 1 2 3 4 5
..count统计某个元素在列表出现的次数
test = ['a','b','a','b','a','d']
test.count('a') # result is 3
..extend方法可以在序列末尾追加多个值
test = [,,,,]
b=[,,]
test.extend(b) #rest 1,2,3,4,5,5,7,8
..index方法用于从列表中找出某个值的位置
test =['abc','abd','abe','abf','abg']
test.index('abe') #result is 2
..insert方法用于将对象插入列表中
test = [,,,,]
test.insert(,'hello') #result is 1,2,3,'hello',4,5
..pop方法移除列表中的一个元素,默认是最后一个元素,pop是有返回值
test = [,,,,]
test.pop() #result is 1,2,3,4
test.append(pop()) #result is 1,2,3,4,5
..remove方法用于移除列表中某个匹配的值
test =['a','b','c','d']
test.remove('a') #result is b,c,d
..reverse结果取反存放
x=[,,,]
x.reverse() #result is 4,3,2,1
x = 'hello'
x.list(reverse()) #result is o,l,l,h
..sort方法是进行排序
x=[,,,,,];
x.sort() #result is 1,2,4,6.7,9
y =x[:] #:切片包包含所有的值
y.sort() #result is 1,2,4,6.7,9
.元组: 不可改变序列
,, #restult is (1,2,3)
() #空元组
..tulpe函数功能与list函数功能基本上一样
tulpe([,,]) #result is (1,2,3)
.元组的基本操作
x = ,,
x[] #result is 2
x[:] #result is 1,2
总结:
序列:是一种数据结构,它包含了元素都行了编号(从开始).
序列包括了列表,字符串,元组。其中列表可以改变修改的
而元组是不可以修改的。通过分片捷捷操作符可以访问序列的一部分(范围),其中分片需要两个索引来
指出分片的起始和结束位置。
序列成员检测。in操作符可以检查一个值是否存在于序列中。