{"id":2667,"date":"2022-02-22T15:30:13","date_gmt":"2022-02-22T07:30:13","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2667"},"modified":"2022-03-10T19:54:19","modified_gmt":"2022-03-10T11:54:19","slug":"python3heapq","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/02\/22\/python3heapq\/","title":{"rendered":"python3\u4e2d\u7684heapq \u5806"},"content":{"rendered":"\n<p>heapq\u5b9e\u73b0\u4e86\u4e00\u4e2a\u9002\u5408\u4e0ePython\u7684\u5217\u8868\u4e00\u8d77\u4f7f\u7528\u7684\u6700\u5c0f\u5806\u6392\u5e8f\u7b97\u6cd5\u3002<\/p>\n\n\n\n<h3><strong>\u6ee1\u4e8c\u53c9\u6811<\/strong><\/h3>\n\n\n\n<p>\u6811\u4e2d\u9664\u4e86\u53f6\u5b50\u8282\u70b9\uff0c\u6bcf\u4e2a\u8282\u70b9\u90fd\u6709\u4e24\u4e2a\u5b50\u8282\u70b9<\/p>\n\n\n\n<h3><strong>\u5b8c\u5168\u4e8c\u53c9\u6811<\/strong><\/h3>\n\n\n\n<p>\u5728\u6ee1\u8db3\u6ee1\u4e8c\u53c9\u6811\u7684\u6027\u8d28\u540e\uff0c\u6700\u540e\u4e00\u5c42\u7684\u53f6\u5b50\u8282\u70b9\u5747\u9700\u5728\u6700\u5de6\u8fb9<\/p>\n\n\n\n<h2>\u5806<\/h2>\n\n\n\n<p>\u5806\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u662f\u4e00\u9897\u5b8c\u5168\u4e8c\u53c9\u6811\u3002\u6700\u5c0f\u5806\u5219\u662f\u5728\u5806\u7684\u57fa\u7840\u589e\u52a0\u4e86\u65b0\u7684\u89c4\u5219\uff0c\u5b83\u7684\u6839\u7ed3\u70b9\u7684\u503c\u662f\u6700\u5c0f\u7684\uff0c\u800c\u4e14\u5b83\u7684\u4efb\u610f\u7ed3\u70b9\u7684\u7236\u7ed3\u70b9\u7684\u503c\u90fd\u5c0f\u4e8e\u6216\u8005\u7b49\u4e8e\u5176\u5de6\u53f3\u7ed3\u70b9\u7684\u503c\u3002\u56e0\u4e3a\u4e8c\u8fdb\u5236\u5806\u53ef\u4ee5\u4f7f\u7528\u6709\u7ec4\u7ec7\u7684\u5217\u8868\u6216\u6570\u7ec4\u6765\u8868\u793a\uff0c\u6240\u4ee5\u5143\u7d20N\u7684\u5b50\u5143\u7d20\u4f4d\u4e8e\u4f4d\u7f6e2 * N + 1\u548c2 * N + 2\u3002\u8fd9\u79cd\u5e03\u5c40\u4f7f\u91cd\u65b0\u5b89\u6392\u5806\u6210\u4e3a\u53ef\u80fd\uff0c\u56e0\u6b64\u5728\u6dfb\u52a0\u6216\u5220\u9664\u9879\u65f6\u4e0d\u9700\u8981\u91cd\u65b0\u5206\u914d\u90a3\u4e48\u591a\u5185\u5b58<br>\u533a\u5206\u5806(heap)\u4e0e\u6808(stack)\uff1a\u5806\u4e0e\u4e8c\u53c9\u6811\u6709\u5173\uff0c\u50cf\u4e00\u5806\u91d1\u5b57\u5854\u578b\u6ce5\u6c99\uff1b\u800c\u6808\u50cf\u4e00\u4e2a\u76f4\u7acb\u5783\u573e\u6876\uff0c\u4e00\u5217\u4e0b\u6765\u3002<\/p>\n\n\n\n<h3><strong>\u6700\u5927\u5806<\/strong><\/h3>\n\n\n\n<p>\u6700\u5927\u5806\u786e\u4fdd\u7236\u5806\u5927\u4e8e\u6216\u7b49\u4e8e\u5b83\u7684\u4e24\u4e2a\u5b50\u5806\u3002<\/p>\n\n\n\n<h3><strong>\u6700\u5c0f\u5806<\/strong><\/h3>\n\n\n\n<p>\u5efa\u5806\uff1a<\/p>\n\n\n\n<p> \u00a0<a href=\"https:\/\/docs.python.org\/zh-cn\/3\/library\/heapq.html#heapq.heapify\"><code>heapify()<\/code><\/a> <\/p>\n\n\n\n<p>\u6700\u5c0f\u5806\u8981\u6c42\u7236\u5806\u5c0f\u4e8e\u6216\u7b49\u4e8e\u5176\u5b50\u5806\u3002Python\u7684heapq\u6a21\u5757\u5b9e\u73b0\u4e86\u4e00\u4e2a\u6700\u5c0f\u5806\u3002<\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\">\u8981\u521b\u5efa\u4e00\u4e2a\u5806\uff0c\u53ef\u4ee5\u4f7f\u7528list\u6765\u521d\u59cb\u5316\u4e3a&nbsp;<code>[]<\/code>&nbsp;\uff0c\u6216\u8005\u4f60\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u51fd\u6570&nbsp;<a href=\"https:\/\/docs.python.org\/zh-cn\/3\/library\/heapq.html#heapq.heapify\"><code>heapify()<\/code><\/a>&nbsp;\uff0c\u6765\u628a\u4e00\u4e2alist\u8f6c\u6362\u6210\u5806\u3002<\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\">\u5b9a\u4e49\u4e86\u4ee5\u4e0b\u51fd\u6570\uff1a<code>heapq.<\/code><code>heappush<\/code>(<em>heap<\/em>,&nbsp;<em>item<\/em>)<a href=\"https:\/\/docs.python.org\/zh-cn\/3\/library\/heapq.html#heapq.heappush\"><\/a><\/p>\n\n\n\n<p>\u5c06&nbsp;<em>item<\/em>&nbsp;\u7684\u503c\u52a0\u5165&nbsp;<em>heap<\/em>&nbsp;\u4e2d\uff0c\u4fdd\u6301\u5806\u7684\u4e0d\u53d8\u6027\u3002<\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\"><code>heapq.heappop<\/code>(<em>heap<\/em>)<a href=\"https:\/\/docs.python.org\/zh-cn\/3\/library\/heapq.html#heapq.heappop\"><\/a><\/p>\n\n\n\n<p>\u5f39\u51fa\u5e76\u8fd4\u56de&nbsp;<em>heap<\/em>&nbsp;\u7684\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u4fdd\u6301\u5806\u7684\u4e0d\u53d8\u6027\u3002\u5982\u679c\u5806\u4e3a\u7a7a\uff0c\u629b\u51fa&nbsp;<a href=\"https:\/\/docs.python.org\/zh-cn\/3\/library\/exceptions.html#IndexError\"><code>IndexError<\/code><\/a>&nbsp;\u3002\u4f7f\u7528&nbsp;<code>heap[0]<\/code>&nbsp;\uff0c\u53ef\u4ee5\u53ea\u8bbf\u95ee\u6700\u5c0f\u7684\u5143\u7d20\u800c\u4e0d\u5f39\u51fa\u5b83\u3002<\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\"><code>heapq.heappushpop<\/code>(<em>heap<\/em>,&nbsp;<em>item<\/em>)<a href=\"https:\/\/docs.python.org\/zh-cn\/3\/library\/heapq.html#heapq.heappushpop\"><\/a><\/p>\n\n\n\n<p>\u5c06&nbsp;<em>item<\/em>&nbsp;\u653e\u5165\u5806\u4e2d\uff0c\u7136\u540e\u5f39\u51fa\u5e76\u8fd4\u56de&nbsp;<em>heap<\/em>&nbsp;\u7684\u6700\u5c0f\u5143\u7d20\u3002\u8be5\u7ec4\u5408\u64cd\u4f5c\u6bd4\u5148\u8c03\u7528&nbsp;<a href=\"https:\/\/docs.python.org\/zh-cn\/3\/library\/heapq.html#heapq.heappush\"><code>heappush()<\/code><\/a>&nbsp;\u518d\u8c03\u7528&nbsp;<a href=\"https:\/\/docs.python.org\/zh-cn\/3\/library\/heapq.html#heapq.heappop\"><code>heappop()<\/code><\/a>&nbsp;\u8fd0\u884c\u8d77\u6765\u66f4\u6709\u6548\u7387\u3002<\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\"><code>heapq.heapify<\/code>(<em>x<\/em>)<a href=\"https:\/\/docs.python.org\/zh-cn\/3\/library\/heapq.html#heapq.heapify\"><\/a><\/p>\n\n\n\n<p>\u5c06list&nbsp;<em>x<\/em>&nbsp;\u8f6c\u6362\u6210\u5806\uff0c\u539f\u5730\uff0c\u7ebf\u6027\u65f6\u95f4\u5185\u3002<code>heapq.<\/code><code>heapreplace<\/code>(<em>heap<\/em>,&nbsp;<em>item<\/em>)<a href=\"https:\/\/docs.python.org\/zh-cn\/3\/library\/heapq.html#heapq.heapreplace\"><\/a><\/p>\n\n\n\n<p>\u5f39\u51fa\u5e76\u8fd4\u56de&nbsp;<em>heap<\/em>&nbsp;\u4e2d\u6700\u5c0f\u7684\u4e00\u9879\uff0c\u540c\u65f6\u63a8\u5165\u65b0\u7684&nbsp;<em>item<\/em>\u3002 \u5806\u7684\u5927\u5c0f\u4e0d\u53d8\u3002 \u5982\u679c\u5806\u4e3a\u7a7a\u5219\u5f15\u53d1&nbsp;<a href=\"https:\/\/docs.python.org\/zh-cn\/3\/library\/exceptions.html#IndexError\"><code>IndexError<\/code><\/a>\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u5355\u6b65\u9aa4\u64cd\u4f5c\u6bd4&nbsp;<a href=\"https:\/\/docs.python.org\/zh-cn\/3\/library\/heapq.html#heapq.heappop\"><code>heappop()<\/code><\/a>&nbsp;\u52a0&nbsp;<a href=\"https:\/\/docs.python.org\/zh-cn\/3\/library\/heapq.html#heapq.heappush\"><code>heappush()<\/code><\/a>&nbsp;\u66f4\u9ad8\u6548\uff0c\u5e76\u4e14\u5728\u4f7f\u7528\u56fa\u5b9a\u5927\u5c0f\u7684\u5806\u65f6\u66f4\u4e3a\u9002\u5b9c\u3002 pop\/push \u7ec4\u5408\u603b\u662f\u4f1a\u4ece\u5806\u4e2d\u8fd4\u56de\u4e00\u4e2a\u5143\u7d20\u5e76\u5c06\u5176\u66ff\u6362\u4e3a&nbsp;<em>item<\/em>\u3002<\/p>\n\n\n\n<p>\u8fd4\u56de\u7684\u503c\u53ef\u80fd\u4f1a\u6bd4\u6dfb\u52a0\u7684&nbsp;<em>item<\/em>&nbsp;\u66f4\u5927\u3002 \u5982\u679c\u4e0d\u5e0c\u671b\u5982\u6b64\uff0c\u53ef\u8003\u8651\u6539\u7528&nbsp;<a href=\"https:\/\/docs.python.org\/zh-cn\/3\/library\/heapq.html#heapq.heappushpop\"><code>heappushpop()<\/code><\/a>\u3002 \u5b83\u7684 push\/pop \u7ec4\u5408\u4f1a\u8fd4\u56de\u4e24\u4e2a\u503c\u4e2d\u8f83\u5c0f\u7684\u4e00\u4e2a\uff0c\u5c06\u8f83\u5927\u7684\u503c\u7559\u5728\u5806\u4e2d\u3002<\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\"><code>heapq.<\/code><code>nlargest<\/code>(<em>n<\/em>,&nbsp;<em>iterable<\/em>,&nbsp;<em>key=None<\/em>)<a href=\"https:\/\/docs.python.org\/zh-cn\/3\/library\/heapq.html#heapq.nlargest\"><\/a><\/p>\n\n\n\n<p>\u4ece&nbsp;<em>iterable<\/em>&nbsp;\u6240\u5b9a\u4e49\u7684\u6570\u636e\u96c6\u4e2d\u8fd4\u56de\u524d&nbsp;<em>n<\/em>&nbsp;\u4e2a\u6700\u5927\u5143\u7d20\u7ec4\u6210\u7684\u5217\u8868\u3002 \u5982\u679c\u63d0\u4f9b\u4e86&nbsp;<em>key<\/em>&nbsp;\u5219\u5176\u5e94\u6307\u5b9a\u4e00\u4e2a\u5355\u53c2\u6570\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u4ece&nbsp;<em>iterable<\/em>&nbsp;\u7684\u6bcf\u4e2a\u5143\u7d20\u4e2d\u63d0\u53d6\u6bd4\u8f83\u952e (\u4f8b\u5982&nbsp;<code>key=str.lower<\/code>)\u3002 \u7b49\u4ef7\u4e8e:&nbsp;<code>sorted(iterable,&nbsp;key=key,&nbsp;reverse=True)[:n]<\/code>\u3002<\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\"><code>heapq.nsmallest<\/code>(<em>n<\/em>,&nbsp;<em>iterable<\/em>,&nbsp;<em>key=None<\/em>)<a href=\"https:\/\/docs.python.org\/zh-cn\/3\/library\/heapq.html#heapq.nsmallest\"><\/a><\/p>\n\n\n\n<p>\u4ece&nbsp;<em>iterable<\/em>&nbsp;\u6240\u5b9a\u4e49\u7684\u6570\u636e\u96c6\u4e2d\u8fd4\u56de\u524d&nbsp;<em>n<\/em>&nbsp;\u4e2a\u6700\u5c0f\u5143\u7d20\u7ec4\u6210\u7684\u5217\u8868\u3002 \u5982\u679c\u63d0\u4f9b\u4e86&nbsp;<em>key<\/em>&nbsp;\u5219\u5176\u5e94\u6307\u5b9a\u4e00\u4e2a\u5355\u53c2\u6570\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u4ece&nbsp;<em>iterable<\/em>&nbsp;\u7684\u6bcf\u4e2a\u5143\u7d20\u4e2d\u63d0\u53d6\u6bd4\u8f83\u952e (\u4f8b\u5982&nbsp;<code>key=str.lower<\/code>)\u3002 \u7b49\u4ef7\u4e8e:&nbsp;<code>sorted(iterable,&nbsp;key=key)[:n]<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>heapq\u5b9e\u73b0\u4e86\u4e00\u4e2a\u9002\u5408\u4e0ePython\u7684\u5217\u8868\u4e00\u8d77\u4f7f\u7528\u7684\u6700\u5c0f\u5806\u6392\u5e8f\u7b97\u6cd5\u3002 \u6ee1\u4e8c\u53c9\u6811 \u6811\u4e2d\u9664\u4e86\u53f6\u5b50\u8282\u70b9\uff0c\u6bcf\u4e2a\u8282\u70b9\u90fd &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/02\/22\/python3heapq\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">python3\u4e2d\u7684heapq \u5806<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[11],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2667"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2667"}],"version-history":[{"count":12,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2667\/revisions"}],"predecessor-version":[{"id":2866,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2667\/revisions\/2866"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2667"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}