List of [python Basics] And pythonlist
Python provides a data type called list, which can store an ordered set of elements.
Remember: A list can store data sets of any size. The list is a mutable object, which is different from the str class. The str class is an immutable object.
1. Create a list
List1 = list () # create an empty list list2 = list ([2, 3, 4]) # create a list that contains elements 2, 3, and 4list3 = list (["red ", "green"]) # create a string list list4 = list (range (3, 6) list5 = list ("abcd ")
We usually omit the brackets:
list1 = list[]list2 = list[2,3,4]
2. list common operations in python, the strings and lists are sequential types. A string is a string sequence, and a list is a sequence of any elements.
Common Operations of sequence (list) s
| Operation |
Description |
| X in s |
Returns true if element x is in series s. |
| X not in s |
Returns true if element x is not in s. |
| S1 + s2 |
Connect two sequences s1 and s2 |
| S * n, n * s |
N sequence s connections |
| S [I] |
I element of sequence s |
| S [I, j] |
Fragment of sequence s from subscript I to J-1 (list truncation) |
| Len (s) |
The length of series s, that is, the number of elements in s. |
| Min (s) |
Minimum element of sequence s |
| Max (s) |
Maximum element of sequence s |
| Sum (s) |
Sum of all elements in sequence s |
| For loop |
In a for loop, the elements are reversed from left to right. |
| <, <=,>, >=, = ,! = |
Compares two sequences. true is returned if it is true. |
| Random. shuffle (s) |
Arrange elements in sequence s at will |
3. if the subscript operator [] has a list mylist, the index in mylist [index] is the subscript of the list. Generally, the range of the lower mark is 0 to len (mylist)-1. mylist [index] is also called a subscript variable. Mylist [0], mylist [1]…… Access the list of 0th elements, the first element ...... Index can also be negative: mylist [-1], mylist [-2]… The first and second elements in the access list respectively ...... (If it is a negative number, the maximum value of the index is-1.) 4. The range of [start: end] mylst [start: end] to be intercepted is from start to end-1, forming a list.
>>>mylst= [0,1,2,3,4,5]>>>mylst[2,4][2, 3]
If start and end are omitted, start is 0 by default, and end is the last subscript of the list by default. start and end can also be negative, if the negative number is-n, replace it with-n + len (mylist ).
>>>mylist[:3][0, 1, 2]>>>mylist[2:][2, 3, 4, 5]>>>mylist[:][0, 1, 2, 3, 4, 5]>>>mylist[-4:-2][2, 3]
If start> end, mylist creates an empty list.
>>> Mylist [3: 2] # start> end, the error Traceback (most recent call last): File "<pyshell #1>", line 1, in <module> mylist [3: 2] NameError: name 'mylist' is not defined
5. List resolution list resolution consists of multiple square brackets, which contain an expression followed by a for clause, followed by 0 or more for or if clauses. For example:
>>>list1 = [x for x in range(5)]>>>list1[0, 1, 2, 3, 4]>>>list2 = [0.5*x for x in list1]>>>list2[0.0, 0.5, 1.0, 1.5, 2.0]>>>list3 = [x for x in list2 if x < 1.5]>>>list3[0.0, 0.5, 1.0]
6. List Method
Common list Methods
| Append (x: object): None |
Add the element to the end of the list |
| Count (x: object): int |
Returns the number of times element x appears in the list. |
| Extend (lst: list): None |
Append all elements in list l to list |
| Index (x: object): int |
Returns the subscript of x that appears for the first time in the list. |
| Insert (index: int, x: object): None |
Insert element x to the specified subscript in the list. |
| Pop (I): object |
Delete the element at the given position and return it. Parameter I (Optional). If not specified, the last element in the list is deleted and returned. |
Remove (x: object ): None |
Delete the first x in the list |
| Reverse (): None |
Reverse all elements in the list (not sorted) |
| Sort (): None |
Sort the elements in the list in ascending order (Note: It is sort) |
The above code is as follows:
>>> List1 = [2, 3, 4, 1, 32, 4] >>> list1.append (19) >>> list1 [2, 3, 4, 1, 32, 4, 19] >>> list1.count (4) 2 >>> list2 = [99, 54] >>> list2.extend (list1) >>> list2 [99, 54, 2, 3, 4, 1, 32, 4, 19] >>> list2.index (4) 4 >>> list2.insert (1, 25) >>> list2 [99, 25, 54, 2, 3, 4, 1, 32, 4, 19] >>> list2.pop () # Delete the last element 19 >>> list2 [99, 25, 54, 2, 3, 4, 1, 32, 4] >>> list2.pop (2) # Delete the element at the specified position, delete element 54 with subscript 2 >>> list2 [99, 25, 2, 3, 4, 1, 32, 4] >>> list2.remove (4) >>> list2 [99, 25, 2, 3, 1, 32, 4] >>> list2.reverse () # reverse the original sequence >>> list2 [4, 32, 1, 3, 2, 25, 99] >>> list2.sort () # Sort the original sequence in ascending order >>> list2 [1, 2, 3, 4, 25, 32, 99]