可迭代对象
- 内置有__iter__方法的对象都是可迭代对象,字符串、列表、元组、字典、集合、打开的文件都是可迭代对象:
迭代器对象
- 迭代器对象是内置有iter和next
a={'dhy','ddhy','dddhy'}
b=iter(a)
print(next(b)) #dddhy
print(next(b)) #dhy
print(next(b)) #ddhy
for循环内部原理
有了迭代器后,我们便可以不依赖索引迭代取值了,使用while循环的实现方式如
number = [1, 2, 3, 4, 5, 6]
dhy = iter(number)
while True:
try:
print(dhy.__next__())
except StopIteration:
break
计时器
import time
start_time = time.time()
print(f'运行时间: {time.time()-start_time}s')
标签:迭代,iter,next,dhy,time,print
From: https://www.cnblogs.com/dhyuan/p/17917216.html