{"id":1062413,"date":"2024-12-31T15:51:34","date_gmt":"2024-12-31T07:51:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1062413.html"},"modified":"2024-12-31T15:51:37","modified_gmt":"2024-12-31T07:51:37","slug":"python%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e6%a0%88%e5%92%8c%e9%98%9f%e5%88%97%e7%9a%84%e8%bd%ac%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1062413.html","title":{"rendered":"python\u5982\u4f55\u4f7f\u7528\u6808\u548c\u961f\u5217\u7684\u8f6c\u6362"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/60d22a13-d564-4a56-85cf-52a9eec5ba66.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u4f7f\u7528\u6808\u548c\u961f\u5217\u7684\u8f6c\u6362\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u6808\u548c\u961f\u5217\u7684\u8f6c\u6362\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684\u6570\u636e\u7ed3\u6784\u5217\u8868\uff08list\uff09\u6765\u5b9e\u73b0\u3002\u6808\u548c\u961f\u5217\u7684\u64cd\u4f5c\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u5165\u6808\u3001\u51fa\u6808\u3001\u5165\u961f\u548c\u51fa\u961f\u3002\u901a\u8fc7\u8fd9\u4e9b\u64cd\u4f5c\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6808\u548c\u961f\u5217\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3001\u7ef4\u62a4\u6570\u636e\u7684\u987a\u5e8f\u3001\u63d0\u9ad8\u7a0b\u5e8f\u7684\u8fd0\u884c\u6548\u7387\u3002<\/strong> \u5176\u4e2d\uff0c\u6808\uff08Stack\uff09\u662f\u540e\u8fdb\u5148\u51fa\uff08LIFO\uff09\u7684\u6570\u636e\u7ed3\u6784\uff0c\u961f\u5217\uff08Queue\uff09\u662f\u5148\u8fdb\u5148\u51fa\uff08FIFO\uff09\u7684\u6570\u636e\u7ed3\u6784\u3002\u4f7f\u7528Python\u7684\u5217\u8868\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5b9e\u73b0\u8fd9\u4e24\u79cd\u6570\u636e\u7ed3\u6784\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5b9e\u73b0\u6808\u548c\u961f\u5217\u7684\u8f6c\u6362\uff0c\u5e76\u63a2\u8ba8\u5177\u4f53\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6808\u7684\u57fa\u672c\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u6808\u662f\u4e00\u79cd\u540e\u8fdb\u5148\u51fa\uff08LIFO\uff09\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5728\u6808\u7684\u9876\u90e8\u8fdb\u884c\u6570\u636e\u7684\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u3002Python\u7684\u5217\u8868\uff08list\uff09\u53ef\u4ee5\u7528\u4f5c\u6808\uff0c\u4e3b\u8981\u64cd\u4f5c\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5165\u6808\uff08Push\uff09\uff1a<\/strong> \u5c06\u4e00\u4e2a\u5143\u7d20\u6dfb\u52a0\u5230\u6808\u7684\u9876\u90e8\u3002<\/li>\n<li><strong>\u51fa\u6808\uff08Pop\uff09\uff1a<\/strong> \u79fb\u9664\u5e76\u8fd4\u56de\u6808\u9876\u5143\u7d20\u3002<\/li>\n<li><strong>\u67e5\u770b\u6808\u9876\u5143\u7d20\uff08Peek\uff09\uff1a<\/strong> \u8fd4\u56de\u6808\u9876\u5143\u7d20\u4f46\u4e0d\u79fb\u9664\u5b83\u3002<\/li>\n<li><strong>\u68c0\u67e5\u6808\u662f\u5426\u4e3a\u7a7a\uff1a<\/strong> \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\u3002<\/li>\n<\/ol>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">class Stack:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.stack = []<\/p>\n<p>    def push(self, item):<\/p>\n<p>        self.stack.append(item)<\/p>\n<p>    def pop(self):<\/p>\n<p>        if not self.is_empty():<\/p>\n<p>            return self.stack.pop()<\/p>\n<p>        else:<\/p>\n<p>            r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se IndexError(&quot;Pop from empty stack&quot;)<\/p>\n<p>    def peek(self):<\/p>\n<p>        if not self.is_empty():<\/p>\n<p>            return self.stack[-1]<\/p>\n<p>        else:<\/p>\n<p>            raise IndexError(&quot;Peek from empty stack&quot;)<\/p>\n<p>    def is_empty(self):<\/p>\n<p>        return len(self.stack) == 0<\/p>\n<p>    def size(self):<\/p>\n<p>        return len(self.stack)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u961f\u5217\u7684\u57fa\u672c\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u961f\u5217\u662f\u4e00\u79cd\u5148\u8fdb\u5148\u51fa\uff08FIFO\uff09\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5143\u7d20\u4ece\u961f\u5217\u7684\u672b\u5c3e\u8fdb\u5165\uff0c\u4ece\u961f\u5217\u7684\u5934\u90e8\u79fb\u9664\u3002Python\u7684\u5217\u8868\u4e5f\u53ef\u4ee5\u7528\u4f5c\u961f\u5217\uff0c\u4e3b\u8981\u64cd\u4f5c\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5165\u961f\uff08Enqueue\uff09\uff1a<\/strong> \u5c06\u4e00\u4e2a\u5143\u7d20\u6dfb\u52a0\u5230\u961f\u5217\u7684\u672b\u5c3e\u3002<\/li>\n<li><strong>\u51fa\u961f\uff08Dequeue\uff09\uff1a<\/strong> \u79fb\u9664\u5e76\u8fd4\u56de\u961f\u5217\u7684\u5934\u90e8\u5143\u7d20\u3002<\/li>\n<li><strong>\u67e5\u770b\u961f\u5217\u5934\u90e8\u5143\u7d20\uff08Front\uff09\uff1a<\/strong> \u8fd4\u56de\u961f\u5217\u5934\u90e8\u5143\u7d20\u4f46\u4e0d\u79fb\u9664\u5b83\u3002<\/li>\n<li><strong>\u68c0\u67e5\u961f\u5217\u662f\u5426\u4e3a\u7a7a\uff1a<\/strong> \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\u3002<\/li>\n<\/ol>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">class Queue:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.queue = []<\/p>\n<p>    def enqueue(self, item):<\/p>\n<p>        self.queue.append(item)<\/p>\n<p>    def dequeue(self):<\/p>\n<p>        if not self.is_empty():<\/p>\n<p>            return self.queue.pop(0)<\/p>\n<p>        else:<\/p>\n<p>            raise IndexError(&quot;Dequeue from empty queue&quot;)<\/p>\n<p>    def front(self):<\/p>\n<p>        if not self.is_empty():<\/p>\n<p>            return self.queue[0]<\/p>\n<p>        else:<\/p>\n<p>            raise IndexError(&quot;Front from empty queue&quot;)<\/p>\n<p>    def is_empty(self):<\/p>\n<p>        return len(self.queue) == 0<\/p>\n<p>    def size(self):<\/p>\n<p>        return len(self.queue)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6808\u548c\u961f\u5217\u7684\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u6808\u548c\u961f\u5217\u4e4b\u95f4\u7684\u8f6c\u6362\u4e3b\u8981\u4f9d\u8d56\u4e8e\u5b83\u4eec\u7684\u57fa\u672c\u64cd\u4f5c\u3002\u901a\u8fc7\u8fd9\u4e9b\u64cd\u4f5c\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6808\u548c\u961f\u5217\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h4>1. \u5c06\u6808\u8f6c\u6362\u4e3a\u961f\u5217<\/h4>\n<\/p>\n<p><p>\u8981\u5c06\u6808\u8f6c\u6362\u4e3a\u961f\u5217\uff0c\u53ef\u4ee5\u5229\u7528\u6808\u7684\u51fa\u6808\uff08pop\uff09\u64cd\u4f5c\u548c\u961f\u5217\u7684\u5165\u961f\uff08enqueue\uff09\u64cd\u4f5c\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u961f\u5217\u3002<\/li>\n<li>\u4e0d\u65ad\u5c06\u6808\u9876\u5143\u7d20\u51fa\u6808\uff0c\u5e76\u5c06\u5176\u5165\u961f\u5230\u65b0\u7684\u961f\u5217\u4e2d\uff0c\u76f4\u5230\u6808\u4e3a\u7a7a\u3002<\/li>\n<\/ul>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def stack_to_queue(stack):<\/p>\n<p>    queue = Queue()<\/p>\n<p>    while not stack.is_empty():<\/p>\n<p>        queue.enqueue(stack.pop())<\/p>\n<p>    return queue<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5c06\u961f\u5217\u8f6c\u6362\u4e3a\u6808<\/h4>\n<\/p>\n<p><p>\u8981\u5c06\u961f\u5217\u8f6c\u6362\u4e3a\u6808\uff0c\u53ef\u4ee5\u5229\u7528\u961f\u5217\u7684\u51fa\u961f\uff08dequeue\uff09\u64cd\u4f5c\u548c\u6808\u7684\u5165\u6808\uff08push\uff09\u64cd\u4f5c\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6808\u3002<\/li>\n<li>\u4e0d\u65ad\u5c06\u961f\u5217\u5934\u90e8\u5143\u7d20\u51fa\u961f\uff0c\u5e76\u5c06\u5176\u5165\u6808\u5230\u65b0\u7684\u6808\u4e2d\uff0c\u76f4\u5230\u961f\u5217\u4e3a\u7a7a\u3002<\/li>\n<\/ul>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def queue_to_stack(queue):<\/p>\n<p>    stack = Stack()<\/p>\n<p>    while not queue.is_empty():<\/p>\n<p>        stack.push(queue.dequeue())<\/p>\n<p>    return stack<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6808\u548c\u961f\u5217\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u6808\u548c\u961f\u5217\u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u548c\u7f16\u7a0b\u4e2d\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u5b83\u4eec\u7684\u4e0d\u540c\u7279\u6027\u4f7f\u5f97\u5b83\u4eec\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1. \u6808\u7684\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<ul>\n<li><strong>\u51fd\u6570\u8c03\u7528\uff1a<\/strong> \u6808\u7528\u4e8e\u7ba1\u7406\u51fd\u6570\u8c03\u7528\u7684\u8fd4\u56de\u5730\u5740\u548c\u5c40\u90e8\u53d8\u91cf\u3002<\/li>\n<li><strong>\u8868\u8fbe\u5f0f\u6c42\u503c\uff1a<\/strong> \u6808\u7528\u4e8e\u4e2d\u7f00\u8868\u8fbe\u5f0f\u8f6c\u6362\u4e3a\u540e\u7f00\u8868\u8fbe\u5f0f\uff0c\u4ee5\u53ca\u540e\u7f00\u8868\u8fbe\u5f0f\u7684\u6c42\u503c\u3002<\/li>\n<li><strong>\u62ec\u53f7\u5339\u914d\uff1a<\/strong> \u6808\u7528\u4e8e\u68c0\u67e5\u8868\u8fbe\u5f0f\u4e2d\u7684\u62ec\u53f7\u662f\u5426\u5339\u914d\u3002<\/li>\n<li><strong>\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff08DFS\uff09\uff1a<\/strong> \u6808\u7528\u4e8e\u5b9e\u73b0\u56fe\u7684\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u7b97\u6cd5\u3002<\/li>\n<\/ul>\n<p><h4>2. \u961f\u5217\u7684\u5e94\u7528\u573a\u666f<\/h4>\n<\/p>\n<ul>\n<li><strong>\u4efb\u52a1\u8c03\u5ea6\uff1a<\/strong> \u961f\u5217\u7528\u4e8e\u7ba1\u7406\u4efb\u52a1\u7684\u6267\u884c\u987a\u5e8f\uff0c\u5982\u64cd\u4f5c\u7cfb\u7edf\u7684\u4efb\u52a1\u8c03\u5ea6\u3002<\/li>\n<li><strong>\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\uff08BFS\uff09\uff1a<\/strong> \u961f\u5217\u7528\u4e8e\u5b9e\u73b0\u56fe\u7684\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u7b97\u6cd5\u3002<\/li>\n<li><strong>\u6d88\u606f\u961f\u5217\uff1a<\/strong> \u961f\u5217\u7528\u4e8e\u5728\u4e0d\u540c\u7cfb\u7edf\u6216\u8fdb\u7a0b\u4e4b\u95f4\u4f20\u9012\u6d88\u606f\u3002<\/li>\n<li><strong>\u7f13\u51b2\u533a\uff1a<\/strong> \u961f\u5217\u7528\u4e8e\u5b9e\u73b0\u6570\u636e\u7f13\u51b2\u533a\uff0c\u5982\u7f51\u7edc\u6570\u636e\u5305\u7684\u7f13\u51b2\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u6808\u548c\u961f\u5217\u7684\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1Python\u7684\u5217\u8868\u53ef\u4ee5\u7528\u4f5c\u6808\u548c\u961f\u5217\uff0c\u4f46\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528<code>collections.deque<\/code>\u6765\u4ee3\u66ff\u5217\u8868\u3002<code>deque<\/code>\uff08\u53cc\u7aef\u961f\u5217\uff09\u5728\u4e24\u7aef\u7684\u64cd\u4f5c\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662fO(1)\uff0c\u800c\u5217\u8868\u5728\u5934\u90e8\u7684\u64cd\u4f5c\u65f6\u95f4\u590d\u6742\u5ea6\u662fO(n)\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528<code>deque<\/code>\u5b9e\u73b0\u6808\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import deque<\/p>\n<p>class Stack:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.stack = deque()<\/p>\n<p>    def push(self, item):<\/p>\n<p>        self.stack.append(item)<\/p>\n<p>    def pop(self):<\/p>\n<p>        if not self.is_empty():<\/p>\n<p>            return self.stack.pop()<\/p>\n<p>        else:<\/p>\n<p>            raise IndexError(&quot;Pop from empty stack&quot;)<\/p>\n<p>    def peek(self):<\/p>\n<p>        if not self.is_empty():<\/p>\n<p>            return self.stack[-1]<\/p>\n<p>        else:<\/p>\n<p>            raise IndexError(&quot;Peek from empty stack&quot;)<\/p>\n<p>    def is_empty(self):<\/p>\n<p>        return len(self.stack) == 0<\/p>\n<p>    def size(self):<\/p>\n<p>        return len(self.stack)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528<code>deque<\/code>\u5b9e\u73b0\u961f\u5217\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import deque<\/p>\n<p>class Queue:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.queue = deque()<\/p>\n<p>    def enqueue(self, item):<\/p>\n<p>        self.queue.append(item)<\/p>\n<p>    def dequeue(self):<\/p>\n<p>        if not self.is_empty():<\/p>\n<p>            return self.queue.popleft()<\/p>\n<p>        else:<\/p>\n<p>            raise IndexError(&quot;Dequeue from empty queue&quot;)<\/p>\n<p>    def front(self):<\/p>\n<p>        if not self.is_empty():<\/p>\n<p>            return self.queue[0]<\/p>\n<p>        else:<\/p>\n<p>            raise IndexError(&quot;Front from empty queue&quot;)<\/p>\n<p>    def is_empty(self):<\/p>\n<p>        return len(self.queue) == 0<\/p>\n<p>    def size(self):<\/p>\n<p>        return len(self.queue)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230<strong>\u6808\u548c\u961f\u5217\u662f\u4e24\u79cd\u91cd\u8981\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u4eec\u7684\u4e0d\u540c\u7279\u6027\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002<\/strong> \u5728Python\u4e2d\uff0c\u4f7f\u7528\u5217\u8868\u548c<code>collections.deque<\/code>\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u6808\u548c\u961f\u5217\uff0c\u5e76\u4e14\u80fd\u591f\u5728\u5b83\u4eec\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002\u7406\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u7684\u57fa\u672c\u64cd\u4f5c\u548c\u5e94\u7528\u573a\u666f\uff0c\u5bf9\u4e8e\u7f16\u5199\u9ad8\u6548\u548c\u53ef\u9760\u7684\u4ee3\u7801\u5177\u6709\u91cd\u8981\u610f\u4e49\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4f7f\u7528Python\u5b9e\u73b0\u6808\u548c\u961f\u5217\u7684\u8f6c\u6362\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u65b9\u6cd5\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u6216collections\u6a21\u5757\u4e2d\u7684deque\u6765\u5b9e\u73b0\u6808\u548c\u961f\u5217\u3002\u6808\u901a\u5e38\u4f7f\u7528\u5217\u8868\u7684append()\u548cpop()\u65b9\u6cd5\u6765\u8fdb\u884c\u5143\u7d20\u7684\u6dfb\u52a0\u548c\u5220\u9664\uff0c\u800c\u961f\u5217\u5219\u53ef\u4ee5\u4f7f\u7528append()\u548cpopleft()\u65b9\u6cd5\uff08\u5f53\u4f7f\u7528deque\u65f6\uff09\u6765\u5b9e\u73b0\u3002\u5bf9\u4e8e\u6808\u548c\u961f\u5217\u7684\u8f6c\u6362\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u961f\u5217\u4e2d\u7684\u5143\u7d20\u9010\u4e2a\u5f39\u51fa\u5e76\u538b\u5165\u6808\u4e2d\uff0c\u6216\u76f8\u53cd\u7684\u65b9\u5f0f\u8fdb\u884c\u3002<\/p>\n<p><strong>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6808\u548c\u961f\u5217\u5206\u522b\u9002\u5408\u5904\u7406\u54ea\u4e9b\u7c7b\u578b\u7684\u95ee\u9898\uff1f<\/strong><br \/>\u6808\u901a\u5e38\u7528\u4e8e\u9700\u8981\u540e\u8fdb\u5148\u51fa\uff08LIFO\uff09\u903b\u8f91\u7684\u95ee\u9898\uff0c\u6bd4\u5982\u51fd\u6570\u8c03\u7528\u7ba1\u7406\u3001\u8868\u8fbe\u5f0f\u6c42\u503c\u548c\u64a4\u9500\u64cd\u4f5c\u7b49\u3002\u961f\u5217\u5219\u9002\u5408\u5904\u7406\u5148\u8fdb\u5148\u51fa\uff08FIFO\uff09\u903b\u8f91\u7684\u573a\u666f\uff0c\u5982\u4efb\u52a1\u8c03\u5ea6\u3001\u6d88\u606f\u4f20\u9012\u548c\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u7b49\u3002\u6839\u636e\u4e0d\u540c\u7684\u95ee\u9898\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u80fd\u591f\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<p><strong>\u5728\u8f6c\u6362\u6808\u548c\u961f\u5217\u65f6\uff0c\u6709\u54ea\u4e9b\u6027\u80fd\u8003\u8651\u9700\u8981\u6ce8\u610f\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u6808\u548c\u961f\u5217\u7684\u8f6c\u6362\u65f6\uff0c\u6027\u80fd\u4e3b\u8981\u53d6\u51b3\u4e8e\u5b9e\u73b0\u7684\u65b9\u5f0f\u3002\u82e5\u4f7f\u7528\u5217\u8868\uff0c\u9891\u7e41\u7684\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d\uff0c\u56e0\u4e3a\u5217\u8868\u5728\u8fd9\u65b9\u9762\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0cdeque\u63d0\u4f9b\u4e86\u66f4\u9ad8\u6548\u7684\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(1)\u3002\u56e0\u6b64\uff0c\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u80fd\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6808\u548c\u961f\u5217\u7684\u8f6c\u6362\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684\u6570\u636e\u7ed3\u6784\u5217\u8868\uff08list\uff09\u6765\u5b9e\u73b0\u3002\u6808\u548c\u961f\u5217\u7684\u64cd\u4f5c\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u5165 [&hellip;]","protected":false},"author":3,"featured_media":1062425,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1062413"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1062413"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1062413\/revisions"}],"predecessor-version":[{"id":1062429,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1062413\/revisions\/1062429"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1062425"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1062413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1062413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1062413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}