教为学:Python学习之路(三):序列
概论
Python的序列总共有六种内建序列:
列表、元组、字符串、Unicode字符串、buffer对象和xrange对象。
我们主要讲的是列表和元组。这两者最大的区别是列表是可以修改的,元组是不可以被修改的。
序列的特征就是会为每一个元素分配一个序号,从零开始是第一个,反过来从最后一个开始是-1…-n。
序列的通用操作
索引
代码:
-
#coding:utf-8
-
months1=["1","2","3","4","5","6","7","8","9","10","11","12"]
-
mouths2=("1","2","3","4","5","6","7","8","9","10","11","12")
-
print months1
-
print mouths2
-
-
print type(months1)
-
print type(mouths2)
-
-
print months1[1]
-
print months1[-1]
结果:
-
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']
-
('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12')
-
<type 'list'>
-
<type 'tuple'>
-
2
-
12
所谓索引,有过其他语言编程经验的都知道,有个叫数组下标的东西,不过,索引更强大,可以反向,而其他语言一般都不可以。
当然索引也是序列的主要特征。
如果要在形式上区分这两者,中括号包围的是列表,小括号包围的是元组。
当然在这里我们还要还要介绍一个非常强大的命令dir:
源码:
-
months1=["1","2","3","4","5","6","7","8","9","10","11","12"]
-
mouths2=("1","2","3","4","5","6","7","8","9","10","11","12")
-
print dir(months1)
-
print dir(mouths2)
结果:
-
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
-
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']
这里是列出该变量所能用的所有方法。
分片
分片和索引的区别在于索引是用来访问一个元素,而分片是用来访问一组元素。
-
months=["1","2","3","4","5","6","7","8","9","10","11","12"]
-
#从序号3到序号5
-
print months[3:6]
-
#从序号0到序号3
-
print months[:3]
-
print months[0:3]
-
#加入步长
-
print months[::1]
-
print months[::2]
-
print months[::3]
结果:
-
['4', '5', '6']
-
['1', '2', '3']
-
['1', '2', '3']
-
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']
-
['1', '3', '5', '7', '9', '11']
-
['1', '4', '7', '10']
分片这东西听起来很神奇,做起很简单。
两个冒号隔离三个部分,起始位置,结束位置+1,步划
算数运算
源码
-
a=[1,2,3]
-
b=[4,5,6]
-
print a+b
-
print a*5
结果:
-
[1, 2, 3, 4, 5, 6]
-
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
理解起来并不困难。
常用函数
-
a=[1,2,3]
-
print len(a)
-
print max(a)
-
print min(a)
结果:
-
3
-
3
-
1
列表
-
a=[1,2,3]
-
a.append(4)
-
print a
-
a=[1,1,1,2,2,3]
-
print a.count(1)
-
print a.count(2)
-
print a.count(3)
-
a=[1,2,3]
-
b=[4,5,6]
-
print a+b
-
a.extend(b)
-
print a
-
print a.index(4)
-
a.insert(3,'aaaa')
-
print a
-
print a.pop()
-
print a
-
print a.remove('aaaa')
-
print a
-
print a.reverse()
-
print a
-
a=[3,312,342,23,6,9]
-
print a
-
a.sort()
-
print a
结果:
-
[1, 2, 3, 4]
-
3
-
2
-
1
-
[1, 2, 3, 4, 5, 6]
-
[1, 2, 3, 4, 5, 6]
-
3
-
[1, 2, 3, 'aaaa', 4, 5, 6]
-
6
-
[1, 2, 3, 'aaaa', 4, 5]
-
None
-
[1, 2, 3, 4, 5]
-
None
-
[5, 4, 3, 2, 1]
-
[3, 312, 342, 23, 6, 9]
-
[3, 6, 9, 23, 312, 342]
对于这一大对的方法,除了我们上面所说的dir查看有什么方法之外,还有一个方法可以用,查看方法的用法。
代码
-
a=[1,2,3]
-
help(a.insert)
结果如下:
-
insert(...)
-
L.insert(index, object) -- insert object before index
元组
元组本质上是一个去除修改内容的列表,一切可以修改列表内容的函数都不可用。