[Python] -- list, python -- list

Source: Internet
Author: User

[Python] -- list, python -- list

The most basic data structure in Python. Each element in the sequence is assigned a number-its location, or index. The first index is 0, the second index is 1, and so on.

List 1. Define the list and retrieve the values in the list
1 names = [] # define an empty list 2 names = ['A', 'B ', 'C'] # define a non-empty list 3 4 # retrieve the value 5 6> names = ['A', 'B ', 'C'] 7 >>> names [0] 8'a' 9 >>> names [1] 10' B '11 >>> names [2] 12'c' 13 >>> names [-1] # returns the last value of 14 'C'
2. Slice
1 >>> names = ['A', 'B', 'C ', 'D'] # the value of the List is from 0. 2> names [] # elements between 1 and 3, including 1, not including 3 3 ['B', 'C'] 4 >>> names [1:-1] # Take the elements from 1 to-1, including 1, -1 5 ['B', 'C'] 6 >>> names [0: 3] 7 ['A', 'B ', 'C'] 8 >>> names [: 3] # From the beginning, 0 can be omitted, and the effect is equivalent to names [0: 3] 9 ['A', 'B ', 'C'] 10 >>> names [3:] # To get the last value, you must not write-1, you can only write 11 ['D'] 12 >>> names [0: 2] #2: Each element takes a 13 ['A ', 'C'] 14 >>> names [: 2] # can be omitted from scratch 0. The effect is the same as that in the previous sentence 15 ['A', 'C']

Slice summary:

① The sequence is always sliced from left to right, not from right to left.

① When the list is sliced, the element of the start position is included, and the element of the marker position is not included (also called Gu head regardless of the end). The last position indicates the step size (names [start bit: marker bit: step Size])

② If the value ranges from 0, 0 can be omitted.

③ When you want to get the last value, the delimiter cannot be-1. Because the delimiter element is not included, it can only be left blank.

3. List Functions & Methods

Function:

Method:

Demo:

1 # Add a new object to the end of the append List 2> names = ['A', 'B', 'C', 'D'] 3> names. append ('E') 4 >>> names 5 ['A', 'B', 'C', 'D ', 'E'] # e is the newly added element 6 7 # insert (TAG value, inserted content) 8 >>> names = ['A', 'B ', 'C', 'D'] 9 >>> names. insert (0, '1') #0 indicates the value of the tag to be inserted, and '1' indicates the inserted content 10 >>> names11 ['1', 'A ', 'B', 'C', 'D'] # insert '1' 12 13 where the following value is 0 # modify the elements in the list, directly names [lower value] = new value 14 >>> names = ['A', 'B', 'C ', 'D '] 15 >>> names [1] = '1' 16 >>> names17 ['A', '1', 'C ', 'D'] 18 19 20 # The list of extended (extend) names2 is merged into names1. However, the list of names2 still exists. If you want to delete the variable names2, you only need del names2 to get 21 >>> names1 = ['A', 'B', 'C', 'D'] 22 >>> names2 = [1, 2, 3, 4] 23 >>> names1.extend (names2) 24 >>> names125 ['A', 'B', 'C', 'D', 1, 2, 3, 4] 26 27 # statistics (count (element) 28 >>> names = ['A', 'B', 'C', 'D ', 'A'] 29 >>> names. count ('A') # count the number of 'A' elements 30 231 32 # Flip (reverse () 33 >>> names = ['A', 'B ', 'C', 'D'] 34 >>> names. reverse () 35 >>> names36 ['D', 'C', 'B', 'a'] # flip the entire list over 37 38 # sort ()) 39 >>> names = [,] 40 >>> names. sort () 41 >>> names42 [1, 2, 3, 4] 43 44 # obtain the base value (index (element) 45 >>> names = ['A ', 'B', 'C', 'D'] 46 >>> names. index ('A') 47 048 49 # clear list (clear () 50 >>> names = ['A', 'B', 'C ', 'D '] 51 >>> names. clear () 52 >>> names53 []

Delete (del, remove (element), pop ())

# Delete an element >>> names = ['A', 'B', 'C ', 'D'] >>> del names [0] >>> names ['B', 'C ', 'D'] # Delete by element >>> names = ['A', 'B', 'C', 'D'] >>> names. remove ('A') >>> names ['B', 'C', 'D'] # Delete the last one >>> names = ['A ', 'B', 'C', 'D']> names. pop () 'D'> names ['A', 'B', 'C']

Extension:

# If pop () has a tag value, it deletes an element. The effect is the same as that of del >>> names = ['A', 'B ', 'C', 'D'] >>> names. pop (1) # 'B' is the same as del when the input subscript is worthwhile> names ['A', 'C ', 'D'] # The del keyword can delete not only the elements in the list, but also the variables names = ['A', 'B', 'C ', 'D'] # delete the names variable del names

Copy:

1 >>> names = ['a','b','c','d']2 >>> names2 = names.copy()3 >>> names24 ['a', 'b', 'c', 'd']

Note: All copies here are light copies, and only the first layer can be copied. For more information, click here.

 

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.