Python Day 03

Generator

An infinite list cannot be enumerated because of the limits of ram. But we could use the generator to identify the principles of list generation.

Note: use the generator as the List Comprehensions but replace the [] by ()

l=[x*(x-1) for x in rang(100)]
g=(x*(x-1) for x in rang(100))

l is a list while the g is a generator

how to call a generator

  • the function next() can help you obtain the next value of the generator
  • you can also obtain the value by for loop
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,682评论 0 10
  • 我们继续在卡塔赫纳小住了三天。 早起就去老城,逛逛集市。 晚出就去城墙走走,看看夕阳。 或者坐在曾经大炮架设孔洞里...
    Jack不是船长阅读 4,081评论 14 10
  • 今日份的C++学习总结 1.如何用输入流计算以空格隔开的单词注意头文件 还有一种 2.如何使double型的用co...
    Dreamindream阅读 160评论 0 0
  • 中考还有3个多礼拜,我难以压制我的焦虑,和同事谈了初中数学与高中数学的关系,方才知道,孩子现在,恐惧的数学最...
    定静慧心阅读 159评论 0 1
  • 时间真是个奇怪的东西 他会把你想要的都带给你 也会将你舍不得的慢慢带走
    广电1702陈永凡阅读 258评论 0 0

友情链接更多精彩内容