SoFunction
Updated on 2024-12-19

python list query operations and slicing

1. List

  • The list ispythonBuilt-in data structures (lists, tuples, dictionaries, collections), equivalent to arrays
  • All the data in a list is ordered in a sequential order, lists are of the sequence type
  • All data in the list is indexed positively and negatively, and can always be mapped to uniquely identified data by the specified indexes
  • Duplicates can exist in the list (distinguishing between sets)
  • Lists can hold any type of data, and multiple types of data can be mixed and stored in a single list
  • The list can be dynamically scaled as needed, and the system will dynamically allocate and reclaim memory as needed without declaring the capacity of the list before use.

2. list creation [] or list()

  • Create lists using the center bracket [], do not name the variable list or l, list is the class name of the list and l can be easily mistaken for the number 1
  • Calling built-in functionslist()((Constructor for class list)
L = [] # Create an empty list
L = [1,2,3,4,5,'python']
print(L) # [1, 2, 3, 4, 5, 'python']
list(rang(1, 5)) # Pass in the range object [1,2,3,4].
list([1,2,3,4,5,'python']) # Directly pass in the middle bracket []
list() # Creating an Empty List

3. Locate element L[0] in the list

Get the elements of the list using the index, throw an error if the specified index does not exist in the listIndexError: list index out of range

4. Query list element index ()

  • The index of the first element is 0, and the subsequent elements are in order +1
  • The index of the last element is -1, and the preceding elements are in turn -1

L = ['H','e','l','l','o'] # Define a list, the elements can be numeric, but for fear of confusing the indexes use the characters
('e')
('l')
('h') # value error
('l',2) # Find 'l' from index 2 #
('l',2,5) # exist[2, 4]look for'l'
  • Get the index of the specified element in the list call methodindex, only values greater than 0 are returned, e.g. ('e') = 1, if there is more than one of the specified elements in the list, the methodindexReturns only the index value of the first specified element, e.g. ('l') = 2, throws an error if the specified element does not exist in the listValueError: ‘h’ is not in list
  • The index method can also be called with two parameters, start and stop, to find an element within a certain range (not including the value at stop).

5. List slicing operation L[start:stop:step]

  • Syntax format for slicing [start:stop:step]
  • The resulting slice is still a list, a copy of a fragment of the original list
  • The resulting slice does not include the element corresponding to index stop
  • The default value of step is 1, which can be simplified to [start:stop].
  • When STEP is positive
  1. If start is not specified, the first element of the slice is by default the first element of the list.
  2. If stop is not specified, the last element of the slice is by default the last element of the list.
  3. Counting slices backward from index start
  • (coll.) fail (a student)stepnegative number
  1. If start is not specified, the first element of the slice defaults to the last element of the list.
  2. If stop is not specified, the last element of the slice is by default the first element of the list.
  3. Counting slices forward from index start

L = list('HelloWorld')
L[1:7:2]
L[1:6]
L[:] # Return to the entire list SyntaxError: invalid syntax for L[].
L[::-1] # Flip the whole list
L[:-1] # stop is specified as the element where -1 is located ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l'].
L[6:0:-2]
L[0:6:-2] # start is specified as the element where 0 is located, there is no value looking forward, return []
L[8::-2] # ['l', 'o', 'o', 'l', 'H']
L[8:0:-2] # ['l', 'o', 'o', 'l'] does not contain the element specified by stop
L[-2:0:-2]
L[:3:-2]
  • Slicing operations allow index overruns (comparison indexes do not)
L = list('HelloWorld')
L[:100]
L[-100:]

[slice(start,stop,step)]

  • You can create an object of type slice by calling the built-in function slice (the constructor of class slice)
  • The built-in function slice is called in three ways
  1. slice(stop)
  2. slice(start,stop)
  3. slice(start,stop,step)
  • The default values for start, stop, and step are all None.
  • slice(start,stop,step) is equivalent to start:stop:step in the slice operation
L = list('HelloWorld')
L[slice(1,9,2)]
L[1:9:2]
L[::]
L[slice(None,None,None)] # L[slice(None)] Returns the entire list
L[1:7]
L[slice(1,7)]
L[:7]
L[slice(7)] #You can just enterstop,Can also write L[slice(None, 7)]

/not in queries whether an element is included, returns True if it exists.

L = list('HelloWorld')
print(5 in L) # False

To this point this article on python list of query operations and slicing of the article is introduced to this, more related to python list of query operations and slicing of the content please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!