国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python的迭代器和生成器使用實例

2019-11-25 18:02:26
字體:
來源:轉載
供稿:網友

一、迭代器Iterators

迭代器僅是一容器對象,它實現了迭代器協議。它有兩個基本方法:

1)next方法
返回容器的下一個元素

2)__iter__方法
返回迭代器自身

迭代器可使用內建的iter方法創建,見例子:

復制代碼 代碼如下:

>>> i = iter('abc')
>>> i.next()
'a'
>>> i.next()
'b'
>>> i.next()
'c'
>>> i.next()
Traceback (most recent call last):
  File "<string>", line 1, in <string>
StopIteration:

class MyIterator(object):
  def __init__(self, step):
  self.step = step
  def next(self):
  """Returns the next element."""
  if self.step==0:
  raise StopIteration
  self.step-=1
  return self.step
  def __iter__(self):
  """Returns the iterator itself."""
  return self
for el in MyIterator(4):
  print el
--------------------


結果:
復制代碼 代碼如下:

3
2
1
0

二、生成器Generators

從Python2.2起,生成器提供了一種簡潔的方式幫助返回列表元素的函數來完成簡單和有效的代碼。
它基于yield指令,允許停止函數并立即返回結果。

此函數保存其執行上下文,如果需要,可立即繼續執行。

例如Fibonacci函數:

復制代碼 代碼如下:

def fibonacci():
  a,b=0,1
  while True:
  yield b
  a,b = b, a+b
fib=fibonacci()
print fib.next()
print fib.next()
print fib.next()
print [fib.next() for i in range(10)]
--------------------

結果:
復制代碼 代碼如下:

1
1
2
[3, 5, 8, 13, 21, 34, 55, 89, 144, 233]

PEP Python Enhancement Proposal Python增強建議

tokenize模塊

復制代碼 代碼如下:

>>> import tokenize
>>> reader = open('c:/temp/py1.py').next
>>> tokens=tokenize.generate_tokens(reader)
>>> tokens.next()
(1, 'class', (1, 0), (1, 5), 'class MyIterator(object):/n')
>>> tokens.next()
(1, 'MyIterator', (1, 6), (1, 16), 'class MyIterator(object):/n')
>>> tokens.next()
(51, '(', (1, 16), (1, 17), 'class MyIterator(object):/n')

例子:
復制代碼 代碼如下:

def power(values):
  for value in values:
  print 'powering %s' %value
  yield value
def adder(values):
  for value in values:
  print 'adding to %s' %value
  if value%2==0:
  yield value+3
  else:
  yield value+2
elements = [1,4,7,9,12,19]
res = adder(power(elements))
print res.next()
print res.next()
--------------------

結果:
復制代碼 代碼如下:

powering 1
adding to 1
3
powering 4
adding to 4
7

保持代碼簡單,而不是數據。
注意:寧可有大量簡單的可迭代函數,也不要一個復雜的一次只計算出一個值的函數。

例子:

復制代碼 代碼如下:

def psychologist():
  print 'Please tell me your problems'
  while True:
  answer = (yield)
  if answer is not None:
  if answer.endswith('?'):
  print ("Don't ask yourself too much questions")
  elif 'good' in answer:
  print "A that's good, go on"
  elif 'bad' in answer:
  print "Don't be so negative"
free = psychologist()
print free.next()
print free.send('I feel bad')
print free.send("Why I shouldn't ?")
print free.send("ok then i should find what is good for me")
--------------------

結果:
復制代碼 代碼如下:

Please tell me your problems
None
Don't be so negative
None
Don't ask yourself too much questions
None
A that's good, go on
None

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大厂| 乌兰县| 沈丘县| 杭锦后旗| 安达市| 博罗县| 永平县| 安图县| 临邑县| 宣恩县| 长乐市| 赣榆县| 莱西市| 内乡县| 大名县| 广平县| 尖扎县| 藁城市| 陇西县| 阳原县| 化州市| 武乡县| 平安县| 四子王旗| 城固县| 遵化市| 孝义市| 固阳县| 略阳县| 金堂县| 兴宁市| 和龙市| 青岛市| 鄂托克旗| 靖远县| 治县。| 汕尾市| 长岛县| 衢州市| 清水河县| 宁武县|