首页 > 其他 > 详细

Iterables, Iterators, and Generators

时间:2020-03-02 09:29:13      阅读:81      评论:0      收藏:0      [点我收藏+]

Iteration is fundamental to data processing. And when scanning datasets that don‘t fit in memory, we need a way to fetch the items lazily, that is, one at a time and on demand. This is what the Iterator pattern is about. 

Every generator is an iterator: generators fully implement the iterator interface. But an iterator retrieves items from a collection, while a generator can produce items "out of thin air".

Every collection in Python is iterable, and iterators are used internally to support:

  • for loop
  • Collection types construction and extension
  • Looping over text files line by line
  • List, dict, and set comprehensions
  • Tuple unpacking
  • Unpacking actual parameters with * in function calls

We‘ll get started studying how the iter(...) function makes sequence iterable.

 

1. Sentence Take #1: A Sequence of Words.

 

 

 

 

 

 

 

 

end ...

Iterables, Iterators, and Generators

原文:https://www.cnblogs.com/neozheng/p/12392754.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!