python: Detailed explanation of the list

  • 2021-12-05 06:53:05
  • OfStack

Directory List list1, List Creation 2, List Access
1) Access to a 1-D list 2) Access to a 2-D list
3. Modify the element 5. del Command
6. List operation
1) List addition 2) List multiplication 7, list method
1) index(value[,start=0[,stop]])
2) count()
3) append ()
4)extend()
5)insert()
Summarize

List list

1. List creation


list1 = [3.14, 1.61, 0, -9, 6]
list2 = [ ' train',  ' bus',  ' car',  ' ship']
list3 = [ ' a',200, ' b',150,  ' c',100]
list4 = [] # Create an empty list 

In Python, a list within a list is often used, that is, a 2-dimensional list

2. List access

Indexed access is applicable to all sequence types of objects: lists, tuples, strings.

1) Access to a 1-dimensional list

vehicle = [ ' train',  ' bus',  ' car',  ' ship']
vehicle[0]
 ' train'

vehicle[1]
 ' bus'

vehicle[2]
 ' car'

2) Access to 2-D lists

To access an element in a 2-dimensional list, you need to use two square parentheses, the first to select a sublist, and the second to select its element in the selected sublist.


computer=[[ ' IBM', ' Apple', ' Lenovo'],[ ' America', ' America', ' China']]
computer[0][-1]
 ' Lenovo'

computer[1][2]
 ' China'

3. Modify the element

4. List slicing

1. In a list, you can use the slicing operation to select elements at a specified location to form a new list. The simple slicing method is:

Original list name [start: end]

You need to provide a start value start and an end value end as the start and end index boundaries of the slice.

Elements at the beginning value start index position are included in the slice, and elements at the end value end index position are not included in the slice;

Default when the left index start of the slice is 0 or when the right index end is the list length.

This simple slicing operation selects elements with indexes in [start, end] from the original list to form a new list.

2. The slicing operation can also provide a non-zero integer (that is, positive or negative, but not 0) as the step size step value for index value growth. Used as follows:

Original list name [start: end: step]

When the step size is 1, the step parameter can be omitted.
If the step size step is 1, it can be omitted. If the step size step is not 1, this parameter cannot be omitted.
The slicing operation applies to all sequence types.

5. del Command

Use the del command to delete elements from a list, or to delete an entire list.

vehicle = [ ' train',  ' bus',  ' car',  ' ship']
del vehicle[3]
vehicle # Deleted 'ship'
[ ' train',  ' bus',  ' car']

del vehicle[3] # Out of index range 
Traceback (most recent call last):
File  " <pyshell#50> " , line 1, in
del vehicle[3]
IndexError: list assignment index out of range

del vehicle # Delete list vehicle
vehicle # List vehicle It doesn't exist anymore 
Traceback (most recent call last):
File  " <pyshell#82> " , line 1, in
vehicle
NameError: name  ' vehicle' is not defined
 In addition, remove() , pop() , clear() Method can delete list elements 

6. List operation

1) List addition

vehicle1 = [ ' train',  ' bus',  ' car',  ' ship']
vehicle2 = [ ' subway',  ' bicycle']
vehicle1 + vehicle2
[ ' train',  ' bus',  ' car',  ' ship',  ' subway',  ' bicycle']

vehicle = [ ' train',  ' bus',  ' car',  ' ship']
vehicle[0]
 ' train'
0

vehicle2
[ ' subway',  ' bicycle']

vehicle=vehicle1 + vehicle2 #  Generate a new list and assign it to a variable vehicle
vehicle
[ ' train',  ' bus',  ' car',  ' ship',  ' subway',  ' bicycle']

vehicle = [ ' train',  ' bus',  ' car',  ' ship']
vehicle[0]
 ' train'
3

2) List multiplication

vehicle = [ ' train',  ' bus',  ' car',  ' ship']
vehicle[0]
 ' train'
4

vehicle = [ ' train',  ' bus',  ' car',  ' ship']
vehicle[0]
 ' train'
5

vehicle = [ ' train',  ' bus',  ' car',  ' ship']
vehicle[0]
 ' train'
6

vehicle*=2 # Compound assignment statement 
vehicle
[ ' train',  ' bus',  ' train',  ' bus',  ' train',  ' bus',  ' train',  ' bus']

7. List method

A method in a list can be thought of as a function that acts on the list in Python, which is a specific type of object.

1) index(value[,start=0[,stop]])

The index () method is used to find the first element index position in the list that matches the value value.

If no value for the parameter start is specified, start the lookup at the index 0, otherwise start the lookup at the index strat.

If you do not specify a value for the end index position stop, you can find the last element of the list, otherwise you can find the index interval within [start, stop].

If no match is found, an exception is thrown.


vehicle = [ ' train',  ' bus',  ' car',  ' subway',  ' ship',  ' bicycle',  ' car']
vehicle.index( ' car') # Entire list scope 'car' No. 1 1 The index position of the second occurrence is 2
2

vehicle = [ ' train',  ' bus',  ' car',  ' ship']
vehicle[0]
 ' train'
9

vehicle.index( ' plane') # There are no 'plane'
Traceback (most recent call last):
File  " <pyshell#81> " , line 1, in
vehicle.index( ' plane')
ValueError:  ' plane' is not in list

vehicle.index( ' plane') # There are no 'plane'
Traceback (most recent call last):
File  " <pyshell#81> " , line 1, in
vehicle.index( ' plane')
ValueError:  ' plane' is not in list

2) count()

The count () method, which counts the number of occurrences of an element in the list.


vehicle = [ ' train',  ' bus',  ' car',  ' subway',  ' ship',  ' bicycle',  ' car']
vehicle.count( ' car')
2

vehicle.count( ' bus')
1

vehicle.count( ' bike')
0

3) append ()

The append () method appends a single element to the end of the list, accepts only one element, and the element can be of any data type. The appended element keeps its original structure type in the list.


vehicle = [ ' train',  ' bus',  ' car',  ' ship']
vehicle.append ( ' plane') # Append 1 Elements 'plane'
vehicle
[ ' train',  ' bus',  ' car',  ' ship',  ' plane']

vehicle.append(8) # Append 1 Elements 8
vehicle
[ ' train',  ' bus',  ' car',  ' ship',  ' plane', 8]

vehicle.append([8,9]) # Append 1 Elements [8,9]
vehicle
[ ' train',  ' bus',  ' car',  ' ship',  ' plane', 8, [8, 9]]

vehicle.append(10,11) # Append 2 Elements 10 And 11 , error 
Traceback (most recent call last):
File  " <pyshell#7> " , line 1, in
vehicle.append(10,11)
TypeError: append() takes exactly one argument (2 given)

4)extend()

List. The extend () method appends to the end of the list all the elements in the parameter container (list, tuple, string, dictionary, collection) in the method. If the parameter is a dictionary, all keys in the dictionary are appended (key)

5)insert()

The insert () method, which inserts 1 element into the list at the specified position. The list's insert method takes two parameters, the first parameter is the index point, which is the insertion position, and the second parameter is the insertion element.

Summarize

This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!


Related articles: