python deque

..

Một deque (phát âm là "boong") là một hàng đợi kết thúc kép, một cấu trúc dữ liệu cho phép bạn thêm và loại bỏ các phần tử từ hai đầu.Deques linh hoạt hơn hàng đợi, chỉ có thể được thêm vào ở cuối và được loại bỏ khỏi phía trước.Deques cũng nhanh hơn danh sách để chèn và loại bỏ các yếu tố ở đầu hoặc cuối, vì chúng không cần phải được thay đổi xung quanh để nhường chỗ cho các yếu tố mới.

Các deques được triển khai trong Python bằng cách sử dụng một danh sách được liên kết gấp đôi, điều đó có nghĩa là mỗi phần tử trong deque có một con trỏ đến phần tử trước nó và phần tử sau nó.Điều này làm cho nó có thể thêm và loại bỏ các phần tử từ một trong hai đầu của deque trong thời gian không đổi.

Để tạo một deque trong Python, bạn có thể sử dụng hàm `deque ()`.Hàm `deque ()` lấy một danh sách các yếu tố làm đối số của nó và tạo ra một deque chứa các yếu tố đó.Ví dụ:

`` `Python
d = deque ([1, 2, 3])
`` `

Bạn cũng có thể tạo một deque trống bằng cách chuyển một danh sách trống cho hàm `deque ()`:

`` `Python
d = deque ()
`` `

Khi bạn đã tạo một deque, bạn có thể thêm các phần tử vào nó bằng các phương thức `append ()` và `appendleft ()`.Phương thức `` append () `thêm một phần tử vào phần cuối của deque và phương thức` appendleft () `thêm một phần tử vào đầu của deque.Ví dụ:

`` `Python
D.Append (4)
D.Appendleft (0)
`` `

Bạn có thể loại bỏ các phần tử khỏi một deque bằng các phương thức `pop ()` và `popleft ()`.Phương thức `pop ()` loại bỏ một phần tử từ đầu của deque và phương thức `popleft ()` loại bỏ một phần tử từ đầu của deque.Ví dụ:

`` `Python
D.Pop ()
D.Popleft ()
`` `

Bạn cũng có thể lặp lại một deque bằng cách sử dụng vòng `for`.Khi bạn lặp lại một deque, các yếu tố sẽ được trả lại theo thứ tự mà chúng được thêm vào deque.Ví dụ:

`` `Python
Đối với mục trong d:
in (mục)
`` `

Deques là một cấu trúc dữ liệu mạnh mẽ có thể được sử dụng cho nhiều tác vụ khác nhau, chẳng hạn như lưu trữ hàng đợi các mặt hàng, thực hiện ngăn xếp hoặc thực hiện các hoạt động cửa sổ trượt.

** Hashtags: **

* #Python
* #cấu trúc dữ liệu
* #linkedList
* #xếp hàng
* #deque
=======================================
#Python #deque #datastructure #linkedList #queue **Python Deque: What is it and how to use it?**

A deque (pronounced "deck") is a double-ended queue, a data structure that allows you to add and remove elements from either end. Deques are more flexible than queues, which can only be added to at the end and removed from the front. Deques are also faster than lists for inserting and removing elements at the beginning or end, as they do not need to be shifted around to make room for new elements.

Deques are implemented in Python using a doubly linked list, which means that each element in the deque has a pointer to the element before it and the element after it. This makes it possible to add and remove elements from either end of the deque in constant time.

To create a deque in Python, you can use the `deque()` function. The `deque()` function takes a list of elements as its argument, and creates a deque containing those elements. For example:

```python
d = deque([1, 2, 3])
```

You can also create an empty deque by passing an empty list to the `deque()` function:

```python
d = deque()
```

Once you have created a deque, you can add elements to it using the `append()` and `appendleft()` methods. The `append()` method adds an element to the end of the deque, and the `appendleft()` method adds an element to the beginning of the deque. For example:

```python
d.append(4)
d.appendleft(0)
```

You can remove elements from a deque using the `pop()` and `popleft()` methods. The `pop()` method removes an element from the end of the deque, and the `popleft()` method removes an element from the beginning of the deque. For example:

```python
d.pop()
d.popleft()
```

You can also iterate over a deque using the `for` loop. When you iterate over a deque, the elements will be returned in the order in which they were added to the deque. For example:

```python
for item in d:
print(item)
```

Deques are a powerful data structure that can be used for a variety of tasks, such as storing a queue of items, implementing a stack, or performing sliding window operations.

**Hashtags:**

* #Python
* #datastructure
* #linkedList
* #queue
* #deque
 
Join ToolsKiemTrieuDoGroup
Back
Top
AdBlock Detected

We get it, advertisements are annoying!

Sure, ad-blocking software does a great job at blocking ads, but it also blocks useful features of our website. For the best site experience please disable your AdBlocker.

I've Disabled AdBlock