迭代器
可迭代对象:可迭代对象实现了__iter__方法,该方法返回一个迭代器对象。
迭代器: 那么什么迭代器呢?它是一个带状态的对象,他能在你调用next()方法的时候返回容器中的下一个值,任何实现了__iter__和__next__(python2中实现next())方法的对象都是迭代器,__iter__返回迭代器自身,__next__返回容器中的下一个值,如果容器中没有更多元素了,则抛出StopIteration异常。
a = [\'aaa\', \'bbb\', \'ccc\']
my_iterable = MyIterable(a)
res2 = iter(my_iterable)
while True:
try:
print(next(res2))
except StopIteration as e:
break
生成器
class Project(object):
def __init__(self, start, end):
self.start = start
self.end = end
def isPrimeNum(self, k): # 判断素数
if k < 2:
return False
for i in range(2, k):
if k % i == 0:
return False
return True
def __iter__(self):
for i in range(self.start, self.end):
if self.isPrimeNum(i):
yield i
###################################
res = Project(1,100)
print(iter(res))
class Project(object):
def __init__(self, start, end):
self.start = start
self.end = end
def isPrimeNum(self, k): # 判断素数
if k < 2:
return False
for i in range(2, k):
if k % i == 0:
return False
return True
def __iter__(self):
for i in range(self.start, self.end):
if self.isPrimeNum(i):
yield i
###################################
res = Project(1,100)
print(iter(res))
正反序的自定义生成器
class MyFloatRange(object):
def __init__(self, start, end, step):
self.start = start
self.end = end
self.step = step
def __iter__(self):
for i in range(self.start, self.end):
if self.start <= self.end:
yield self.start
self.start += self.step
def __reversed__(self):
for i in range(self.start, self.end):
if self.start <= self.end:
yield self.end
self.end -= self.step
for i in MyFloatRange(1, 11, 2):
print(i)
for i in reversed(MyFloatRange(1, 11, 2)):
print(i)
class MyFloatRange(object):
def __init__(self, start, end, step):
self.start = start
self.end = end
self.step = step
def __iter__(self):
for i in range(self.start, self.end):
if self.start <= self.end:
yield self.start
self.start += self.step
def __reversed__(self):
for i in range(self.start, self.end):
if self.start <= self.end:
yield self.end
self.end -= self.step
for i in MyFloatRange(1, 11, 2):
print(i)
for i in reversed(MyFloatRange(1, 11, 2)):
print(i)
如何对迭代器做切片操作
我们使用from itertools import islice
from itertools import islice
#对文件的每一行做切片操作
f = open("xxx.txt", "r")
res = islice(f, 3, 9)
循环res
from itertools import islice
#对文件的每一行做切片操作
f = open("xxx.txt", "r")
res = islice(f, 3, 9)
循环res
如何在for循环中迭代多个对象(并行)
我们通过zip这个拉链函数
from random import randint
chin = [randint(50, 100) for _ in range(40)]
math = [randint(50, 100) for _ in range(40)]
eigh = [randint(50, 100) for _ in range(40)]
for x,y,z in zip(chin, math, eigh):
print(x, y, z)
from random import randint
chin = [randint(50, 100) for _ in range(40)]
math = [randint(50, 100) for _ in range(40)]
eigh = [randint(50, 100) for _ in range(40)]
for x,y,z in zip(chin, math, eigh):
print(x, y, z)
如何在for循环中迭代多个对象(串行)
我们通过from itertools import chain函数
from random import randint
from itertools import chain
chin = [randint(50, 100) for _ in range(5)]
math = [randint(50, 100) for _ in range(5)]
eigh = [randint(50, 100) for _ in range(5)]
for i in chain(chin, math, eigh):
print(i)
from random import randint
from itertools import chain
chin = [randint(50, 100) for _ in range(5)]
math = [randint(50, 100) for _ in range(5)]
eigh = [randint(50, 100) for _ in range(5)]
for i in chain(chin, math, eigh):
print(i)