[Python] A rough understanding of iterators, iterators, and generators

I want to master Iterable, Iterator, and Generator, but honestly I can only understand it somehow, so I summarized it briefly so that it is easy to imagine.

Iterable

An object that can be used repeatedly. Objects of the list, dict, set and str classes are iterable. Simply put, what you can use with *** for syntax ***

Iterator

Iterators are a type of iterator (iterators are not necessarily iterators) Simply put, it's like a *** list *** returns the current element with next () and advances to the next element Simply put *** Take out something like a list until it's empty ***

Generator

If there is a [yield return value] in the function, that function is called a generator function. Also, the list comprehension that is changed from square brackets to parentheses is called a generator expression. Example *** (i for i in range (N)) *** The return value of that function or expression is called a generator iterator. Simply put, it is *** that returns an iterator ***.

Image diagram スクリーンショット 2020-05-28 14.47.39.png

Generator features


def sample():
    cumsum = 0
    for i in range(1, 5):
        cumsum += i
        print(f'First here →{cumsum}')
        yield cumsum

for cumsum in generator_sample2(): #point
    print(f'Yield minutes →{cumsum}')

*** *** First here → 1 Yield minutes → 1 First here → 3 Yield minutes → 3 First here → 6 Yield minutes → 6 First here → 10 Yield minutes → 10

Iterator features 1


sample_list = [i for i in range(1, 6)]
print(f'The contents of the list{sample_list}')

sample_iter = iter(sample_list) #iter()Make it an iterator

for i in sample_iter:
    print(i)
    
print(f'The contents of the list do not change{sample_list}')
print(f'Iterator becomes empty when used{list(sample_iter)}')

*** *** Contents of the list [1, 2, 3, 4, 5] 1 2 3 4 5 The contents of the list do not change [1, 2, 3, 4, 5] Iterators are empty when used []

Iterator feature 2


sample_list = [i for i in range(1, 6)]
print(f'The contents of the list{sample_list}')

sample_iter = iter(sample_list) #iter()Make it an iterator
print(f'Something like a list{sample_iter}') #I'm using it here
print(f'Can be seen as a list{list(sample_iter)}')

print(f'Iterator becomes empty when used{list(sample_iter)}')

*** *** Contents of the list [1, 2, 3, 4, 5] Something like a list <list_iterator object at 0x7fbf11e34b80> Seen as a list [1, 2, 3, 4, 5] Iterators are empty when used []

*** *** [About Iterable](https://python.ms/iterable/#_1-%E5%85%B7%E4%BD%93%E7%9A%84%E3%81%AB%E8%A8%80%E3 % 81% 88% E3% 81% B0) [About Iterator](https://python.ms/iterator/#_1-%E3%82%A4%E3%83%86%E3%83%AC%E3%83%BC%E3%82%BF%E3 % 82% 92% E8% A7% A6% E3% 81% A3% E3% 81% A6% E3% 81% BF% E3% 82% 8B% E3% 80% 82) About generators


** Addition **

@shiracamus Thank you for pointing out.

Recommended Posts

[Python] A rough understanding of iterators, iterators, and generators
Python iterators and generators
A rough understanding of python-fire and a memo
[Python] A rough understanding of the logging module
Connect a lot of Python or and and
Full understanding of Python threading and multiprocessing
A discussion of the strengths and weaknesses of Python
A quick comparison of Python and node.js test libraries
Generate Fibonacci numbers with Python closures, iterators, and generators
Get an abstract understanding of Python modules and packages
Python list comprehensions and generators
Source installation and installation of Python
Full understanding of Python debugging
Let's review the language specifications around Python iterators and generators
Detect objects of a specific color and size with Python
A rough summary of the differences between Windows and Linux
Python: Create a dictionary from a list of keys and values
Python a + = b and a = a + b are different
A record of patching a python package
Environment construction of python and opencv
The story of Python and the story of NaN
A good description of Python decorators
Installation of SciPy and matplotlib (Python)
Python and DB: Understanding DBI cursors
[Python] A memorandum of beautiful soup4
A rough summary of OS history
[Python] return A [or / and] B
Coexistence of Python2 and 3 with CircleCI (1.0)
Summary of Python indexes and slices
Reputation of Python books and reference books
A memorandum of understanding for the Python package management tool ez_setup
Build and test a CI environment for multiple versions of Python
Installation of Visual studio code and installation of python
Calculate the shortest route of a graph with Dijkstra's algorithm and Python
A complete understanding of Python's asynchronous programming
Display a list of alphabets in Python 3
Extraction of tweet.js (json.loads and eval) (Python)
Make a relation diagram of Python module
A little scrutiny of pandas 1.0 and dask
A memo with Python2.7 and Python3 on CentOS
A memorandum of understanding about django's QueryDict
A complete understanding of Python's object-oriented programming
[python] Get a list of instance variables
[python] [meta] Is the type of python a type?
A story about Python pop and append
Easy introduction of python3 series and OpenCV3
[Python] Various combinations of strings and values
The story of blackjack A processing (python)
[Python] Get a list of folders only
Idempotent automation of Python and PyPI setup
Project Euler # 1 "Multiples of 3 and 5" in Python
A memorandum of python string deletion process
[Python] Create a list of date and time (datetime type) for a certain period
Build a python environment to learn the theory and implementation of deep learning
[Python] A program that calculates the number of updates of the highest and lowest records
Get the stock price of a Japanese company with Python and make a graph
A memo of a tutorial on running python on heroku
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
Draw a graph of a quadratic function in Python
Correspondence summary of array operation of ruby and python
Summary of the differences between PHP and Python