文章目录
写在前面
文章内容为个人学习笔记,学习内容源自“小甲鱼”-《零基础入门学习Python》
1、序列
1、可以通过索引获取每一个元素
2、第一元素的索引都是0
3、可以通过切片方法获取某一范围内的集合
4、都有很多共有的运算法
根据是否能被修改
1、可变序列:列表
2、不可变序列:元组、字符串
2、用于序列的运算符
2.1、“+”和“*”
# “+”和“*”
# 可变序列进行运算,变量名存储位置不会变更,数据进行变更
# 不可变序列进行运算,本质是新建一个变量空间进行赋值,变量名存储空间变更,数据不变。
# 可变序列
>>> x = [1, 2, 3]
>>> id(x)
1522377680832
>>> x *= 2
>>> id(x)
1522377680832 # ID值不变
>>> x
[1, 2, 3, 1, 2, 3]
# 不可变序列
>>> y = (1, 2, 3)
>>> id(y)
1522408830848
>>> y *= 2
>>> id(y)
1522408546752 # ID值发生变更,y起始是一个新的变量名
>>> y
(1, 2, 3, 1, 2, 3)
2.2、is和is not,同一性运算符
>>> x = [1, 2, 3]
>>> y = [1, 2, 3]
>>> x == y
True
>>> x is y # 数值相等,但是对象不是同一个
False
2.3、in和not in,包含性运算符
# 用于判断某个元素是否包含在序列中
>>> "baidu" in "www.baidu.com"
True
>>> "baidu" in ['baidu', 'google', 'bing']
True
>>> "baidu" in ('baidu', 'google', 'bing')
True
2.4、del,删除一个或多个指定的对象/元素
# 删除对象
>>> x = [1, 2, 3]
>>> y = [1, 2, 3]
>>> x; y
[1, 2, 3]
[1, 2, 3]
>>> del x, y
>>> x; y
Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
x; y
NameError: name 'x' is not defined
# 删除可变序列中的指定元素
>>> x = [1, 2, 3, 4, 5]
>>> del x[1:3] # 等同于 x[1:3] = []
>>> x
[1, 4, 5]
>>> x = [1, 2, 3, 4, 5]
>>> del x[::2]
>>> x
[2, 4]
>>> x = [1, 2, 3, 4, 5]
>>> del x[:] # 等同于 x.clear()
>>> x
[]
3、序列对象(可迭代对象)函数
3.1、list()、tuple()、str()
# list()、tuple()、str()
>>> list("www.baidu.com")
['w', 'w', 'w', '.', 'b', 'a', 'i', 'd', 'u', '.', 'c', 'o', 'm']
>>> list((1, 2, 3))
[1, 2, 3]
>>> tuple("www.baidu.com")
('w', 'w', 'w', '.', 'b', 'a', 'i', 'd', 'u', '.', 'c', 'o', 'm')
>>> tuple([1, 2, 3])
(1, 2, 3)
3.2、min()、max()
# 传参可以是可迭代对象,也可以传参多个参数
# default参数,函数运行出错是,输出default指定的内容
# min(iterable, *[, key, default]), 返回最小值
# min(arg1, arg2, *args[, key])
# max(iterable, *[, key, default]),返回最大值
# max(arg1, arg2, *args[, key])
>>> min([1, 2, 3, 4, 5])
1
>>> min('www.baidu.com') # 字符串,比较字符的ASCII码
'.'
>>> min('Iamagoodman')
'I'
# default参数用法
>>> s = []
>>> min(s)
Traceback (most recent call last):
File "<pyshell#47>", line 1, in <module>
min(s)
ValueError: min() arg is an empty sequence
>>> min(s, default='No data!')
'No data!'
3.3、len()、sum()
# len()函数检查的长度,有范围!!!
# len()函数底层使用C语言编写,根本原理是直接读取C语言结构体里面对象的长度
# 32位系统最大长度为2的31次方-1
# 64位系统最大长度为2的63次方-1
# sum(iterable, /, start=0)
# 传参是可迭代对象
# start参数指定sum函数加预算到的起始值
>>> num = [9, 5, 2, 7]
>>> sum(num)
23
>>> sum(num, start=100)
123
3.4、sorted()、reversed()
# sorted(iterable, /, *, key=None, reverse=False)
# 和列表的list.sort方法类似,执行可迭代对象的排序
# 区别!!!sorted()函数返回一个新的对象,list.sort()方法更改原来的对象
# 区别!!!list.sort()只能处理列表,sorted()能处理字符串、元组,返回值是列表
>>> num = [9, 5, 2, 7]
>>> sorted(num)
[2, 5, 7, 9]
>>> num # sorted()函数使用后,原对象不改变
[9, 5, 2, 7]
>>>
>>> num.sort() # list.sort()方法使用后,原对象改变,不返回新的对象
>>> num
[2, 5, 7, 9]
>>> sorted(["China", "Canada", "Amarica", "England"])
['Amarica', 'Canada', 'China', 'England']
>>> sorted(["China", "Canada", "Amarica", "England"], key = len)
['China', 'Canada', 'Amarica', 'England']
>>> sorted(["China", "Canada", "Amarica", "England"], key = len, reverse = True)
['Amarica', 'England', 'Canada', 'China']
# reversed(seq)
# 返回值是一个迭代器!!!一个可迭代对象
>>> x = ["China", "Canada", "Amarica", "England"]
>>> reversed(x)
<list_reverseiterator object at 0x0000016276A45D30>
>>> list(reversed(x)) # 可迭代对象,可以使用可迭代方法进行转换
['England', 'Amarica', 'Canada', 'China']
3.5、all()、any()
# all(iterable)
# 判断可迭代对象中每个元素的值为真
>>> x=[1, 2, 3, 0]
>>> y=[1, 2, 3, 4]
>>> all(x) # 0表示假
False
>>> all(y)
True
# any(iterable)
# 判断可迭代对象中是否存在某个元素的值为真
>>> x = [0, 0, 0, 666, 0]
>>> any(x)
True
3.6、enumerate()
# enumerate(iterable, start=0)
# 将可迭代对象中的每个元素和元素的索引,构成一个元组,返回一个枚举对象
# start参数指定返回时元素的索引起始值
>>> x = ["China", "Canada", "Amarica", "England"]
>>> enumerate(x)
<enumerate object at 0x0000016276A5B700>
>>> list(enumerate(x))
[(0, 'China'), (1, 'Canada'), (2, 'Amarica'), (3, 'England')]
>>> list(enumerate(x, start = 20))
[(20, 'China'), (21, 'Canada'), (22, 'Amarica'), (23, 'England')]
3.7、*zip()
# zip(*iterables)
# 传参是多个可迭代对象,将每个对象的第i个元素取出,组成一个元组
# 对个迭代对象的长度不一致,以最短的对象长度为准
>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zipped = zip(x, y)
>>> list(zipped)
[(1, 4), (2, 5), (3, 6)]
>>> x2, y2 = zip(*zip(x, y))
>>> x == list(x2) and y == list(y2)
True
# 可迭代对象的长度不一致,以短的长度为准,其他丢掉
# 如果要以长的长度为准,可使用itertools模块的zip_logest()方法,没有的内容填充None
>>> x=[1, 2, 3]
>>> y=[1, 2, 3]
>>> z=["China", "Canada", "Amarica", "England"]
>>> list(zip(x, y, z))
[(1, 1, 'China'), (2, 2, 'Canada'), (3, 3, 'Amarica')]
>>> import itertools
>>> list(itertools.zip_longest(x, y, z))
[(1, 1, 'China'), (2, 2, 'Canada'), (3, 3, 'Amarica'), (None, None, 'England')]
3.8、*map()
# map(function, iterable, ...)
# 根据提供函数,对指定的可迭代对象的每一个元素进行运算,并将运算的结果生成迭代器返回。
>>> mapped = map(ord, "hello") # ord函数,将字符串转ASCII码
>>> list(mapped)
[104, 101, 108, 108, 111]
>>> mapped = map(pow, [2, 3, 5], [4, 2, 2])
>>> list(mapped)
[16, 9, 25]
3.9、*filter()
# filter(function, iterable)
# 根据提供函数,对指定的可迭代对象的每一个元素进行运算,并将运算的结果为真的元素,生成迭代器返回。
# 将iterable中每个元素,使用function函数进行运算,运算的结果为真,将该元素放入迭代器返回,反之丢弃该元素。
>>> list(filter(str.islower, 'BAIdu'))
['d', 'u']
4、迭代器和可迭代对象
1、一个迭代器肯定是一个可迭代对象。
2、可迭代对象可以重复使用,迭代器则只能使用一次
>>> mapped = map(pow, [2, 3, 5], [4, 2, 2]) # map()返回一个迭代器
>>> list(mapped) # 使用第一次
[16, 9, 25]
>>> list(mapped) # 使用第二次
[]
# iter(),将可迭代对象,转为迭代器
>>> x = [1, 2, 3, 4]
>>> y = iter(x)
>>> print(type(x)); print(type(y))
<class 'list'>
<class 'list_iterator'>
# next(),逐个提取迭代器中的元素
>>> x = iter(["China", "Canada", "Amarica", "England"])
>>> while True:
print(next(x))
China
Canada
Amarica
England
Traceback (most recent call last): # 迭代器运行结束,默认抛出异常
File "<pyshell#141>", line 2, in <module>
print(next(x))
StopIteration
1429

被折叠的 条评论
为什么被折叠?



