When to Use Generator in Python

Source: Internet
Author: User
Keywords python generator python generator use
Speaking of generator, let's talk about list comprehensions first. List parsing can create lists dynamically.

[expr for iter_var in iterable if cond_expr]

The core of the sentence is the for loop, which iterates through all the entries of the iterable object. If cond_expr (conditional expression) is satisfied, the previous expr (expression) is applied to the member. The final result is a list of expressions that satisfy the condition. For lambda, map(), filter(), etc. can be reduced to a list resolution through list resolution. First look at the role of these three functions:

map(lambda x:x**2, range(6))
>>>[0, 1, 4, 9, 16, 25]
lambda input parameters: output expressions allow the creation of a line of function objects without def definitions, simplifying code
map (operation, list of operations required) applies an operation to all list members

You can use list comprehension to simplify the above operations:

[x**2 for x in range(6)]
>>>[0, 1, 4, 9, 16, 25]
You can also use list comprehension to simplify the filter() operation, such as:

seq = [11, 10, 1, 9, 10, 2, 3, 44, 12, 11]
print filter(lambda x: x% 2, seq)
>>>[11, 1, 9, 3, 11]
You can use list comprehension to simplify the above operations:


print [x for x in seq if x% 2]
>>>[11, 1, 9, 3, 11]
The generator is an extension of list comprehension.
(Expr for iter_var in iterable if cond_expr)
It is very similar to a list parser, and the syntax is basically the same, but instead of actually creating a list, it generates a generator. This generator "yields" this item every time it calculates an item. The generator expression uses "lazy evaluation", so it uses memory more efficiently.


print (x for x in seq if x% 2)
>>><generator object <genexpr> at 0x00000000025B6AB0>
So when exactly do you use generators?

When creating a list is only an intermediate process, in order to avoid creating a huge list, we can use generator expressions to complete. For example, when we want to count the number of words in a txt text, we don't need to generate the list first and then count the words.


f = open('*.txt','r')
len([word for line in f for word in line.split()]) #Use list parsing, first generate list and then count
len(word for line in f for word in line.split()) #Use the generator to count the generators returned, no list is generated
All that is done is to remove the square brackets, which not only saves two bytes, but also saves memory.
This reminds me of using xrange() instead of range(). The reason is the same. xrange returns a generator, and range returns a list.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.