[Python][迭代器和生成器]自定义迭代模式
发布日期:2021-05-28 16:50:26 浏览次数:33 分类:精选文章

本文共 543 字,大约阅读时间需要 1 分钟。

接码题

代码实现示例

创造一个生成器

def float_range(init, end, step):      x = init      while x < end:          yield x          x += step

测试过程

for f in float_range(0, 4, 0.5):      print(f)   produce a list by float_range(0,1,0.125) and print it: flist = list(float_range(0,1,0.125)) print(flist)

深度解析

解析示例

def countdown(n):      print('开始计数从', n)      while n > 0:          yield n          n -= 1      print('完成!')  c = countdown(3)  print(next(c))  print(next(c))  print(next(c))  try:      print(next(c))  except StopIteration:      print("已经到达结束.")

实验结果

上一篇:IntelliJ Idea学习笔记001--- IntelliJ Idea常用快捷键列表
下一篇:[5GC]SMF用户面管理功能

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2025年04月18日 10时50分15秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章