The list of operations

Lists, like the previous data type strings, are ordered data structures, with the concept of indexes and slices. By giving an index number or using slices, we can get the data we want.

The use of indexing and slicing in Python will be covered in detail in this article.

The index

In Python, indexes can be positive or negative. A positive index starts at 0 on the left and a negative index starts at -1 on the right.

In a list, the index of an element indicates its position in the list.

Viewing data Information

# give a list

number = [-1.1.2.3.4.5.6.7.8.9.10.5.6.7.8.9]
Copy the code

  • The index starts at 0 on the left and -1 on the right
  • There are two ways to represent the same element
type(number)  # View data type as list
Copy the code

The result is of type list

list
Copy the code

To view the memory address, use the id function;

id(number)  # Check the list of memory addresses
Copy the code
4600162736
Copy the code

View the length of the list:

len(number)  Check the length of the list
Copy the code
16
Copy the code

Specify index number

number[0]  # First data
Copy the code
- 1Copy the code
number[-16]  # Count backwards
Copy the code
- 1Copy the code

The 16th inverse is also minus 1, because it’s just 16

number[-1]  # Final data
Copy the code
9
Copy the code
number[3]
Copy the code
3
Copy the code
number[-4]
Copy the code
6
Copy the code

If the specified index number exceeds the length of the list, an error will be reported:

number[18]  If # exceeds the length, an error will be reported
Copy the code
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) < ipython-input-10-983efb3D1bf6 > in <module> ----> 1 number[18Copy the code

The index function

The index function is used to find the first index position that an element appears in a list.

In the list created above, some elements are duplicated, such as 56789, and we use index to see where they are:

number.index(-1) 
Copy the code
0
Copy the code
number.index(6)  If # occurs multiple times, only the index position of the first occurrence is displayed
Copy the code
6
Copy the code
number.index(7)
Copy the code
7
Copy the code
number.index(9)
Copy the code
9
Copy the code

The index function can also specify the start and end index position of the search: index(x,start,stop)

  • X: The object to be searched.
  • Start: Indicates the start position of the search, which is optional.
  • End: Optional, the end of the search.
number.index(7.8.16)  # find the first position of 7; Start at index 8 and go to index 16
Copy the code
13
Copy the code
number.index(9.13.16)
Copy the code
15
Copy the code

slice

Slicing rule

List [start:stop:step] :

  • Start indicates the start index position (including); If you do not write it, it means to start from scratch
  • Stop indicates the end position (not included). If no, the slice operation is performed at the end
  • Step indicates the step size, which can be positive or negative. If no, the default value is 1

Is the index

number   # the original list
Copy the code
[1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 6, 7, 8, 9]Copy the code
len(number)  # List length is 16
Copy the code
16
Copy the code
The default step is 1
# # Does not contain data at index 11 position

number[0:11]  
Copy the code
[-1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Copy the code
# 2. Change the step size

number[0:15:2]
Copy the code
[-1, 2, 4, 6, 8, 10, 6, 8]
Copy the code
number[0:15:4]
Copy the code
[1, 4, 8, 6]Copy the code
# 3. Do not write the starting position

number[:15]  # start with index 0
Copy the code
[-1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 6, 7, 8, 8]Copy the code
number[:15:3]  # start at index 0 and step 3
Copy the code
[-1, 3, 6, 9, 6]
Copy the code
# 4. Do not write the end position

number[0: :]# Cut to the end
Copy the code
[1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 6, 7, 8, 9]Copy the code
number[1: :2]  # start at index 1, step 2
Copy the code
[1, 3, 5, 7, 9, 5, 7, 9]
Copy the code
number[1: :4]  Change the step size to 4
Copy the code
[1, 5, 9, 7)Copy the code
# 5. Do not write the beginning and end positions

number[::]  
Copy the code
[1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 6, 7, 8, 9]Copy the code

This gives you the same result as the original list, so you make a copy

number[::3]  # Step size 3
Copy the code
[-1, 3, 6, 9, 6, 9]
Copy the code

The negative index

# 1, default step size

number[-16: -2:]
Copy the code
[-1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 6, 7]
Copy the code
# 2. Change the step size

number[-16: -2:3]
Copy the code
[-1, 3, 6, 9, 6]
Copy the code
number[-10: -2:2]  # includes position -10, does not include position -2, step size 2
Copy the code
[6, 8, 10, 6]
Copy the code
# 3, do not specify the beginning

number[:-2:2]
Copy the code
[-1, 2, 4, 6, 8, 10, 6]
Copy the code
# 4. No end is specified

number[-16: :3]
Copy the code
[-1, 3, 6, 9, 6, 9]
Copy the code

Use both positive and negative indexes

number[-16:9:]  The position of # -16 is the position of the first element, not the position of index 9
Copy the code
[-1, 1, 2, 3, 4, 5, 6, 7, 8]
Copy the code
number[-15:13:2]  Change step size
Copy the code
[1, 3, 5, 7, 9, 5]
Copy the code
number[3: -3:]
Copy the code
[3, 4, 5, 6, 7, 8, 9, 10, 5, 6]
Copy the code
number[4: -1:2]
Copy the code
[4, 6, 8, 10, 6, 8]
Copy the code

Use negative step size

In the previous slicing operation, the step size was an integer, or the default 1, but now we will change it to a negative number as the index.

When using negative step sizes, start must be greater than or equal to the index of stop

number[::-2]  # Cut backwards, step size -2
Copy the code
[9, 7, 5, 9, 7, 5, 3, 1]
Copy the code
number[-3: -16: -3]
Copy the code
[7, 10, 7, 4, 1]
Copy the code
number[-2: : -2]
Copy the code
[8, 6, 10, 8, 6, 4, 2, -1]
Copy the code
number[:-15: -3]  # Cut from the last element (index -1), step -3 to -15
Copy the code
[9, 6, 9, 6, 3]
Copy the code
number[15:4: -3]
Copy the code
[9, 6, 9, 6]
Copy the code

Inversion of the list

You can reverse the entire list by setting the step size to -1

number[::-1]  Set step size to -1
Copy the code
[9, 8, 7, 6, 5, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1]Copy the code

Slice assignment

number   # the original data
Copy the code
[1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 6, 7, 8, 9]Copy the code
number[10:]
Copy the code
[10, 5, 6, 7, 8, 9]
Copy the code
id(number)
Copy the code
4600162736
Copy the code

We now change the index from 10 to all values:

number[10:] = [20.25.30.35.40.45]
Copy the code
number
Copy the code
[1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 25, 30, 35, 40, 45]Copy the code
id(number)  # Change data memory address still unchanged
Copy the code
4600162736
Copy the code

Deleting Slice Data

Delete some data from the list by using the del keyword. Deleting a piece of data from a list does not change its address in memory

number
Copy the code
[1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 25, 30, 35, 40, 45]Copy the code
number[12:]
Copy the code
[30, 35, 40, 45]
Copy the code
id(number)
Copy the code
4600162736
Copy the code
del number[12:]  # Delete in place without generating new data
Copy the code
number  # Missing deleted parts
Copy the code
[-1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 25]
Copy the code
id(number)  # Memory address unchanged
Copy the code

The memory address of the list does not change

4600162736
Copy the code