python的几个迭代器
1,range()
for i in range(10):
print(i)
2,count()
from itertools import count
for i in count():
if i >= 10:
break
print(i)
3,zip()
L1 = [1,3,5,7,9]
L2 = [2,4,6,8,10]
for i,j,z in zip(range(len(L1)),L1,L2):
print(i,j,z)
4,enumerate()
for i,j in enumerate([2,4,6,8]):
print(i,j)
5.filter() 可以传入函数参数
func = lambda x : x % 2 == 0
for i in filter(func,range(10)):
print(i) # 输出结果为True的值
6.迭代器也可以作为函数的参数传入
print(*range(5)) # 0 1 2 3 4
print(*map(lambda x : x ** 2 , range(5))) # 0 1 4 9 16