{"id":180607,"date":"2023-03-04T14:07:21","date_gmt":"2023-03-04T06:07:21","guid":{"rendered":"http:\/\/www.idc.net\/help\/180607\/"},"modified":"2023-03-04T14:07:21","modified_gmt":"2023-03-04T06:07:21","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91python%e6%95%99%e7%a8%8b%ef%bc%9apython%e6%80%8e%e4%b9%88%e5%ae%9e%e7%8e%b0%e9%93%be%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/180607\/","title":{"rendered":"\u540e\u6d6a\u4e91Python\u6559\u7a0b\uff1apython\u600e\u4e48\u5b9e\u73b0\u94fe\u8868"},"content":{"rendered":"<p><strong><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/article\/000\/000\/003\/5dd8a46a2b6e6881.jpg\" class=\"aligncenter\"><\/strong><\/p>\n<p><strong>\u4e00\u3001\u6982\u5ff5\u68b3\u7406<\/strong><\/p>\n<p>\u94fe\u8868\u662f\u8ba1\u7b97\u673a\u79d1\u5b66\u91cc\u9762\u5e94\u7528\u5e94\u7528\u6700\u5e7f\u6cdb\u7684\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\u3002\u5b83\u662f\u6700\u7b80\u5355\u7684\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\uff0c\u540c\u65f6\u4e5f\u662f\u6bd4\u8f83\u9ad8\u9636\u7684\u6570\u636e\u7ed3\u6784\uff08\u4f8b\u5982\u68e7\u3001\u73af\u5f62\u7f13\u51b2\u548c\u961f\u5217\uff09<\/p>\n<p>\u7b80\u5355\u7684\u8bf4\uff0c\u4e00\u4e2a\u5217\u8868\u5c31\u662f\u5355\u6570\u636e\u901a\u8fc7\u7d22\u5f15\u96c6\u5408\u5728\u4e00\u8d77\u3002\u5728C\u91cc\u9762\u8fd9\u53eb\u505a\u6307\u9488\u3002\u6bd4\u65b9\u8bf4\uff0c\u4e00\u4e2a\u6570\u636e\u5143\u7d20\u53ef\u4ee5\u7531\u5730\u5740\u5143\u7d20\uff0c\u5730\u7406\u5143\u7d20\u3001\u8def\u7531\u4fe1\u606f\u6d3b\u7740\u4ea4\u6613\u7ec6\u8282\u7b49\u7b49\u7ec4\u6210\u3002\u4f46\u662f\u94fe\u8868\u91cc\u9762\u7684\u5143\u7d20\u7c7b\u578b\u90fd\u662f\u4e00\u6837\u7684\uff0c\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u5217\u8868\u3002<\/p>\n<p>\u4e00\u4e2a\u5355\u72ec\u7684\u5217\u8868\u5143\u7d20\u53eb\u505a\u4e00\u4e2a\u8282\u70b9\u3002\u8fd9\u4e9b\u8282\u70b9\u4e0d\u50cf\u6570\u7ec4\u4e00\u6837\u90fd\u6309\u987a\u5e8f\u5b58\u50a8\u5728\u5185\u5b58\u5f53\u4e2d\uff0c\u76f8\u53cd\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u8282\u70b9\u6307\u5411\u53e6\u5916\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488\u5728\u5185\u5b58\u4e0d\u540c\u7684\u5730\u65b9\u627e\u5230\u8fd9\u4e9b\u5143\u7d20\u3002\u5217\u8868\u6700\u540e\u4e00\u9879\u4e60\u60ef\u7528NIL\u8868\u793a\uff0c\u76f8\u5f53\u4e8epython\u91cc\u9762\u7684None\u3002<\/p>\n<p>\u8fd9\u91cc\u4ecb\u7ecd\u4e24\u79cd\u4e0d\u540c\u7684\u5217\u8868\u2014\u2014\u5355\u94fe\u8868\u548c\u53cc\u94fe\u8868\u3002\u53cc\u94fe\u8868\u4e2d\u7684\u67d0\u4e2a\u8282\u70b9\u53ea\u4f1a\u6307\u5411\u5217\u8868\u4e2d\u7684\u4e0b\u4e00\u4e2a\u5143\u7d20\uff0c\u4f46\u662f\u5728\u53cc\u94fe\u8868\u91cc\u9762\uff0c\u5f53\u524d\u8282\u70b9\u540c\u65f6\u4e5f\u4f1a\u6307\u5411\u524d\u4e00\u4e2a\u8282\u70b9\u3002\u6240\u4ee5\u53cc\u94fe\u8868\u4f1a\u5360\u7528\u66f4\u591a\u7684\u5185\u5b58\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u989d\u5916\u7684\u53d8\u91cf\u53bb\u5b58\u50a8\u7d22\u5f15\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/119\/443\/686\/1574478311665280.png\" class=\"aligncenter\"><\/p>\n<p>\u5355\u94fe\u8868\u53ef\u4ee5\u4ece\u5934\u5230\u5c3e\u987a\u5e8f\u67e5\u8be2\uff0c\u4f46\u662f\u53cd\u8fc7\u6765\u5c31\u4e0d\u662f\u90a3\u4e48\u5bb9\u6613\u4e86\u3002\u7136\u800c\uff0c\u53cc\u94fe\u8868\u4e0d\u7ba1\u4f60\u662f\u4ece\u54ea\u4e2a\u8282\u70b9\u5f00\u59cb\uff0c\u4ece\u4efb\u610f\u65b9\u5411\u67e5\u8be2\u90fd\u662f\u4e00\u6837\u7684\u3002\u5728\u5355\u94fe\u8868\u4e2d\u589e\u52a0\u548c\u5220\u9664\u8282\u70b9\u53ea\u9700\u8981\u4e24\u6b65\uff0c\u4f46\u662f\u5728\u53cc\u94fe\u8868\u91cc\u5c31\u9700\u8981\u56db\u6b65\u4e86\u3002<\/p>\n<p>\u4f46\u662f\u5728python\u91cc\u9762\u6ca1\u6709\u63d0\u4f9b\u50cf\u53cc\u94fe\u8868\u4e00\u6837\u7684\u6570\u636e\u7ed3\u6784\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u81ea\u5df1\u521b\u5efa\u4e00\u4e2a\u8fd9\u6837\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<p><strong>\u4e8c\u3001\u5982\u679c\u4f7f\u7528python\u521b\u5efa\u94fe\u8868<\/strong><\/p>\n<p>\uff081\uff09\u5c06\u8282\u70b9\u5b9a\u4e49\u6210\u4e00\u4e2a\u6570\u636e\u7ed3\u6784<\/p>\n<p>\u9996\u5148\u6211\u4eec\u5c06\u8282\u70b9\u7c7b\u5b9a\u4e49\u6210ListNode\uff0c\u8be5\u7c7b\u5728\u521d\u59cb\u5316\u5b9e\u4f8b\u5bf9\u8c61\u65f6\uff0c\u5b9a\u4e49\u4e86\u4e24\u4e2a\u5b9e\u4f8b\u53d8\u91cf\uff0c\u5176\u4e2ddata\u7528\u6765\u5b58\u50a8\u8282\u70b9\u7684\u503c\uff0cnext\u7528\u6765\u5b58\u50a8\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u7d22\u5f15\uff0c\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u4e00\u4e0b\u4e00\u4e2a\u8282\u70b9\u8981\u5b9a\u4e49\u7684\u65b9\u6cd5\u548c\u5c5e\u6027<\/p>\n<pre>__init__()\uff1a\u521d\u59cb\u5316\u8282\u70b9\nself.data\uff1a\u5b58\u50a8\u8282\u70b9\u7684\u503c\nself.next\uff1a\u5b58\u50a8\u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u7d22\u5f15\nhas_value()\uff1a\u5c06\u5f53\u524d\u8282\u70b9\u503c\u548c\u5176\u4ed6\u7684\u503c\u6bd4\u8f83<\/pre>\n<p>\u4e0a\u9762\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u6db5\u76d6\u4e86\u4e00\u4e2a\u8282\u70b9\u5e94\u6709\u7684\u57fa\u672c\u5c5e\u6027\u548c\u884c\u4e3a<\/p>\n<p>Listing1:The ListNode class<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/789\/312\/332\/1574479062217105.jpg\" class=\"aligncenter\"><\/p>\n<p>\u4e0a\u9762\u521b\u5efa\u4e86\u6700\u7b80\u5355\u7684\u8282\u70b9\u7c7b\uff0c\u4e0b\u9762\u521d\u59cb\u5316ListNode\u7684\u5bf9\u8c61<\/p>\n<p>Listing2:\u521d\u59cb\u5316\u8282\u70b9<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/421\/996\/678\/1574478388675725.png\" class=\"aligncenter\"><\/p>\n<p>\u4e0a\u9762\u521b\u5efa\u4e86\u4e09\u4e2a\u72ec\u7acb\u7684\u8282\u70b9<\/p>\n<p>\uff082\uff09\u521b\u5efa\u4e00\u4e2a\u5355\u94fe\u8868\u7c7b<\/p>\n<p>\u73b0\u5728\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3aSingleLinkedList\u7684\u7c7b\u53bb\u7ba1\u7406\u6211\u4eec\u7684\u8282\u70b9\uff0c\u5b83\u5305\u542b\u4e86\u4e0b\u9762\u8fd9\u4e9b\u65b9\u6cd5\uff1a<\/p>\n<pre>__init__()\uff1a\u521d\u59cb\u5316\u5bf9\u8c61\nlist_length()\uff1a\u8fd4\u56de\u8282\u70b9\u6570\u91cf\noutput_list():\u8f93\u51fa\u8282\u70b9\u503c\nadd_list_item():\u5728\u5217\u8868\u672b\u5c3e\u589e\u52a0\u4e00\u4e2a\u65b0\u7684\u8282\u70b9\nunordered_search():\u6839\u636e\u4e00\u4e2a\u7279\u6b8a\u503c\u53bb\u67e5\u8be2\u5217\u8868\nremove_list_item_by_id()\uff1a\u6839\u636e\u8282\u70b9id\u79fb\u9664\u8282\u70b9<\/pre>\n<p>\u4e0b\u9762\u4e00\u4e00\u8bb2\u89e3\u8fd9\u4e9b\u65b9\u6cd5<\/p>\n<p>__init__()\u5b9a\u4e49\u4e86head\u548ctail\uff0c\u90fd\u521d\u59cb\u5316\u4e3aNone<\/p>\n<p>Listing3:The SingleLinkedList class(part one)<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/316\/515\/326\/1574478430564852.png\" class=\"aligncenter\"><\/p>\n<p>\uff083\uff09\u6dfb\u52a0\u8282\u70b9<\/p>\n<p>\u901a\u8fc7add_list_item()\u6dfb\u52a0\u5217\u8868\u5143\u7d20\u3002\u5148\u68c0\u6d4b\u662f\u4e0d\u662fListNode\u7684\u5b9e\u4f8b\uff0c\u5982\u679c\u4e0d\u662f\uff0c\u5c31\u65b0\u5efa\u4e00\u4e2a\u8282\u70b9\u3002\u5982\u679c\u5217\u8868\u8fd8\u662f\u7a7a\u7684\u8bdd\uff0c\u5c31\u628a\u8be5\u8282\u70b9\u5f53\u4f5c\u5934\u8282\u70b9\uff0c\u5982\u679c\u4e0d\u662f\u7a7a\uff0c\u5c31\u5c06\u5f53\u524d\u8282\u70b9\u6307\u5411\u4e0b\u4e00\u4e2a\u5143\u7d20\uff08\u4e5f\u5c31\u662f\u521a\u65b0\u6dfb\u52a0\u7684\u8282\u70b9\uff09\u3002\u628a\u65b0\u8282\u70b9\u6dfb\u52a0\u5230\u5217\u8868\u5f53\u4e2d<\/p>\n<p>Listing4:The SinglelinkedList class(part two)<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/871\/834\/507\/1574478435995439.png\" class=\"aligncenter\"><\/p>\n<p>list_length()\u65b9\u6cd5\u8ba1\u7b97\u8282\u70b9\u6570\u91cf\uff0c\u8fd4\u56de\u5217\u8868\u7684\u957f\u5ea6\u3002\u5728\u4e00\u4e2a\u5faa\u73af\u5f53\u4e2d\u5faa\u73af\u5217\u8868\uff0cself.next\u4f9d\u6b21\u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9<\/p>\n<p>Listing5:The SingleLinkedList class(part three)<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/331\/389\/225\/1574478678417074.png\" class=\"aligncenter\"><\/p>\n<p>output_list()\u7528\u6765\u8f93\u51fa\u65b0\u7684\u8282\u70b9\u503c<\/p>\n<p>Listing6:The SingleLinkedList class(part four)<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/257\/335\/773\/1574478690676531.png\" class=\"aligncenter\"><\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u521d\u59cb\u5316SingleLinkedList\u7684\u5b9e\u4f8btrack\uff0c\u7136\u540e\u521b\u5efa4\u4e2a\u8282\u70b9\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/986\/768\/626\/1574479098829507.jpg\" class=\"aligncenter\"><\/p>\n<p>\uff084\uff09\u67e5\u8be2\u5217\u8868<\/p>\n<p>\u67e5\u8be2\u6574\u4e2a\u5217\u8868\u4f7f\u7528unordered_search()\u3002\u5b83\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u989d\u5916\u7684\u53c2\u6570\u5e2e\u52a9\u67e5\u8be2\u3002\u5217\u8868\u7684\u5934\u662f\u5207\u5165\u70b9\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/357\/973\/869\/1574479122227493.jpg\" class=\"aligncenter\"><\/p>\n<p>\uff085\uff09\u4ece\u5217\u8868\u4e2d\u79fb\u9664\u4e00\u4e2a\u5143\u7d20<\/p>\n<p>\u4ece\u5217\u8868\u4e2d\u79fb\u9664\u4e00\u4e2a\u8282\u70b9 \u65f6\uff0c\u6307\u5411\u8be5\u8282\u70b9\u7d22\u5f15\u9700\u8981\u88ab\u79fb\u52a8\u5230\uff0c\u88ab\u79fb\u9664\u8282\u70b9\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\u3002\u88ab\u79fb\u9664\u7684\u8282\u70b9\u4f1a\u7531python\u7684\u5783\u573e\u56de\u6536\u673a\u5236\u6e05\u9664<\/p>\n<p>Listing10:Removing a node by node number<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/288\/229\/668\/1574479155804268.jpg\" class=\"aligncenter\"><\/p>\n<p>\uff086\uff09\u521b\u5efa\u4e00\u4e2a\u53cc\u94fe\u8868<\/p>\n<p>\u521b\u5efa\u53cc\u94fe\u8868\u5176\u5b9e\u5c31\u662f\u5728ListNode\u7684\u57fa\u7840\u4e0a\uff0c\u5728\u521b\u5efa\u4e00\u4e2aprevious\u7684\u5c5e\u6027<\/p>\n<p>Listing11\uff1aExtended list node class<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/526\/507\/736\/1574478804258636.png\" class=\"aligncenter\"><\/p>\n<p>\u7136\u540e\u6211\u4eec\u5c31\u53ef\u4ee5\u4f9d\u636e\u4e0a\u9762\u7684\u5b9a\u4e49\u65b0\u5efa\u4e00\u4e2a\u53cc\u94fe\u8868\u7c7b<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/902\/118\/927\/1574478815280654.png\" class=\"aligncenter\"><\/p>\n<p>\u6dfb\u52a0\u65b0\u7684\u8282\u70b9\u8ddf\u5355\u94fe\u8868\u6709\u6240\u4e0d\u540c<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/457\/628\/960\/1574478826852286.png\" class=\"aligncenter\"><\/p>\n<p>\u79fb\u9664\u53cc\u94fe\u8868\u4e2d\u7684\u8282\u70b9<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/283\/674\/441\/1574478836860110.png\" class=\"aligncenter\"><\/p>\n<p>python\u5b9e\u9645\u8fd0\u7528<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/902\/988\/731\/1574478846732952.png\" class=\"aligncenter\"><\/p>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/295\/425\/134\/1574478857840290.png\" class=\"aligncenter\"><\/p>\n<p>\uff087\uff09\u4f7f\u7528\u961f\u5217\u5b9e\u73b0\u53cc\u5411\u5217\u8868<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/972\/595\/789\/1574478868166697.png\" class=\"aligncenter\"><\/p>\n<p>python\u5b66\u4e60\u7f51\uff0c\u514d\u8d39\u7684\u5728\u7ebf\u5b66\u4e60python\u5e73\u53f0\uff0c\u6b22\u8fce\u5173\u6ce8\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u6982\u5ff5\u68b3\u7406 \u94fe\u8868\u662f\u8ba1\u7b97\u673a\u79d1\u5b66\u91cc\u9762\u5e94\u7528\u5e94\u7528\u6700\u5e7f\u6cdb\u7684\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\u3002\u5b83\u662f\u6700\u7b80\u5355\u7684\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\uff0c\u540c\u65f6\u4e5f\u662f\u6bd4\u8f83\u9ad8\u9636\u7684\u6570 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":180608,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182397],"tags":[],"class_list":["post-180607","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/180607","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=180607"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/180607\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/180608"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=180607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=180607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=180607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}