程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python foundation 12 (iterator, generator, calculation of Fibonacci sequence)

編輯:Python

Iterators and generators

Iteratable object Iterable

  • str tuple list dict set These can act directly on for Circular objects are collectively called iteratable objects
 from collections.abc import Iterable
list1 = [1, 2, 3]
print(isinstance(list1, Iterable))
isinstance Determine whether it is an iteratable object

iterator (Iterator)

Can be next() The object that function calls and returns the next value continuously is called an iterator

  • hold list、dict、str etc. Iterable become Iterator have access to iter() function
  • Lazily generate the next value
 s = "123456"
s = iter(s)
print(next(s))
print(next(s))
print(next(s))
print(next(s))
print(next(s))
print(next(s))

An error will be reported when the range is exceeded StopIteration

 Determine whether it is an iterator
from collections.abc import Iterable, Iterator
s = "123456"
s = iter(s)
print(isinstance(s, Iterable))

When the range is exceeded, the following methods can be used to prevent error reporting

 s = "123456"
s = iter(s)
while 1:
try:
print(next(s))
except StopIteration:
print(" End of the iteration !")
break

generator (Generator)

Generator Expressions

List derivation List generator

 l = [i for i in range(100)]
for i in range(100):
l.append(i)
print(l)
l = (i for i in range(1000000000000))
print(l)
print(next(l))
print(next(l))
print(next(l))
print(next(l))
for i in range(101):
print(next(l))

Generator function keyword yield

  • stay Python in , Used yield The function of the is called the generator (generator).
  • Different from ordinary functions , A generator is a function that returns an iterator , Can only be used for iterative operations , It's easier to understand that a generator is an iterator .
  • During the call generator run , Every encounter yield Function will pause and save all current running information , return yield Value , And next time next() Method to continue from the current location .

When there is yield When a keyword This function returns a generator , Can only be used for iterative operations . use next() In the process of calling the generator , Every encounter yield Keyword will pause the function and keep the running information of the current function , return yield Value , And continue to execute downward from the current position at the next execution

 def my_generator():
for i in range(1, 6):
print(f" The first {
i} Execution times yield Before ")
yield i
print(f" The first {
i} Execution times yield after ")
g = my_generator()
print(next(g))
print(next(g))
print(next(g))
print(next(g))
print(next(g))
print(next(g))

Fibonacci sequence

Two values are known 0 1 Each subsequent digit is equal to the sum of the first two terms

0 1 1 2 3 5 8 13 21

 Simple calculation method
def ficonacci():
a, b = 0, 1
while 1:
yield a
a, b = b, a+b
f = ficonacci()
for i in range(1000):
print(next(f))

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved