I want to learn more about Python and better understand the list-Python tutorial.

Source: Internet
Author: User
For list, because it is indeed very complex and widely used in python, although the basic content has been introduced, I would like to explain it a little more here, the deeper it is, the more... List parsing

Let's take a look at the following example. In this example, we want to get the square of each integer from 1 to 9 and put the result in the list to print it out.

>>> power2 = []>>> for i in range(1,10):...   power2.append(i*i)... >>> power2[1, 4, 9, 16, 25, 36, 49, 64, 81]

Python has a very interesting function, that is, list parsing, which is like this:

>>> squares = [x**2 for x in range(1,10)]>>> squares[1, 4, 9, 16, 25, 36, 49, 64, 81]

Are you surprised to see this result? This is python, and the pursuit of concise and elegant python!

The official documentation provides a description of list parsing:

List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.

Do you still remember the question mentioned above?

Returns a positive integer less than 100 that can be divisible by 3.
We use the following methods:

aliquot = []for n in range(1,100):  if n%3 == 0:    aliquot.append(n)print aliquot

Okay. If you use list to parse and rewrite the data, it will be like this:

>>> aliquot = [n for n in range(1,100) if n%3==0]>>> aliquot[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]

Shocked. Absolutely Awesome X!

In fact, it is not only a list composed of numbers, but can be operated in this way. After restoring your excitement, read the following code and find out the charm of list parsing.

>>> Mybag = ['glass ', 'apple', 'Greenleaf'] # Some have leading spaces and some have trailing spaces >>> [one. strip () for one in mybag] # Remove spaces before and after elements ['glass ', 'apple', 'Greenleaf'] enumerate

This is an interesting built-in function. we could have used for I in range (len (list) to get each element number of a list, then, you can use list [I] to obtain the element. What if I want to get the element numbers and elements at the same time? This is the case:

>>> For I in range (len (week )):... print week [I] + 'is' + str (I) # note that I is of the int type. if it is connected to the previous one with +, it must be of the str type... monday is 0 Sunday is 1 Friday is 2

Python provides a built-in function enumerate to implement similar functions.

>>> for (i,day) in enumerate(week):...   print day+' is '+str(i)... monday is 0sunday is 1friday is 2

It is an interesting built-in function. it mainly provides a simple and quick method.

This is what the official documentation says:

The code is as follows:


Return an enumerate object. sequence must be a sequence, an iterator, or some other object which supports iteration. the next () method of the iterator returned by enumerate () returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over sequence:


By the way, copy a few examples for the audience to enjoy. it is best to experiment.

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']>>> list(enumerate(seasons))[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]>>> list(enumerate(seasons, start=1))[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]

There is something like (0, 'Spring') here. this is another data type, which will be explained later.

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.