赞
踩
目录
3.3 对应iteator, 除了使用map() 函数,用list的闭包形式也可以达到相同效果
4.2 奇怪的内容:(原因不明,看起来只有str.strip() 符合要求)
5 join() 函数: 分隔符号.join(iteator) 返回字符串
iterator
#一般只会返回其 object 及其物理地址,而不会返回其 迭代的内容
# print(iterator)
#在jupyter notebook 可以直接输出迭代内容
# 其他IDE不确定?
for x in iterator:
print(x)
print([a for a in A2])
list(iterator)
print(list(iterator))
- A=[1,2,3]
-
- print("\nA=",end="")
- A
-
-
- print("\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]
- # 可迭代对象:iterable object
- # 可迭代对象:list,tuple,string
-
- a=[1,2,3]
- b=[4,5,6]
- c="abcdefgh"
-
- print(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')]
- #map(function,iterable) 将函数映射到可迭代对象上
-
- #可使用自定义函数
- def square(x):
- return x**2
- a=map(square,[1,2,3])
- a
- print(a)
- print(list(a))
-
- #lambda 匿名函数也行
- a=map(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))
-
-
- #允许映射到多个可迭代对象
- a=map(lambda x,y:(x*y,x+y),[1,2],[3,4])
- print(list(a))
-
- #没有函数时,类zip()函数??
- a=map(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))
-
- #对元组
- a=map(int,(1,2,3))
- print(list(a)) #ValueError: invalid literal for int() with base 10: 'a'
-
- #对字典这种可迭代对象
- a=map(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'}
- a=my_dict.keys()
- b=my_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']
- A1=[1,2,3]
-
- print([a for a in A1])
- print([a**2 for a in A1])
输出结果 [1, 2, 3] [1, 4, 9]
str1=" hello world " #原字符串:13个字的字符串,包含边上2个空格
str2=map(string.strip,str1) # 返回:13个没有两边空格的字符串,把原字符串映射了n次?
str3=map(str.strip,str1) # 返回:会分切为13个单个字母,符合一般的需求
str4=map(str1.strip,str1) # 返回:1个没有两边空格的字符串+12个有两边空格的原字符串,把原字符串映射了n次?
- str2=map(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']
- str3=map(str.strip,str1)对应结果
- <map object at 0x0000022DAF288AF0>
- ['', 'h', 'e', 'l', 'l', 'o', '', 'w', 'o', 'r', 'l', 'd', '']
- str4=map(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']
-
- # 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 "
- #错误写法:不是独立的方法, str1=strip(str1)
- #正确写法:是字符串.方法(), str1=str1.strip()
-
- #string.strip 这个方法效果匪夷所思
- str2=map(string.strip,str1)
- str2
- print(str2)
- print(list(str2))
- print()
-
- #str.strip 切成一个个字符
- str3=map(str.strip,str1)
- str3
- print(str3)
- print(list(str3))
- print()
-
- #str.strip 切成一个个字符
- str4=map(str1.strip,str1)
- str4
- print(str4)
- print(list(str4))
- print()
-
输出结果
Hello World123456 3233121 Hello World 123456333211 <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'] <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']
- items = ['apple', 'banana', 'orange']
- separator = ', '
- result = separator.join(items)
- print(result)
out: apple, banana, orange
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。