Python初学小知识(六):序列


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)
1868669438272

2.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
True

2.3 判断包含

>>> 'a' in 'ab'
True
>>> 'a' not in 'ab'
False

2.4 del语句

2.4.1 删除指定元素
>>> x = 'ab'
>>> y = [1,2]
>>> del x, y
>>> x
Traceback (most recent call last):
  File "<pyshell#97>", line 1, in <module>
    x
NameError: name 'x' is not defined
>>> y
Traceback (most recent call last):
  File "<pyshell#98>", line 1, in <module>
    y
NameError: name 'y' is not defined
2.4.2 删除可变序列中的指定元素
>>> 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 "<pyshell#130>", line 1, in <module>
    min(s)
ValueError: min() arg is an empty sequence
>>> min(s, default='There is nothing')
'There is nothing'

3.3 len()、sum()

len()函数有最大长度,要注意这个限制

>>> s = [1,1,2,3,5]
>>> sum(s)
12
>>> sum(s, start=100)  #添加了一个start参数表示从100开始加
112

3.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)
<list_reverseiterator object at 0x000001B3155FCC70>  #是迭代器对象内存地址
>>> 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)

4. 一些函数(再续)

4.1 all()、any()

>>> x = [1,1,0]
>>> y = [1,2,9]
>>> all(x)  #判断是否全为真
False
>>> all(y)
True
>>> any(x)  #判断是否存在真
True
>>> any(y)
True

4.2 enumerate()

在这里插入图片描述

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> enumerate(seasons) 		 #为seasons创建枚举对象
<enumerate object at 0x000001B3156027C0>  
>>> 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')]

4.3 zip()

在这里插入图片描述

>>> 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)
<class 'list'>  #类型是列表
>>> type(y)
<class 'list_iterator'>  #类型是列表的迭代器

7. 逐个将迭代器的元素提取出来

>>> 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 "<pyshell#204>", line 1, in <module>
    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'  #这样相比上面,不提示异常,友好了很多
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值