Python list derivation and generator expression usage example, python Generator

Source: Internet
Author: User
Tags python list

Python list derivation and generator expression usage example, python Generator

This article describes the Python list derivation and generator expression usage. We will share this with you for your reference. The details are as follows:

Like the list,List DerivationIt is also represented by square brackets [] and uses a abbreviated for loop. The first part is an expression that generates the result list element, and the second part is a loop on an input expression. To read and understand the list expression, we recommend that you first check whether the if condition exists from the for loop, and then map the expression starting with the derivation to each matching element.

>>> even_numbers = [x for x in range(10) if x%2 == 0]>>> even_numbers[0,2,4,6,8]

The preceding example shows how to use the if statement to filter elements.

Python also supports a structure similar to a list expression calledGenerator expression(Generator expression), in addition to its features known as "inert computing", it is basically consistent with the usage of list derivation. It works in the following way:Each time an object is processed, rather than processing and constructing the entire data structure in one breath, the potential advantage of this operation is that it can save a lot of memory.

>>> even_numbers = (x for x in range(10000) if x%2 == 0)>>> even_numbers<generator object at 0x....>

When processing a large amount of data, it is best to consider generator expressions rather than list derivation.

Another list derivation and generator expression example is:

>>> data = ['abc','def','I use python', 'hong201']>>> sum([len(word) for word in data])25>>> sum(len(word) for word in data)25

enumerate()Is a built-in function that allows you to iterate and count at the same time, while the for Loop itself can only iterate but cannot count:

>>> data = [123,"abc",3.14]>>> for i in enumerate(data):...   print i...(0, 123)(1, 'abc')(2, 3.14)

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.