How to quickly build a Generator for python
This article will explain in detail how to quickly build a generator for python. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
1. Surround the data generation rules in parentheses to create an object of generator type.
A = (x for x in range (10)) print (type (a)) # run result: #
2. Function + yield keyword creation method. You need to write it as a multiline function and return the generator object by calling the function.
The generation rules that you want to represent are complex and cannot be represented by a single line of code.
Def fib (): n = 0a while b = 0je 1m n