Python Basics list common operations and knowledge points summary

Source: Internet
Author: User

Listing (list)

The list is the most frequently used data type in Python. A list can accomplish the data structure implementation of most collection classes. It supports characters, numbers, and even strings that can contain lists (so-called nesting). The list is identified by [], which is the most common type of composite data for Python.


Common operations

l=[1,2,3, ' Xyn ', 5]

Code

Meaning

Output

L.append (' a ')

Append an element to the L list

[Xyn, ' 5,1 ', ' a ']

L.insert (0, ' a ')

Insert ' A ' in the position of l[0]

[' A ', ' Xyn ', 5,1]

L.pop (0)

Delete the l[0] position of the element, and output L[0]

[2,3, ' Xyn ', 5,1]

L.remove (1)

Remove the first occurrence of this element in L

[2,3, ' Xyn ', 5,1]

L.count (1)

The number of occurrences of this element 1 in the list

2

L.index (5)

The position of the element 5, without throwing an exception

4

L.extend (list)

Append list, merge elements of list to L

[Xyn, ' 5,1 ', ' A ', ' B ', ' C ']

A=[5,4,8,10,0,88]

A.sort ()

Print a

Sort

[0, 4, 5, 8, 10, 88]

L.reverse ()

Reverse

[88, 0, 10, 8, 4, 5]

String cutting and List compositing


' Tuotatianwang,taibaijinxing,juanliandajiang ' # string manager with ', ' cut, and exist manager_list list of Manager_list=manager.split (', ')# Output cut complete list  manager_list# Use the Join function to turn manager_list into a string again and use spaces to connect elements together new_manager='. Join (Manager_ List)New_manager

Output result: [' Tuotatianwang ', ' taibaijinxing ', ' Juanliandajiang ']
Tuotatianwang taibaijinxing Juanliandajiang

The list is similar to a string, and here is the list quiz, where three items are wrong:

A. For list = [' A ', ' B ', ' C ', ' d ', list[-2:] and list[2:] should have the same result valueB. For list = [' A ', ' B ', ' C ', ' d '], after List.insert (3, ' X '), the value of list is [' A ', ' B ', ' C ', ' X ' , ' d ']C. For list = [' A ', ' B ', ' C ', ' d '], print List.pop (3) will get the output result D d. For list = [' A ', ' B ', ' C ', ' D '], print list[2:] will get output [' B ', ' C ', ' d '] e. The string ' A B C ' is cut by a space and the output can be written as print ' a B C '. Split () F. For list = [' A ', ' B ', ' C ', ' d '], print List.pop (3) will get output [' A ', ' B ', ' C '] g. Synthesis of list = [' A ', ' B ', ' C '] into string ' a|b|c ' and output can be written as print list.join (' | ')




********************************************************************************************************** *********************************************, I'm a split line, don't peek ********************************************* .*********************************************************************************************************** **


d [' C ', ' d ']

F D

G-Correct expression

list = [' A ''B ''c'd']new_list=' | '. Join (list)new_list

can ~ ~ Please a lot of advice more attention ~ Bye-bye Branch ~

Python Basics list common operations and knowledge points summary

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.