在线制作手机网站,织梦做的网站 首页有空白,山西网站建设设计,关键词完整版免费听目录
1 python里的可迭代对象
1.1 什么是可迭代对象
1.2 python里的可迭代对象
1.3 可迭代对象如何遍历
1.3.1 可迭代方法
1.3.2 迭代器的测试
1.4 python里的可迭代对象都不是向量#xff0c;加法等是合并规则
1.5 可迭代对象不支持减法操作
1.6 可迭代器可以直接用…
目录
1 python里的可迭代对象
1.1 什么是可迭代对象
1.2 python里的可迭代对象
1.3 可迭代对象如何遍历
1.3.1 可迭代方法
1.3.2 迭代器的测试
1.4 python里的可迭代对象都不是向量加法等是合并规则
1.5 可迭代对象不支持减法操作
1.6 可迭代器可以直接用in判断单个元素的包含操作但是不能直接判断lsit tuple的包含关系
2 zip()函数
我愿理解zip()为一个矩阵横向和纵向两种组合方式转化
2.1 zip() 函数定义
2.2 zip()函数的效果
2.3 zip() 函数和 zip(*) 函数
2.4 测试代码
3 map()函数
3.1 map(函数
3.2 测试代码1
3.3 对应iteator, 除了使用map() 函数,用list的闭包形式也可以达到相同效果
4 str.strip() 字符串的strip()方法
4.1 str.strip() 的方法
4.2 奇怪的内容原因不明看起来只有str.strip() 符合要求
4.3 测试代码
5 join() 函数: 分隔符号.join(iteator) 返回字符串
5.1 基本语法分隔符号.join(iteator)
5.2 iteator可以是闭包或各种返回为迭代器都可以 1 python里的可迭代对象
1.1 什么是可迭代对象
可循环遍历的就是可迭代的也就是可以使用for循环遍历它们的对象写个for循环就可以遍历的这种python里还可以用list() 遍历更方便可迭代的iterable 可迭代的对象 iterable object迭代器 iterator 1.2 python里的可迭代对象
listtuplestring #字符串天然按其前后次序可迭代dictionaryset也可以自定义可迭代对象 1.3 可迭代对象如何遍历
python里对可迭代对象的遍历是很方便的最简单的遍历方法有如下几种
1.3.1 可迭代方法
方法1直接输出迭代器iterator iterator #一般只会返回其 object 及其物理地址而不会返回其 迭代的内容 方法2直接用print()函数 # print(iterator) #在jupyter notebook 可以直接输出迭代内容 # 其他IDE不确定 方法3使用 for 循环对其进行遍历迭代器iterator for x in iterator: print(x) 方法4直接使用list的闭包形式[]
print([a for a in A2]) 方法5使用 list() 函数将迭代器转换为一个列表 list(iterator) print(list(iterator)) 1.3.2 迭代器的测试
A[1,2,3]print(\nA,end)
Aprint(\nprint(A),end)
print(A)print(\nfor a in A:,end)
for a in A:print(a)print(\n[a for a in A],end)
print([a for a in A])print(\nlist(A),end)
print(list(A)) 输出结果 A
print(A)[1, 2, 3]for a in A:1
2
3[a for a in A][1, 2, 3]list(A)[1, 2, 3] 1.4 python里的可迭代对象都不是向量加法等是合并规则
python里的可迭代对象,包括list[] 都不是向量加法等是合并规则只有numpy里的 ndarray 对象里的一维数组才是向量计算规则
#python的原生语法只有list tuple等类型list也不是向量都只是可迭代对象
a1Hello
b1Worlda2[1,2,3]
b2[4,5,6]a3(1,2,3,4)
b3(5,6,7,8)print(a1b1)
print(a2b2)
print(a3b3)#numpy里的array才是向量矩阵张量等计算才是向量规则
import numpy as np
a4np.array([1,2,3])
b4np.array([4,5,6])print(a4b4) 【OUT】
HelloWorld
[1, 2, 3, 4, 5, 6]
(1, 2, 3, 4, 5, 6, 7, 8)
[5 7 9] 1.5 可迭代对象不支持减法操作
#可迭代器不支持减法操作#TypeError: unsupported operand type(s) for -: str and str#TypeError: unsupported operand type(s) for -: list and list 1.6 可迭代器可以直接用in判断单个元素的包含操作但是不能直接判断lsit tuple的包含关系
#可迭代器可以直接用in判断包含操作但是要注意只能是单个元素,不能直接判断lsit tuple的包含关系如果要判断不同list之间包含关系估计要写个遍历判断下
#试试减法in
#python的原生语法只有list tuple等类型list也不是向量都只是可迭代对象
a1Hello
b1lloa2[1,2,3]
b23
b20[3]
b21[2,3]a3(1,2,3,4)
b34
b30(4)
b31(3,4)#可迭代器不支持减法操作
#TypeError: unsupported operand type(s) for -: str and str
#TypeError: unsupported operand type(s) for -: list and list
#print(a1-b1)
#print(a2-b2)
#print(a3-b3)#可迭代器可以直接用in判断包含操作但是要注意只能是单个元素, 不能直接判断lsit tuple的包含关系
print(b1 in a1)
print()print(b2 in a2)
print(b20 in a2)
print(b21 in a2)
print()print(b3 in a3)
print(b30 in a3)
print(b31 in a3)
print() 【OUT】
TrueTrue
False
FalseTrue
True
False 1.7 iteator,其中list 可用闭包形式实现遍历和函数映射
iteator,其中list 可用闭包形式实现遍历和函数映射闭包形式[][function(i) for i in list ]
A1[1,2,3]print([a for a in A1])
print([a**2 for a in A1]) 输出结果
[1, 2, 3]
[1, 4, 9] 2 zip()函数
我愿理解zip()为一个矩阵横向和纵向两种组合方式转化
2.1 zip() 函数定义
zip()函数我愿理解zip()为一个矩阵横向和纵向两种组合方式转化zip() 函数用于将可迭代的对象作为参数将对象中对应的元素index相同打包成一个个元组然后返回由这些元组组成的列表。如果各个迭代器的元素个数不一致则返回列表长度与最短的对象相同利用 * 号操作符可以将元组解压为列表。zip 方法在 Python 2 和 Python 3 中的不同在 Python 3.x 中为了减少内存zip() 返回的是一个对象。如需展示列表需手动 list() 转换。 2.2 zip()函数的效果
zip()函数的效果把几个数组排列后把index相同的排成新的数组多余的丢弃效果相当于 矩阵按行排列修改为按列去排。 2.3 zip() 函数和 zip(*) 函数
下面两者互为逆运算czip(a,b)zip(*c)a,b 2.4 测试代码
# 可迭代对象iterable object
# 可迭代对象list,tuple,stringa[1,2,3]
b[4,5,6]
cabcdefghprint(a)
print(zip(a,b))
print(list(zip(a,b)))
print()print(c)
print(list(c))
print(zip(a,c))
print(list(zip(a,c)))#可见在zip()这
#string就等同于list(string),都是可迭代对象
#但是这2个对象从名称看还是有差别的 25880 125740
print(zip(a,list(c)))
print(list(zip(a,list(c))))
print()zip(*zip(a,b))
print(zip(*zip(a,b)))
print(list(zip(*zip(a,b))))
print(list(zip(*zip(a,c)))) [1, 2, 3]
zip object at 0x0000022DADAC4B40
[(1, 4), (2, 5), (3, 6)]abcdefgh
[a, b, c, d, e, f, g, h]
zip object at 0x0000022DADAC4B40
[(1, a), (2, b), (3, c)]
zip object at 0x0000022DAE0F3D80
[(1, a), (2, b), (3, c)]zip object at 0x0000022DAE0F3D80
[(1, 2, 3), (4, 5, 6)]
[(1, 2, 3), (a, b, c)] 3 map()函数
3.1 map(函数
map(function, iterable)map()函数用于将函数映射到可迭代对象中的每个元素可迭代对象中的每个元素对于可迭代对象中的每个元素应用该函数函数返回值包含在生成的map对象中。第一个参数接受一个函数名后面的参数接受一个或多个可迭代的序列(列表元组集合返回的是一个集合。即后边的可迭代对象中的每个元素依次作用到函数并返回输出值构成一个集合。 3.2 测试代码1
map(function, iterator)其中function可以有很多种类型
function自定义函数function匿名函数 map(lambda item: [item[0], item[1], item[1] * TAX], carts)lambda x,y:x*yfunctionnone这也可以function系统函数比如 intstr.strip 等等也可以
iterator
liststringtupledictionary... #map(function,iterable) 将函数映射到可迭代对象上#可使用自定义函数
def square(x):return x**2
amap(square,[1,2,3])
a
print(a)
print(list(a))#lambda 匿名函数也行
amap(lambda x,y:x*y,[1,2],[3,4])
print(list(a))names [david, peter, jenifer]
new_names map(lambda name: name.capitalize(), names)
print(list(new_names))#复杂一点的
carts [[SmartPhone, 400],[Tablet, 450],[Laptop, 700]]
TAX 0.1
carts map(lambda item: [item[0], item[1], item[1] * TAX], carts)
print(list(carts))#允许映射到多个可迭代对象
amap(lambda x,y:(x*y,xy),[1,2],[3,4])
print(list(a))#没有函数时类zip()函数
amap(None,[1,2],[3,4])
#print(list(a)) #TypeError: NoneType object is not callable#对字符串这种对象
string Hello World
result list(map(str.strip, string))
print(result)string Hello World
a map(str.strip, string)
print(list(a))#对元组
amap(int,(1,2,3))
print(list(a)) #ValueError: invalid literal for int() with base 10: a#对字典这种可迭代对象
amap(int,{a:1,b:2,c:3})
#print(list(a))#对字典的keys,values的映射
my_dict {name: Alice, age: 25, city: New York}
a map(lambda x: x[0], my_dict.items())
b map(lambda x: x[-1], my_dict.items())
print(list(a))
print(list(b))#字典本身的方法也可以做到一样的效果
my_dict {name: Alice, age: 25, city: New York}
amy_dict.keys()
bmy_dict.values()
print(list(a))
print(list(b)) 运行结果
map object at 0x0000022DAF27D240
[1, 4, 9]
[3, 8]
[David, Peter, Jenifer]
[[SmartPhone, 400, 40.0], [Tablet, 450, 45.0], [Laptop, 700, 70.0]]
[(3, 4), (8, 6)]
[H, e, l, l, o, , W, o, r, l, d]
[H, e, l, l, o, , W, o, r, l, d]
[1, 2, 3]
[name, age, city]
[Alice, 25, New York]
[name, age, city]
[Alice, 25, New York] 3.3 对应iteator, 除了使用map() 函数,用list的闭包形式也可以达到相同效果
A1[1,2,3]print([a for a in A1])
print([a**2 for a in A1]) 输出结果
[1, 2, 3]
[1, 4, 9] 4 str.strip() 字符串的strip()方法
4.1 str.strip() 的方法
#错误写法在python不是独立的函数, str1strip(str1)#正确写法是字符串.方法(), str1str1.strip() string.strip() 方法必须是none 或者是string不能是数字123这种不带参数的删除字符串首尾的指定字符如果为空则是各种空白/n, /r, /t, 等 4.2 奇怪的内容原因不明看起来只有str.strip() 符合要求
下面3种用法语法上都OK但是结果不相同string.strip() str.strip() #从结果上看正确用法str1.strip() str1 hello world #原字符串13个字的字符串包含边上2个空格 str2map(string.strip,str1) # 返回13个没有两边空格的字符串把原字符串映射了n次 str3map(str.strip,str1) # 返回会分切为13个单个字母符合一般的需求 str4map(str1.strip,str1) # 返回1个没有两边空格的字符串12个有两边空格的原字符串把原字符串映射了n次 str2map(string.strip,str1)对应结果 map object at 0x0000022DAF288460[Hello World, Hello World, Hello World, Hello World, Hello World, Hello World, Hello World, Hello World, Hello World, Hello World, Hello World, Hello Worl, Hello World]str3map(str.strip,str1)对应结果 map object at 0x0000022DAF288AF0[, h, e, l, l, o, , w, o, r, l, d, ]str4map(str1.strip,str1)对应结果 map object at 0x0000022DAF28A020[hello world, hello world , hello world , hello world , hello world , hello world , hello world, hello world , hello world , hello world , hello world , hello world , hello world] 4.3 测试代码 # string.strip()
#方法必须是none 或者是string不能是数字123这种
str1 3233121Hello World123456333211
str1.strip(123) #TypeError: strip arg must be None or str
print(str1.strip(123))# 不带参数的
# 删除字符串首尾的指定字符如果为空则是各种空白/n, /r, /t, 等
str1 3233121 Hello World 123456333211
print(str1.strip())
print()str1 hello world
#错误写法不是独立的方法 str1strip(str1)
#正确写法是字符串.方法(), str1str1.strip()#string.strip 这个方法效果匪夷所思
str2map(string.strip,str1)
str2
print(str2)
print(list(str2))
print()#str.strip 切成一个个字符
str3map(str.strip,str1)
str3
print(str3)
print(list(str3))
print()#str.strip 切成一个个字符
str4map(str1.strip,str1)
str4
print(str4)
print(list(str4))
print() 输出结果 Hello World123456
3233121 Hello World 123456333211map object at 0x0000022DAF288460
[Hello World, Hello World, Hello World, Hello World, Hello World, Hello World, Hello World, Hello World, Hello World, Hello World, Hello World, Hello Worl, Hello World]map object at 0x0000022DAF288AF0
[, h, e, l, l, o, , w, o, r, l, d, ]map object at 0x0000022DAF28A020
[hello world, hello world , hello world , hello world , hello world , hello world , hello world, hello world , hello world , hello world , hello world , hello world , hello world]5 join() 函数: 分隔符号.join(iteator) 返回字符串 5.1 基本语法分隔符号.join(iteator)
分隔符号.join(iteator)分隔符号iteator迭代器返回字符串
items [apple, banana, orange]
separator ,
result separator.join(items)
print(result) out:
apple, banana, orange 5.2 iteator可以是闭包或各种返回为迭代器都可以
分隔符号.join(iteator)iteator迭代器可以是各种带条件的