使用生成器创建新的迭代模式
生活随笔
收集整理的這篇文章主要介紹了
使用生成器创建新的迭代模式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一個函數中需要有一個 yield 語句即可將其轉換為一個生成器。 def frange(start, stop, increment):x = startwhile x < stop:yield xx += incrementfor i in frange(0, 4, 2):print(i) # 0 2 一個生成器函數主要特征是它只會回應在迭代中使用到的 next 操作 def cutdata(n):print("start",n)while n > 0:yield nn-=1print("Done")res=cutdata(3)
next(res)
next(res)
next(res)
"""
start 3
Done
Done"""
?
轉載于:https://www.cnblogs.com/zzy-9318/p/10484946.html
總結
以上是生活随笔為你收集整理的使用生成器创建新的迭代模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Diamond3.5软件的使用--(2)
- 下一篇: Codeforces 924D Cont