1、 6种内建序列
列表 元组 字符串 Unicode字符串 buffer对象 xrange对象
2、 通用序列操作
索引:序列的所有元素都是有编号的,从0开始递增
>>>greeting = "hello"
>>>greeting[0]
‘h‘
>>>greeting[-1]
‘o‘
>>>greeting[-2]
‘l‘
分片:用来访问一定范围内的元素
>>> tag=‘abcdefghijklmnopqrstuvwyz‘
>>> tag[9:20]
‘jklmnopqrst‘
>>> tag[20:-2]
‘uvw‘
>>> tag[1:5:2] // 1是开始位置 5是结束位置 2是步长
‘bd‘
序列相加:使用+可以进行序列的连接操作
>>> tag = [1, 2,3]
>>> tag2= [4,5,6]
>>> tag + tag2
[1, 2, 3, 4, 5, 6]
>>> tag3 = [1,2]
>>> tag+tag3
[1, 2, 3, 1, 2]
相乘:以数字x乘以一个序列会生成新的序列,而在新的序列中,原来的序列将被重复x次
>>> ‘python‘*5
‘pythonpythonpythonpythonpython‘
>>> [42]*5
[42, 42, 42, 42, 42]
成员资格:检查一个值是否在序列中,使用in运算符
>>> test
‘hello,world!‘
>>> ‘h‘ in test
True
>>> ‘y‘ in test
False
长度len、最大值max、最小值min:
>>> nums = [2,4,1,5,7]
>>> len(nums)
5
>>> max(nums)
7
>>> min(nums)
1
>>> str=‘abcd1‘
>>> len(str)
5
>>> max(str)
‘d‘
>>> min(str)
‘1‘
3、 列表
列表是可变的mutable, 可以改变列表的内容
list函数:
>>> list(‘hello‘)
[‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘]
>>> list(1234)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ‘int‘ object is not iterable
列表元素赋值:
>>> x=list(‘11111‘)
>>> x
[‘1‘, ‘1‘, ‘1‘, ‘1‘, ‘1‘]
>>> x[0]=2
>>> x
[2, ‘1‘, ‘1‘, ‘1‘, ‘1‘]
>>> x[1]=‘str‘
>>> x
[2, ‘str‘, ‘1‘, ‘1‘, ‘1‘]
删除元素: del 删除单个元素或者部分元素
>>> x
[2, ‘str‘, ‘1‘, ‘1‘, ‘1‘]
>>> del x[3]
>>> x
[2, ‘str‘, ‘1‘, ‘1‘]
>>> name
[‘p‘, ‘e‘, ‘y‘, ‘i‘, ‘l‘, ‘i‘, ‘a‘]
>>> del name[2:]
>>> name
[‘p‘, ‘e‘]
分片赋值:可增加长度、替换中间元素、删除元素、
>>> name = list(‘perl‘)
>>> name
[‘p‘, ‘e‘, ‘r‘, ‘l‘]
>>> name[2:]=‘yilia‘
>>> name
[‘p‘, ‘e‘, ‘y‘, ‘i‘, ‘l‘, ‘i‘, ‘a‘]
列表方法: 对象.方法(参数)
(1)append:
>>> name
[‘p‘, ‘e‘]
>>> name.append(‘yilia‘)
>>> name
[‘p‘, ‘e‘, ‘yilia‘]
>>> name.append(2)
>>> name
[‘p‘, ‘e‘, ‘yilia‘, 2]
(2)count: 统计某个元素在列表中出现的次数
>>> x = [[1,2],1,3,4,5,[3,4]]
>>> count[1]
>>> x.count(1)
1
>>> x.count(2)
0
>>> x.count(3)
1
(3)extend: 操作对象不可以直接是字符串
>>> a = ‘python‘
>>> b = ‘language‘
>>> a.extend(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: ‘str‘ object has no attribute ‘extend‘
>>> a.extend(b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: ‘str‘ object has no attribute ‘extend‘
>>> list(a)
[‘p‘, ‘y‘, ‘t‘, ‘h‘, ‘o‘, ‘n‘]
>>> a = list(a)
>>> b = list(b)
>>> a.extend(b)
>>> a
[‘p‘, ‘y‘, ‘t‘, ‘h‘, ‘o‘, ‘n‘, ‘l‘, ‘a‘, ‘n‘, ‘g‘, ‘u‘, ‘a‘, ‘g‘, ‘e‘]
(4)index: 求某个字符的索引
>>> a = [12, 3, 2, 34, 21, 4]
>>> a.index(12)
0
>>> a.index(5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 5 is not in list
(5)insert:将对象插入到列表
>>> a
[12, 3, 2, 34, 21, 4]
>>> a.insert(3,"yes")
>>> a
[12, 3, 2, ‘yes‘, 34, 21, 4]
(6)pop: 移除列表中的元素(默认是最后一个元素),并且返回该元素 栈:后进先出
>>> a
[12, 3, 2, ‘yes‘, 34, 21, 4]
>>> a.pop()
4
>>> a.pop(3)
‘yes‘
>>> a
[12, 3, 2, 34, 21]
(7)remove: 移除列表中某个值的第一个匹配项 (无返回值)
>>> a
[12, 3, ‘who‘, 2, ‘is‘, 34, 21]
>>> a.remove(2)
>>> a
[12, 3, ‘who‘, ‘is‘, 34, 21]
>>> a.remove(‘id‘)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> a.remove(‘is‘)
>>> a
[12, 3, ‘who‘, 34, 21]
(8)reverse: 将列表中的元素反向存放 (无返回值)
>>> a
[12, 3, ‘who‘, 34, 21]
>>> a.reverse()
>>> a
[21, 34, ‘who‘, 3, 12]
P58 (9)sort: 对原序列进行排序,不生成新的序列
>>> a = [1, 3,5,2,6,4,4]
>>> b = a.sort()
>>> a
[1, 2, 3, 4, 4, 5, 6]
>>> b
#以上方法对b赋值无效,需要采用以下方法
>>> b = a
>>> b.sort
<built-in method sort of list object at 0x7f97d091fd88>
>>> b.sort()
>>> b
[1, 2, 3, 4, 4, 5, 6]
>>> c = [2, 9, 4, 3, 6, 4]
>>> d = sorted(c)
>>> c
[2, 9, 4, 3, 6, 4]
>>> d
[2, 3, 4, 4, 6, 9]
(10)高级排序:
cmp(x,y): x>y 正数 ; x<y 负数 ; x=y 0
key=len
>>> a = [1, ‘aa‘, ‘b‘, ‘aaaaaa‘, ‘aaa‘]
>>> a.sort(cmp)
>>> a
[1, ‘aa‘, ‘aaa‘, ‘aaaaaa‘, ‘b‘]
4、元组
不可变序列, 通常用括号来表示
>>> 1,2,3
(1, 2, 3)
#空元组
>>> ()
()
#一个元素的元组需要加,
>>> 42,
(42,)
(1)tuple函数:以一个序列作为参数并把它转化为元组
>>> tuple([1,2,3])
(1, 2, 3)
>>> tuple(‘abc‘)
(‘a‘, ‘b‘, ‘c‘)
(2)元组并不复杂,除了创建和访问元组元素外,无其它太多操作