python 序列学习笔记

python 序列学习笔记

list:
str:
tuple:
min:
max:
len:
sorted:1、返回全新的列表而不改变原列表 2、可处理多种数据类型,如里列表,字符串,元组
sort:1改变源列表 2、只能处理列表

reversed:返回逆向迭代器
reverse:

all:判断可迭代对象中是否所有元素都为真
any:判断可迭代对象中是否存在某个元素为真

enumerate():用于返回一个枚举对象,它的功能就是将可迭代对象中每个元素从0开始的序号共同构成一个二元组的列表
seasons = [“Spring”, “Summer”, “Autumn”, “Winter”]
list(enumerate(seasons, start=1))
[(1, ‘Spring’), (2, ‘Summer’), (3, ‘Autumn’), (4, ‘Winter’)]

zip()函数:用于创建一个聚合多个可迭代对象的迭代器,它会将作为参数传入的每个可迭代对象的每个元素依次组合成元组,即第i个元组包含来自每个参数的第i个元素

s = [1, 2, 3]
t = [4, 5, 6]
zipped = zip(x, y)
Traceback (most recent call last):
File “<pyshell#61>”, line 1, in
zipped = zip(x, y)
NameError: name ‘x’ is not defined
zipped = zip(s, t)
list(zipped)
[(1, 4), (2, 5), (3, 6)]
z = [7, 8, 9]
list(zip(s, t, z))
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
x = “Fishc”

list(zip(s, t, z, x))
[(1, 4, 7, ‘F’), (2, 5, 8, ‘i’), (3, 6, 9, ‘s’)]

import itertools
zipped = itertools.zip_longest([1, 2, 3], [4, 5, 6], “Fishc”)
list(zipped)
[(1, 4, ‘F’), (2, 5, ‘i’), (3, 6, ‘s’), (None, None, ‘h’), (None, None, ‘c’)]

map():会根据提供的函数对指定的可迭代对象进行运算,并将返回运算结果的迭代器

list(map(ord, “Fishc”))
[70, 105, 115, 104, 99]
list(map(pow, [2, 3, 10], [5, 2, 3]))
[32, 9, 1000]

list(map(min, [0, 1, 3], [3, 2, -1], [-5, 0, 6]))
[-5, 0, -1]
list(map(min, [0, 1, 3], [3, 2, -1], [-5, 0, 6, 7, 8]))
[-5, 0, -1]

Filter():根据提供的函数对所指定的每个可迭代对象进行运算,并将运算结果为真的元素以迭代器的形式返回

list(filter(str.islower, “Fishc”))
[‘i’, ‘s’, ‘h’, ‘c’]
list(filter(str.isupper, “Fishc”))
[‘F’]

迭代器:一次性
可迭代对象:可重复操作

代码片段1运行结果:

x = [1, 2, 5, 8, 0]
y =reversed(s)
Traceback (most recent call last):
File “<pyshell#1>”, line 1, in
y =reversed(s)
NameError: name ‘s’ is not defined
y =reversed(x)
print(y)
<list_reverseiterator object at 0x000001CFB18D2DD8>
print(y)
<list_reverseiterator object at 0x000001CFB18D2DD8>

for each in y:
print(each)

0
8
5
2
1

list(y)
[]

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值