Example of list loop statement usage in python

Source: Internet
Author: User
This article mainly introduces the usage of list loop statements in python, and describes in detail the list parsing of Python in the form of examples, including various common traversal operations and principle analysis, for more information about the list statement usage in python, see the following example. Share it with you for your reference. The usage analysis is as follows:

One of the powerful features of Python is its list parsing. It provides a compact method that maps a list to another by applying a function to each element in the list.
Instance

The code is as follows:

A = ['cat', 'window', 'deletestrate']
For x in:
Print x, len (x)
For x in [1, 2, 3]: print x, # iteration Loop through a list: for in
A = ['cat', 'window', 'deletestrate']
For x in a [:]: # make a slice copy of the entire list
If len (x)> 6: a. insert (0, x)

Print

Running result:

The code is as follows:


Cat 3
Window 6
Defenestrate 12
1 2 3 ['deletestrate', 'cat', 'window', 'deletestrate']



Operate based on the array length:

The code is as follows:

A = ['Mary ', 'had', 'A', 'Little', 'lamb']
For I in range (len ()):
Print I, a [I]


Running result:

The code is as follows:


0 Mary
1 had
2
3 little
4 lamb

The code is as follows:

Words = ['A', 'B', 'C', 'D', 'E']
For word in words:
Print word


Running result:

The code is as follows:


A
B
C
D
E

List parsing:

The code is as follows:

>>> Li = [1, 9, 8, 4]
>>> [Elem * 2 for elem in li]
[2, 18, 16, 8]
>>> Li
[1, 9, 8, 4]
>>> Li = [elem * 2 for elem in li]
>>> Li
[2, 18, 16, 8]

To make it easier to understand, let's look at it from the right to the left. Li is a list to be mapped. Python cyclically traverses every element in li. Perform the following operations on each element: First temporarily assign the value to the variable elem, then use the Python application function elem * 2 for calculation, and then append the calculation result to the list to be returned.

Note that the parsing of the list does not change the original list.

It is safe to assign the resolution result of a list to the variables mapped to it. Don't worry about competition or any strange things. Python creates a new list in the memory. when the list is parsed, Python assigns the result to the variable.

I hope this article will help you with Python programming.

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.