"Python Notes" Lesson II (ii): List

Source: Internet
Author: User

Description

The list in Python is similar to an array in other high-level languages, but Python's list is much easier to manipulate.

The main focus of the list in Python is on the operation of the list parameters, focusing on the following:

Names.append (Names.count (Names.extend (Names.index (Names.insert (Names.pop (Names.remove) (Names.reverse (Names.sort (

The following is basically a description of the above operation.



1. Basic operation

• The basic operation is as follows:

Names[num]: The element sorted as number num NAMES[-1:]: Last 1 elements names[-5:]: Last 5 elements names[-5:-1]: last 5 elements, but excluding last 1 elements (i.e. excluding names[-1]) Names[:5] : Starting 5 elements (i.e. excluding names[5]) Names[0:5]: Starting 5 elements (i.e. excluding names[5]) names[10:15]:names[10] to names[14]

• The above has the scope of the case, the front and back two digits, can be recorded as: "Out of the box", that is, after not taken before

· Do a simple test in the following environment:

>>> names = [' Xpleaf ', ' Yonghaoyip ', ' CL ']>>> names.extend (range) >>> names[' xpleaf ', ' Yonghaoyip ', ' CL ', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 2 8, 29]

(1) Names[num]: An element sorted as a number num

>>> names[2] ' CL '

(2) Names[-1:]: Last 1 elements

>>> names[-1:][29]

(3) Names[-5:]: last 5 elements

>>> names[-5:][25, 26, 27, 28, 29]

(4) Names[-5:-1]: The last 5 elements, but excluding the last 1 elements (i.e. excluding names[-1])

>>> names[-5:-1][25, 26, 27, 28]

(5) Names[:5] or Names[0:5]: start 5 elements (i.e. excluding names[5])

>>> names[:5][' xpleaf ', ' Yonghaoyip ', ' cl ', 0, 1]>>> names[0:5][' xpleaf ', ' Yonghaoyip ', ' cl ', 0, 1]

(6) names[10:15]:names[10] to names[14]

>>> Names[10:15][7, 8, 9, ten, 11]>>> names[10]7>>> names[14]11>>> names[15]12



2.append () parameter


• Basic features: Adding elements to the list

• Basic Syntax:

Names.append (' Xpleaf ')

The append () parameter adds an element to the last face of the list and can only add one at a time;

• The demo is as follows:

>>> names.append (' xpleaf ') >>> names[' xpleaf ', ' Yonghaoyip ', ' CL ', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ------------------------------------



3.count () parameter


• Basic function: Count the number of identical elements in the list

• Basic Syntax:

Names.count (' Xpleaf ')

• The demo is as follows:

>>> names[' xpleaf ', ' Yonghaoyip ', ' CL ', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, (+), (+), (+), (+), +,-2, ' Xpleaf ']>>> names.count (' xpleaf ')



4.extend () parameter


• Basic functionality: Merge elements from another list into the current list

• Basic Syntax:

Names.extend (range (10)) is equivalent to the following: Names = names + range (Ten) ===> list can also be added directly, the characteristics of the sequence in Python

• The demo is as follows:

>>> names[' xpleaf ',  ' Yonghaoyip ',  ' CL ',  0,  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,  14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,  26, 27, 28, 29,  ' Xpleaf ']>>> range (Ten)     ===> The output of range () is already a list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>>  names.extend (range) >>> names[' xpleaf ',  ' Yonghaoyip ',  ' CL ',  0, 1,  2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,  15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,  27, 28, 29,  ' Xpleaf ',  0, 1, 2, 3, 4, 5, 6, 7,  8, 9] 

append () adds the elements in the list, if written as follows:

Names.append (range) ===> only adds a list element at the end of the names list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]



5.index () parameter


• Basic function: Find the index number of the list element you want to find

• Basic Syntax:

Names.index (' Xpleaf ')

• If there are multiple identical elements in the list, index () finds only the 1th number of the same element

• The demo is as follows:

>>> names.append (' cl ') >>> names[' xpleaf ', ' Yonghaoyip ', ' CL ', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, All, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ' CL ']>>&, +, (+), (+), (+), (+), (+) Gt Names.index (' xpleaf ') 0>>> names.index (' cl ') 2>>> names[2] ' cl '



6.inset () parameter


• Basic function: Insert an element in a position in a list

• Basic Syntax:

Names.insert (6, ' love ') ===> inserts the element ' love ' in the position where the index number of the list is 6

• The demo is as follows:

>>> names[' xpleaf ',  ' Yonghaoyip ',  ' CL ',  0,  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,  14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,  26, 27, 28, 29,  ' Xpleaf ',  0, 1, 2, 3, 4, 5, 6,  7, 8, 9,  ' CL ']>>> names.insert (6, ' love ') >>> names[' xpleaf ',   ' Yonghaoyip ',  ' CL ', 0, 1, 2,  ' love ',  3, 4, 5, 6, 7,  8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,  20, 21, 22, 23, 24, 25, 26, 27, 28, 29,  ' Xpleaf ',  0, 1, 2, 3, 4, 5, 6, 7, 8, 9,  ' CL ']>>>  Names[6] ' love ' 


--Small integrated operation: replace ' xpleaf ' in the above list with ' Xpleaf_yip '

• Clumsy methods that do not take full advantage of Python features:

>>> while True: ... if Names.count (' xpleaf ') = = 0: ... else: ... names[names.index (' xpleaf ')] = ' Xpleaf_yip ' ... >>> names[' Xpleaf_yip ', ' Yonghaoyip ', ' CL ', 0, 1, 2, ' Love ', 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 , (+), 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ' CL ', (+), (+), (+), (+), ' Xpleaf_yip ', +-

• Although the goal can be achieved, the method is actually rather stupid;


• Ways to take advantage of Python features:

>>> for I in range (Names.count (' Xpleaf ')): ... names[names.index (' xpleaf ')] = ' Xpleaf_yip ' ... >>> name s[' Xpleaf_yip ', ' Yonghaoyip ', ' CL ', 0, 1, 2, ' Love ', 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, , at, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ' CL ', and ' Xpleaf_yip ',.



< not finished,continue>






This article is from the "fragrant fluttering leaves" blog, please make sure to keep this source http://xpleaf.blog.51cto.com/9315560/1692957

"Python Notes" Lesson II (ii): List

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.