- 十、序列
- 1. 列表、元组、字符串统称为序列
- 2. 一些函数
- 2.1 加法+拼接 和 乘法*拷贝
- 2.2 同一性运算符(检测id是否相同)
- 2.3 判断包含
- 2.4 del语句
- 2.4.1 删除指定元素
- 2.4.2 删除可变序列中的指定元素
- 3. 一些函数(续)
- 3.1 列表、元组、字符串相互转换
- 3.2 min()、max()
- 3.3 len()、sum()
- 3.4 sorted()、reversed()
- 3.4.1 sorted()
- 3.4.1 reversed():返回的是参数的反向迭代器
- 4. 一些函数(再续)
- 4.1 all()、any()
- 4.2 enumerate()
- 4.3 zip()
- 4.4 map()
- 4.5 filter()
- 5. 迭代器与可迭代对象
- 6. 序列都是可迭代对象,将可重复使用的可迭代对象变为一次性的迭代器
- 7. 逐个将迭代器的元素提取出来
C语言和Python经常弄混,尤其总是忍不住给Python后面加分号……干脆给自己做个笔记省的忘了。。。(小甲鱼那边的) 十、序列 1. 列表、元组、字符串统称为序列
Python将列表、元组、字符串统称为序列,根据是否能被修改,分为可变序列(列表)和不可变序列(元组、字符串)
2. 一些函数 2.1 加法+拼接 和 乘法*拷贝>>> (1, 2) + (3,) (1, 2, 3)
- 对于可变序列——列表,id是不变的,相当于身份证
- 但是对于不可变序列——元组和字符串,id前后不一致
>>> s = [1,2] >>> id(s) #这个对象的身份证 1868669438272 >>> s *= 3 >>> s [1, 2, 1, 2, 1, 2] >>> id(s) 18686694382722.2 同一性运算符(检测id是否相同)
>>> x = [1,2] >>> y = [1,2] >>> x is y False >>> x = (1,2) >>> y = (1,2) >>> x is y False >>> x = 'ab' >>> y = 'ab' >>> x is y True2.3 判断包含
>>> 'a' in 'ab' True >>> 'a' not in 'ab' False2.4 del语句 2.4.1 删除指定元素
>>> x = 'ab' >>> y = [1,2] >>> del x, y >>> x Traceback (most recent call last): File "2.4.2 删除可变序列中的指定元素", line 1, in x NameError: name 'x' is not defined >>> y Traceback (most recent call last): File " ", line 1, in y NameError: name 'y' is not defined
>>> x = [1,2,3,4,5] >>> del x[1:4] #消除了x[1]、x[2]、x[3] >>> x [1, 5] #用切片的方法是一样的 >>> y = [1,2,3,4,5] >>> y[1:4] = [] >>> y [1, 5] #不过del语句也可以完成切片完成不了的事情: >>> x = [1,2,3,4,5] >>> del x[::2] #从第一个开始,步进2删除 >>> x [2, 4] #清空列表 >>> x = [1,2,3,4,5] >>> x.clear() >>> x [] >>> y = [1,2,3,4,5] >>> del y[:] >>> y []3. 一些函数(续) 3.1 列表、元组、字符串相互转换
#转换为列表:
>>> list('ab')
['a', 'b']
>>> list((1,2))
[1, 2]
#转化为元组:
>>> tuple('ab')
('a', 'b')
>>> tuple([1,2])
(1, 2)
#转化为字符串:
>>> str([1,2])
'[1, 2]'
>>> str((1,2))
'(1, 2)'
3.2 min()、max()
>>> s = [1,1,2,3,5] >>> min(s) 1 >>> t = 'FishC' >>> max(t) 's' >>> min(1,1,2,3,5) 1
如果是空的对象,会报错,添加一个default参数:
>>> s = [] >>> min(s) Traceback (most recent call last): File "3.3 len()、sum()", line 1, in min(s) ValueError: min() arg is an empty sequence >>> min(s, default='There is nothing') 'There is nothing'
len()函数有最大长度,要注意这个限制
>>> s = [1,1,2,3,5] >>> sum(s) 12 >>> sum(s, start=100) #添加了一个start参数表示从100开始加 1123.4 sorted()、reversed() 3.4.1 sorted()
>>> s = [1,2,3,0,6] >>> sorted(s) [0, 1, 2, 3, 6] >>> s [1, 2, 3, 0, 6] #s不改变 >>> s.sort() >>> s [0, 1, 2, 3, 6] #用sort()方法,s改变了 >>> sorted(s, reverse=True) #排序后翻转 [6, 3, 2, 1, 0] >>> t = ['food', 'Apple', 'Book', 'banana', 'cat'] >>> sorted(t) ['Apple', 'Book', 'banana', 'cat', 'food'] #首字母大写的排前面 >>> sorted(t, key=len) #关键字是len,表示干预排序,调用len函数,比较的是len函数的返回结果 ['cat', 'food', 'Book', 'Apple', 'banana'] #此时就不必在意对字符串的排序了 #同长度的字符串按照原来t的位置排序
列表的sort()函数只能处理列表,但是sorted()可以接受任何形式的可迭代对象作为参数:
>>> sorted('FishC')
['C', 'F', 'h', 'i', 's']
>>> sorted((1,3,0,2,4))
[0, 1, 2, 3, 4]
3.4.1 reversed():返回的是参数的反向迭代器
>>> s = [1,2,5,8,0] >>> reversed(s)4. 一些函数(再续) 4.1 all()、any()#是迭代器对象内存地址 >>> list(reversed(s)) #把reversed(s)作为一个可迭代对象,也可以元组 [0, 8, 5, 2, 1] #结果反过来了 >>> tuple(reversed('FishC')) ('C', 'h', 's', 'i', 'F') >>> tuple(reversed(range(0, 10))) (9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
>>> x = [1,1,0] >>> y = [1,2,9] >>> all(x) #判断是否全为真 False >>> all(y) True >>> any(x) #判断是否存在真 True >>> any(y) True4.2 enumerate()
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter'] >>> enumerate(seasons) #为seasons创建枚举对象4.3 zip()>>> list(enumerate(seasons)) [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')] #将seasons列表的每一个元素抽取出来,和一个0开始的索引一起构成元组,默认从0开始 >>> list(enumerate(seasons, 10)) #更改序号从10开始 [(10, 'Spring'), (11, 'Summer'), (12, 'Fall'), (13, 'Winter')]
>>> x = [1,2,3] >>> y = [4,5,6] >>> m = zip(x, y) >>> list(m) [(1, 4), (2, 5), (3, 6)] #各自组合起来了 >>> z = [7,8,9] >>> n = zip(x, y, z) >>> list(n) [(1, 4, 7), (2, 5, 8), (3, 6, 9)] #如果长度不同: >>> z = 'fishc' >>> list(zip(x, y, z)) [(1, 4, 'f'), (2, 5, 'i'), (3, 6, 's')] #以最短的长度为准 #如果不想忽略掉'h'和'c': >>> import itertools >>> zipped = itertools.zip_longest(x, y, z) >>> list(zipped) [(1, 4, 'f'), (2, 5, 'i'), (3, 6, 's'), (None, None, 'h'), (None, None, 'c')]4.4 map()
>>> mapped = map(ord, 'FishC') #第一个参数是指定一个函数,ord的作用是将一个字符转换成对应的编码值 >>> list(mapped) [70, 105, 115, 104, 67] #对应的字母的编码 #就是将'FishC'里面的每一个字符交给ord函数,转换得到对应的编码值,然后将结果保存起来 #如果对象很多: >>> mapped = map(pow, [2,3,10], [5,2,3]) #pow是计算次方 >>> list(mapped) [32, 9, 1000] #对应位置的次方,与[pow(2, 5), pow(3, 2), pow(10, 3)]的作用相同 #如果长度不同: >>> list(map(max, [1,3,5], [2,2,2], [0,3,9,8])) [2, 3, 9] #对比的是1,2,0; 3,2,3; 5,2,9。后面多出来的8就不算了。4.5 filter()
其实就是过滤:
>>> list(filter(str.islower, 'FishC')) #找到小写字母 ['i', 's', 'h']5. 迭代器与可迭代对象
一个迭代器肯定是一个可迭代对象;最大的区别是:可迭代对象可以重复使用,而迭代器是一次性的。
>>> mapped = map(ord, 'FishC') #map()返回的是一个迭代器 >>> for i in mapped: print(i) 70 105 115 104 67 >>> list(mapped) #前面将所有mapped里面的内容打印出来之后,mapped就被清空了 []6. 序列都是可迭代对象,将可重复使用的可迭代对象变为一次性的迭代器
>>> x = [1,2,3,4,5] >>> y = iter(x) >>> type(x)7. 逐个将迭代器的元素提取出来#类型是列表 >>> type(y) #类型是列表的迭代器
>>> x = [1,2,3,4,5] >>> y = iter(x) >>> next(y) #一个一个提取 1 >>> next(y) 2 >>> next(y) 3 >>> next(y) 4 >>> next(y) 5 >>> next(y) #y里面没有元素了,是个异常 Traceback (most recent call last): File "", line 1, in next(y) StopIteration
不希望传出异常:
>>> z = iter(x) >>> next(z, 'There is nothing') 1 >>> next(z, 'There is nothing') 2 >>> next(z, 'There is nothing') 3 >>> next(z, 'There is nothing') 4 >>> next(z, 'There is nothing') 5 >>> next(z, 'There is nothing') 'There is nothing' #这样相比上面,不提示异常,友好了很多



