承接国内外服务器租用托管、定制开发、网站代运营、网站seo优化托管接单、网站代更新,新老站点皆可!!咨询QQ:3787320601
当前位置:首页  >  互联网圈  >  python如何写迭代函数

python如何写迭代函数

管理员 2023-03-27 09:52:18 互联网圈 35 ℃ 0 评论 737字 收藏

python如何写迭代函数

python怎么写迭代函数

在python中书写迭代函数的方法

1.next函数

# 首先取得Iterator对象:

it = iter([1, 2, 3, 4, 5])

# 循环:

while True:

try:

# 取得下一个值:

x = next(it)

print(x)

except StopIteration:

# 遇到StopIteration就退出循环

break

输出结果为:

1

2

3

4

5

2.iter函数

>>>lst = [1, 2, 3]

>>> for i in iter(lst):

... print(i)

...

1

2

3

文章来源:丸子建站

文章标题:python如何写迭代函数

https://www.wanzijz.com/view/4417.html

相关文章

Related articles

X

截屏,微信识别二维码

微信号:weimawl

(点击微信号复制,添加好友)

打开微信