Example of a list loop statement usage in Python

Source: Internet
Author: User
This example describes the use of the list loop statement in Python. Share to everyone for your reference. The specific usage analysis is as follows:

One of the great features of Python is its parsing of lists, which provides a compact way to map a list to another list by applying a function to each element in the list.
Instance
Copy the Code code as follows:

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

Print a

Operation Result:
Copy the Code code as follows:


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



To manipulate according to the length of the array:
Copy CodeThe code is as follows:

A = [' Mary ', ' had ', ' a ', ' little ', ' lamb ']
For I in range (Len (a)):
Print I, A[i]


Operation Result:
Copy CodeThe code is as follows:


0 Mary
1 had
2 A
3 Little
4 Lamb

Copy the Code code as follows:

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


Operation Result:
Copy CodeThe code is as follows:


A
B
C
D
E

List Parsing Introduction:
Copy the Code code 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]

For the sake of understanding it, let's look from right to left. Li is a list that will be mapped. Python loops through each element in the LI. Do the following for each element: first temporarily assigns its value to the variable Elem, then Python applies the function elem*2 for the calculation, and finally appends the result to the list to be returned.

It is important to note that parsing the list does not change the original list.

It is safe to assign the parse result of a list to the variable to which it is mapped. Don't worry about a competitive situation or any weird things happening. Python creates a new list in memory, and when parsing the list is complete, Python assigns the result to the variable.

Hopefully this article will help you with Python programming.

  • 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.