Python3 based list instance resolution

  • 2020-04-02 13:55:05
  • OfStack

In general, any value in Python is an object, so any type (int, STR, list...) It's all one class. While a class must have its methods or properties, it is obviously impossible to keep track of all the methods of so many classes. Here are two tips:

Dir () : a built-in function that queries all properties of a class or object, for example > > > Dir (list).

Help () : built-in function for querying specific documentation, for example > > > Help (int).

In the previous post, we took a look at lists and saw that lists are the most frequently used data type in Python. This article takes a closer look at the use of lists.

I. method of list:

List. Append (x)
Adding an item to the end of the list is equivalent to a[len(a):] = [x].

List. The extend (L)
Attaching the given list L to the current list is equivalent to a[len(a):] = L.

List. The insert (I, x)
Inserting an item before a given position I, for example, a.insert(0, x) is inserted at the head of the list, while a.insert(len(a), x) is equivalent to a.append(x).

List. Remove (x)
Removing the first item in the list whose value is x will cause an error.

Pop ([I]) is list.
Deletes an item from a given position in the list and returns it. If no index is specified, a.op () removes and returns the last item in the list. (brackets indicate optional)

List. The clear ()
Deletes all items from the list, del a[:].

List. The index (x)
Returns the index of the item in the list whose first value is x. If there is no matching item, an error occurs.

List. The count (x)
Returns the number of occurrences of x in the list.

List. The sort ()
Sort the list in place.

List. The reverse ()
Flip the list items in place.

List. The copy ()
Returns a shallow copy of the list, equivalent to a[:].

List when stack

The List method makes it easy to use as a stack. As we know, the characteristics of the stack are that the last entry element comes out first (i.e., last in first out), the stack is pressed with the append() method, and the stack is pushed with the pop() method without specifying the index.

The sample code is as follows:


stack = []
for x in range(1,6):
 stack.append(x)  #  Into the stack 
 print('push', x, end=' ')
 print(stack)

print('Now stack is', stack)

while len(stack)>0:
 print('pop', stack.pop(), end=' ') #  Out of the stack 
 print(stack)

List as queue

A list can also be used as a queue, with the feature that the first element to be added is the first to be taken out (i.e., first in, first out). However, using a list as a queue is not efficient because it is fast to add and pop elements from the end of the list, and slow to insert or pop them at the beginning of the list (because all elements have to move one place).
To implement a queue, use the collections. Deque of the standard library, which is designed to add and pop up quickly on both ends.

The sample code is as follows:


from collections import deque
queue = deque()    #  Create an empty queue 
for x in range(1,6):
 queue.append(x)  #  The team 
 print('push', x, end=' ')
 print(list(queue))

print('Now queue is', list(queue))

while len(queue)>0:
 print('pop', queue.popleft(), end=' ') #  Out of the team 
 print(list(queue))

Iv. List derivation

List derivation provides an easy way to create a list from a sequence. Usually the program does something with each element of the sequence and USES the result as an element in the new list, or creates a subsequence based on a specified condition.

The structure of the list derivation is: in a square bracket, first an expression, then a for clause, then zero or more for or if clauses. The return result is a list of expressions generated from the for and if contexts that follow.

The sample code is as follows:


squares = [x**2 for x in range(10)] #  The derived type 
print(squares)
#  The output is [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
pairs = [(x, y) for x in [1,2,3] for y in [3,1,4] if x!=y] #  The derived type 
print(pairs)
#  The output is [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]

Nested lists

There is no concept of two-dimensional arrays in Python, but we can do the same thing with nested lists.


mat = [
   [1, 2, 3],
   [4, 5, 6],
   [7, 8, 9]
   ]

Again, we can use the derivation to generate nested lists:


mat = [[1,2,3], [4,5,6], [7,8,9]]
new_mat = [ [row[i] for row in mat] for i in [0,1,2] ] #  nested 
print(new_mat)
#  The output [[1, 4, 7], [2, 5, 8], [3, 6, 9]]

Attachment: del statement

The del statement, unlike the pop() method, which returns a value, can drop items from a list by giving them an index instead of a value. The del statement can also remove slices from the list or clear the entire list:


lst = [1,2,3,4,5,6,7,8,9]
del lst[2]  #  Deletes the specified index entry 
print(lst)  
del lst[2:5] #  Delete slices 
print(lst)
del lst[:]  #  Delete the entire list 
print(lst)
del Can also be used to delete variable entities: 
del lst

An error occurs when a reference to LST is made after the variable entity is deleted.


Related articles: