{"id":990023,"date":"2024-12-27T08:16:27","date_gmt":"2024-12-27T00:16:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/990023.html"},"modified":"2024-12-27T08:16:30","modified_gmt":"2024-12-27T00:16:30","slug":"python%e5%a6%82%e4%bd%95%e6%b8%85%e7%a9%baqueue%e9%98%9f%e5%88%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/990023.html","title":{"rendered":"python\u5982\u4f55\u6e05\u7a7aqueue\u961f\u5217"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064937\/92dc1edb-0dcf-4f6a-afd4-25b2988d16ba.webp\" alt=\"python\u5982\u4f55\u6e05\u7a7aqueue\u961f\u5217\" \/><\/p>\n<p><p> <strong>\u8981\u6e05\u7a7aPython\u4e2d\u7684Queue\u961f\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u521b\u5efa\u4e00\u4e2a\u65b0\u961f\u5217\u3001\u4f7f\u7528\u5faa\u73af\u9010\u4e2a\u79fb\u9664\u5143\u7d20\u3001\u4f7f\u7528Queue\u7684<code>queue.clear()<\/code>\u65b9\u6cd5\uff08\u9488\u5bf9\u67d0\u4e9b\u7279\u5b9a\u5b9e\u73b0\uff09\u3002\u63a8\u8350\u4f7f\u7528\u5faa\u73af\u9010\u4e2a\u79fb\u9664\u5143\u7d20\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\u3002<\/strong>\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u521b\u5efa\u4e00\u4e2a\u65b0\u961f\u5217<\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\u76f4\u63a5\uff0c\u5373\u76f4\u63a5\u7528\u4e00\u4e2a\u65b0\u7684Queue\u5bf9\u8c61\u66ff\u6362\u539f\u6709\u7684Queue\u5bf9\u8c61\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u5728\u4ee3\u7801\u4e0a\u770b\u4f3c\u7b80\u5355\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u4e0d\u9002\u7528\uff0c\u7279\u522b\u662f\u5f53\u961f\u5217\u662f\u7531\u591a\u4e2a\u7ebf\u7a0b\u5171\u4eab\u65f6\u3002\u8fd9\u662f\u56e0\u4e3a\u7528\u65b0\u961f\u5217\u66ff\u6362\u65e7\u961f\u5217\u4f1a\u5bfc\u81f4\u5176\u4ed6\u7ebf\u7a0b\u5bf9\u65e7\u961f\u5217\u7684\u5f15\u7528\u5931\u6548\u3002\u56e0\u6b64\uff0c\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u9700\u8981\u683c\u5916\u6ce8\u610f\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import queue<\/p>\n<p>q = queue.Queue()<\/p>\n<h2><strong>\u5047\u8bbe\u961f\u5217\u4e2d\u5df2\u7ecf\u6709\u4e00\u4e9b\u5143\u7d20<\/strong><\/h2>\n<p>q.put(1)<\/p>\n<p>q.put(2)<\/p>\n<p>q.put(3)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u961f\u5217<\/strong><\/h2>\n<p>q = queue.Queue()  # \u8fd9\u6837\u539f\u961f\u5217\u4e2d\u7684\u5143\u7d20\u5c31\u88ab\u6e05\u7a7a\u4e86<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5faa\u73af\u9010\u4e2a\u79fb\u9664\u5143\u7d20<\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u591a\u79cd\u573a\u5408\uff0c\u4e5f\u662f\u6700\u4e3a\u666e\u904d\u5b89\u5168\u7684\u4e00\u79cd\u65b9\u6cd5\u3002\u901a\u8fc7\u5728\u4e00\u4e2a\u5faa\u73af\u4e2d\u8c03\u7528<code>get()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9010\u4e2a\u79fb\u9664Queue\u4e2d\u7684\u5143\u7d20\u3002\u65e0\u8bba\u961f\u5217\u7684\u5b9e\u73b0\u7ec6\u8282\u5982\u4f55\uff0c\u8fd9\u79cd\u65b9\u6cd5\u90fd\u53ef\u4ee5\u786e\u4fdd\u961f\u5217\u88ab\u6e05\u7a7a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import queue<\/p>\n<p>q = queue.Queue()<\/p>\n<h2><strong>\u5047\u8bbe\u961f\u5217\u4e2d\u5df2\u7ecf\u6709\u4e00\u4e9b\u5143\u7d20<\/strong><\/h2>\n<p>q.put(1)<\/p>\n<p>q.put(2)<\/p>\n<p>q.put(3)<\/p>\n<h2><strong>\u5faa\u73af\u6e05\u7a7a\u961f\u5217<\/strong><\/h2>\n<p>while not q.empty():<\/p>\n<p>    q.get()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u5b83\u53ef\u4ee5\u786e\u4fdd\u5728\u4efb\u4f55\u65f6\u5019\uff0c\u961f\u5217\u7684\u72b6\u6001\u90fd\u662f\u4e00\u81f4\u7684\uff0c\u5e76\u4e14\u4e0d\u4f1a\u56e0\u4e3a\u521b\u5efa\u65b0\u5bf9\u8c61\u800c\u5bfc\u81f4\u5185\u5b58\u95ee\u9898\u6216\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528Queue\u7684<code>queue.clear()<\/code>\u65b9\u6cd5\uff08\u9488\u5bf9\u67d0\u4e9b\u7279\u5b9a\u5b9e\u73b0\uff09<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u7279\u5b9a\u7684Queue\u5b9e\u73b0\u4e2d\uff0c\u53ef\u80fd\u4f1a\u63d0\u4f9b\u4e00\u4e2a<code>clear()<\/code>\u65b9\u6cd5\u6765\u76f4\u63a5\u6e05\u7a7a\u961f\u5217\u3002\u4e0d\u8fc7\uff0c\u8fd9\u5e76\u4e0d\u662fPython\u6807\u51c6\u5e93\u4e2dQueue\u7c7b\u7684\u7279\u6027\uff0c\u800c\u662f\u67d0\u4e9b\u7b2c\u4e09\u65b9\u5e93\u53ef\u80fd\u5b9e\u73b0\u7684\u529f\u80fd\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u524d\uff0c\u9700\u8981\u786e\u8ba4\u6240\u4f7f\u7528\u7684Queue\u5bf9\u8c61\u662f\u5426\u652f\u6301<code>clear()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8fd9\u79cd\u65b9\u6cd5\u5728\u6807\u51c6\u5e93Queue\u4e2d\u5e76\u4e0d\u53ef\u7528<\/p>\n<p>q = queue.Queue()<\/p>\n<h2><strong>\u5047\u8bbe\u961f\u5217\u4e2d\u5df2\u7ecf\u6709\u4e00\u4e9b\u5143\u7d20<\/strong><\/h2>\n<p>q.put(1)<\/p>\n<p>q.put(2)<\/p>\n<p>q.put(3)<\/p>\n<h2><strong>\u5982\u679c\u961f\u5217\u5b9e\u73b0\u63d0\u4f9b\u4e86clear\u65b9\u6cd5<\/strong><\/h2>\n<h2><strong>q.clear()  # \u76f4\u63a5\u6e05\u7a7a\u961f\u5217<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u6ce8\u610f\u4e8b\u9879\u548c\u4f7f\u7528\u573a\u666f<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7ebf\u7a0b\u5b89\u5168<\/strong>\uff1a\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u64cd\u4f5c\u961f\u5217\u65f6\uff0c\u5fc5\u987b\u786e\u4fdd\u64cd\u4f5c\u7684\u7ebf\u7a0b\u5b89\u5168\u6027\u3002Python\u7684Queue\u6a21\u5757\u672c\u8eab\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u4f46\u5728\u6e05\u7a7a\u961f\u5217\u65f6\uff0c\u4f7f\u7528\u65b0\u961f\u5217\u66ff\u6362\u65e7\u961f\u5217\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\u8003\u8651<\/strong>\uff1a\u4f7f\u7528\u5faa\u73af\u9010\u4e2a\u79fb\u9664\u5143\u7d20\u7684\u65b9\u6cd5\u53ef\u80fd\u4f1a\u5728\u961f\u5217\u7279\u522b\u5927\u65f6\u5f71\u54cd\u6027\u80fd\uff0c\u4f46\u5176\u4f18\u70b9\u662f\u517c\u5bb9\u6027\u597d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7279\u5b9a\u5b9e\u73b0<\/strong>\uff1a\u5982\u679c\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u7684Queue\u5b9e\u73b0\uff0c\u5efa\u8bae\u67e5\u9605\u6587\u6863\u4ee5\u786e\u5b9a\u662f\u5426\u6709\u7279\u6b8a\u7684\u6e05\u7a7a\u65b9\u6cd5\uff0c\u5e76\u4e86\u89e3\u5176\u5b9e\u73b0\u673a\u5236\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9002\u7528\u573a\u5408<\/strong>\uff1a\u5728\u9700\u8981\u9891\u7e41\u6e05\u7a7a\u961f\u5217\u7684\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u5c24\u4e3a\u91cd\u8981\u3002\u6bd4\u5982\u5728\u9ad8\u5e76\u53d1\u7684\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u5efa\u8bae\u4f7f\u7528\u5faa\u73af\u9010\u4e2a\u79fb\u9664\u7684\u65b9\u6cd5\uff0c\u4ee5\u907f\u514d\u5bf9\u961f\u5217\u7684\u5176\u4ed6\u5f15\u7528\u4ea7\u751f\u5f71\u54cd\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e86\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6ce8\u610f\u4e8b\u9879\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u961f\u5217\u6e05\u7a7a\u65b9\u5f0f\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6709\u6548\u6e05\u7a7a\u4e00\u4e2a\u961f\u5217\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6e05\u7a7a\u4e00\u4e2a\u961f\u5217\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>queue<\/code>\u6a21\u5757\u4e2d\u7684<code>Queue<\/code>\u7c7b\u3002\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u8c03\u7528<code>get()<\/code>\u65b9\u6cd5\uff0c\u76f4\u5230\u961f\u5217\u4e3a\u7a7a\uff0c\u6216\u8005\u76f4\u63a5\u4f7f\u7528<code>queue.queue.clear()<\/code>\u65b9\u6cd5\u6765\u5feb\u901f\u6e05\u7a7a\u961f\u5217\u3002\u6b64\u5916\uff0c\u82e5\u60a8\u4f7f\u7528\u7684\u662f\u5217\u8868\u5b9e\u73b0\u7684\u961f\u5217\uff0c\u53ef\u4ee5\u901a\u8fc7\u8d4b\u503c\u7a7a\u5217\u8868<code>my_queue = []<\/code>\u6765\u6e05\u7a7a\u3002<\/p>\n<p><strong>\u6e05\u7a7a\u961f\u5217\u540e\u4f1a\u5f71\u54cd\u5230\u6b63\u5728\u4f7f\u7528\u8be5\u961f\u5217\u7684\u7ebf\u7a0b\u5417\uff1f<\/strong><br \/>\u5982\u679c\u961f\u5217\u662f\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u4f7f\u7528\u7684\uff0c\u6e05\u7a7a\u961f\u5217\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6b63\u5728\u7b49\u5f85\u961f\u5217\u7684\u7ebf\u7a0b\u51fa\u73b0\u5f02\u5e38\u3002\u4e3a\u4e86\u5b89\u5168\u5730\u6e05\u7a7a\u961f\u5217\uff0c\u5efa\u8bae\u5728\u6e05\u7a7a\u64cd\u4f5c\u524d\u5148\u9501\u5b9a\u961f\u5217\uff0c\u786e\u4fdd\u6ca1\u6709\u7ebf\u7a0b\u5728\u4f7f\u7528\u5b83\uff0c\u4ee5\u907f\u514d\u6f5c\u5728\u7684\u7ade\u4e89\u6761\u4ef6\u3002<\/p>\n<p><strong>\u4f7f\u7528<code>collections.deque<\/code>\u6e05\u7a7a\u961f\u5217\u7684\u6700\u4f73\u65b9\u5f0f\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u4f7f\u7528<code>collections.deque<\/code>\u4f5c\u4e3a\u961f\u5217\u5b9e\u73b0\uff0c\u6700\u7b80\u5355\u7684\u6e05\u7a7a\u65b9\u6cd5\u662f\u4f7f\u7528<code>clear()<\/code>\u65b9\u6cd5\u3002\u6b64\u65b9\u6cd5\u80fd\u591f\u9ad8\u6548\u5730\u5220\u9664\u6240\u6709\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)\u3002\u6b64\u5916\uff0c\u60a8\u4e5f\u53ef\u4ee5\u901a\u8fc7\u91cd\u65b0\u8d4b\u503c\u4e00\u4e2a\u7a7a\u7684<code>deque<\/code>\u5bf9\u8c61\u6765\u6e05\u7a7a\u961f\u5217\uff0c\u4f8b\u5982<code>my_deque = deque()<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u4e5f\u4f1a\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u6e05\u7a7aPython\u4e2d\u7684Queue\u961f\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u521b\u5efa\u4e00\u4e2a\u65b0\u961f\u5217\u3001\u4f7f\u7528\u5faa\u73af\u9010\u4e2a\u79fb\u9664\u5143\u7d20\u3001\u4f7f\u7528Queue\u7684 [&hellip;]","protected":false},"author":3,"featured_media":990029,"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\/990023"}],"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=990023"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/990023\/revisions"}],"predecessor-version":[{"id":990032,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/990023\/revisions\/990032"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/990029"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=990023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=990023"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=990023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}