Introduction to the usage of Python deque

Introduction to the usage of Python deque

deque is a class in the Python standard library collections, which implements a queue that can be operated on at both ends, which is equivalent to a double-ended queue, which is very similar to Python's list of basic data types.

Python implementation of double-ended queue reference: https://blog.csdn.net/weixin_43790276/article/details/104033394

When using Python to implement a deque, a list is used. You can use a deque to compare with deque. There are many methods implemented in the deque class. Next, we will introduce the usage of the deque class.

1. Deque join and leave the team

The deque class implements a single data enqueue, an iterable object enqueues, and the data is inserted into the specified position.

1. Single data enqueue

# coding=utf-8
import collections


queue = collections.deque()
queue.append('a')
queue.append('b')
queue.append('c')
print(queue)
queue.appendleft('A')
queue.appendleft('B')
print(queue)

operation result:

deque(['a', 'b', 'c'])
deque(['B', 'A', 'a', 'b', 'c'])

append(item), adds a piece of data to the end of the queue. The function is similar to the append() method of the list.

appendleft(item), adds a piece of data to the head of the queue. Contrary to the direction of append().

2. Enqueue iterable objects

queue.extend(['D', 'E'])
queue.extendleft(['c', 'd'])
print(queue)

operation result:

deque(['d', 'c', 'B', 'A', 'a', 'b', 'c', 'D', 'E'])

extend(iterable), add the data in an iterable object to the end of the queue in order. It should be noted here that strings are also iterable objects. If you directly add the string'ABC','A','B', and'C' will be added to the queue, because'ABC' will be treated as an iterable object. Iteratively, if you want to add'ABC' as a whole string, you can put it in the list, and adding ['ABC'] will meet expectations.

extendleft(iterable), which adds the data in an iterable object to the head of the queue in order.

3. Insert data at specified location

queue.insert(3, 'T')
print(queue)

operation result:

deque(['d', 'c', 'B', 'T', 'A', 'a', 'b', 'c', 'D', 'E'])

insert(index, item), insert a piece of data at the specified position in the queue, and index is the specified position index.

The deque method at both ends of the queue is implemented in the deque class.

print(queue.pop())
print(queue.popleft())
print(queue)

operation result:

E
d
deque(['c', 'B', 'T', 'A', 'a', 'b', 'c', 'D'])

pop(), pop the data at the end of the queue and use it as the return value.

popleft(), pops the data at the head of the queue as the return value.

Two, deque's copy method

queue_b = queue.copy()
print(queue)
print(queue_b)
print(id(queue))
print(id(queue_b))

operation result:

deque(['c', 'B', 'T', 'A', 'a', 'b', 'c', 'D'])
deque(['c', 'B', 'T', 'A', 'a', 'b', 'c', 'D'])
2502045746056
2502045746992

copy(), copy the queue. After copying, operations on the original queue will not affect the copied queue. This method is only available in versions higher than Python 3.5.

Three, deque returns the number and index of the specified value

print(queue.count('b'))
queue.append('b')
print(queue.count('b'))
print(queue.count('z'))
print(queue.index('T'))

operation result:

1
2
0
2

count(item), returns the number of the specified value in the queue, or 0 if the value does not exist.

index(item), returns the index of the specified value in the queue, if the value does not exist, an error is reported, if there are multiple identical data, the index of the first value from left to right is returned.

Fourth, the flip and rotation of the deque

print(queue)
queue.reverse()
print(queue)
queue.rotate(3)
print(queue)

operation result:

deque(['c', 'B', 'T', 'A', 'a', 'b', 'c', 'D', 'b'])
deque(['b', 'D', 'c', 'b', 'a', 'A', 'T', 'B', 'c'])
deque(['T', 'B', 'c', 'b', 'D', 'c', 'b', 'a', 'A'])

reverse(), reverse the queue. It has the same function as the reverse() method of the list.

rotate(num), rotate the data in the queue. Each rotation is to dequeue the data at the end of the team and then enter the queue from the head of the team, which is equivalent to pop() and then appendleft(item), retate(num) to pass in the number of rotations.

Five, the deletion of deque

print(queue)
queue.remove('T')
print(queue)
queue.clear()
print(queue)

operation result:

deque(['T', 'B', 'c', 'b', 'D', 'c', 'b', 'a', 'A'])
deque(['B', 'c', 'b', 'D', 'c', 'b', 'a', 'A'])
deque([])

remove(item), delete the specified data from the queue. If the specified data does not exist, an error will be reported. If there are multiple identical data, only the first data from left to right will be deleted.

clear(), clear the queue.

Six, deque specifies the length of the queue

que = collections.deque(maxlen=5)
que.extend(['a', 'b', 'c', 'd', 'e'])
print(que)
que.append('F')
print(que)
que.appendleft('A')
print(que)

operation result:

deque(['a', 'b', 'c', 'd', 'e'], maxlen=5)
deque(['b', 'c', 'd', 'e', 'F'], maxlen=5)
deque(['A', 'b', 'c', 'd', 'e'], maxlen=5)

When instantiating the queue, you can use the maxlen method to specify the length of the queue. For ease of use, the maxlen method is converted into a property with property in the deque class, and maxlen can be passed in as a parameter when the deque class is initialized.

After the length of the queue is specified, if the queue has reached the maximum length and data is added from the end of the queue at this time, the data at the head of the queue will be automatically dequeued. The data at the head of the queue is equal to the newly added data at the end of the queue and is "squeezed" out of the queue to ensure that the length of the queue does not exceed the specified maximum length. On the contrary, adding data from the head of the team will automatically leave the data at the end of the team.

 

 

Guess you like

Origin blog.csdn.net/weixin_43790276/article/details/107749745