Python learning notes (8) Python list (3)

sequence Sequence: mathematically, a sequence is an objec...

sequence

Sequence: mathematically, a sequence is an object (or event) that is arranged in a column; thus, each element is either before or after another element. The order between elements is very important here. Wikipedia

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

Python has six built-in types for sequences, but the most common are lists and tuples.

The operations that sequence can perform include indexing, slicing, adding, multiplying, and checking members.

Python has built-in methods for determining the length of a sequence and the maximum and minimum elements.

Comparison of list and string

1. List modifiable, string not modifiable

2. Strings and lists are sequences

3. List can be modified in place

4. List has multidimensional list, string does not

5. The string cannot be changed. The list can be modified in place

1 >>> a=[1,2,3] 2 >>> id(a) 3 46281096L 4 >>> a.append(4) #Append an element. If there is no return value, this is an external representation of in place modification 5 >>> a 6 [1, 2, 3, 4] 7 >>> id(a) #After appending, the position of the list in memory does not change 8 46281096L 9 >>> a[1]=9 #The list can also be modified in this way, changing the element with index position of 1 to 9, and the list modifiable features 10 >>> a 11 [1, 9, 3, 4] 12 >>> b="python" 13 >>> b[1]="w" #Error reporting in string, string does not support modification 14 Traceback (most recent call last): 15 File "<stdin>", line 1, in <module> 16 TypeError: 'str' object does not support item assignment23 >>> b[0]+"w"+b[2:] #If a business needs string modification, this can be done, but the modification actually generates another string, and the original string has not changed 24 'pwthon' 25 >>> b #The actual b string does not change 26 'python' 27 >>> a 28 [1, 9, 3, 4] #One-dimensional list 29 >>> m =[[1,2,3],[4,5,6],[7,8,9]] #Multidimensional list 30 >>> m[1] 31 [4, 5, 6] 32 >>> m[1][0] 33 4 34 >>>

Conversion between list() and str() list and string

1 >>> b="python" 2 >>> b 3 'python' 4 >>> list(b) #String to list 5 ['p', 'y', 't', 'h', 'o', 'n'] 6 >>> a ="www.baidu.com" 7 >>> a.split(".") #String is divided into a list by using the separator dot 8 ['www', 'baidu', 'com'] 9 >>> c =list(b) 10 >>> c 11 ['p', 'y', 't', 'h', 'o', 'n'] 12 >>> "".join(c) #Then join function is used to connect list c with null and convert it to string 13 'python' 14 >>> "-".join(c) #Join list c with a minus sign using the join function 15 'p-y-t-h-o-n'

5 April 2020, 02:11 | Views: 7239

Add new comment

For adding a comment, please log in
or create account

0 comments