码迷,mamicode.com
首页 > 编程语言 > 详细

Python无限元素列表实例教程

时间:2014-08-19 07:07:43      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:http   使用   for   ar   代码   ef   on   python   

有关Python中无限元素列表的实现方法。

本文实例讲述了Python怎么实现无限元素列表的方法,具体实现可使用Yield来完成。
下面所述的2段实例代码通过Python Yield 生成器实现了简单的无限元素列表。(www.jbxue.com)
1.递增无限列表
具体代码:
def increment():
i = 0
while True:
yield i
i += 1

for j in increment():
print i
if (j > 10) : break

2.斐波那契无限列表
具体代码:
def fibonacci():
i = j = 1
while True:
result, i, j = i, j, i + j
yield result

for k in fibonacci():
print k
if (k > 100) : break

Python无限元素列表实例教程,布布扣,bubuko.com

Python无限元素列表实例教程

标签:http   使用   for   ar   代码   ef   on   python   

原文地址:http://www.cnblogs.com/cfinder010/p/3921168.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!