{"id":205351,"date":"2025-05-29T13:51:08","date_gmt":"2025-05-29T05:51:08","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205351\/"},"modified":"2025-05-29T13:51:08","modified_gmt":"2025-05-29T05:51:08","slug":"python-%e5%85%83%e7%bb%84%e3%80%81%e9%9b%86%e5%90%88%e5%92%8c%e5%ad%97%e5%85%b8-f-daysofmiva-%e6%97%a5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205351\/","title":{"rendered":"Python \u5143\u7ec4\u3001\u96c6\u5408\u548c\u5b57\u5178 || #f #daysofMiva \u65e5"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u5143\u7ec4\u3001\u96c6\u5408\u548c\u5b57\u5178 || #f #daysofMiva \u65e5<\/h1>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6587\u7ae0\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300aPython \u5143\u7ec4\u3001\u96c6\u5408\u548c\u5b57\u5178 || #f #daysofMiva \u65e5\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p>\u4eca\u5929\u662f #100daysofmiva \u7684\u7b2c 5 \u5929 &#8211; \u8bf7\u53c2\u9605 github \u9879\u76ee<br \/> \u3002 <\/p>\n<p>\u6211\u6df1\u5165\u7814\u7a76\u4e86 python \u4e2d\u7684\u4e09\u79cd\u57fa\u672c\u6570\u636e\u7ed3\u6784\uff1a\u5143\u7ec4\u3001\u96c6\u5408\u548c\u5b57\u5178\u3002\u8fd9\u4e9b\u7ed3\u6784\u5bf9\u4e8e\u6709\u6548\u7ec4\u7ec7\u548c\u8bbf\u95ee\u6570\u636e\u81f3\u5173\u91cd\u8981\uff0c\u6bcf\u4e2a\u7ed3\u6784\u90fd\u5177\u6709\u72ec\u7279\u7684\u7279\u5f81\u548c\u7528\u4f8b\u3002\u8fd9\u662f\u6211\u6240\u5b66\u5230\u7684\u8be6\u7ec6\u62a5\u544a\uff0c\u5305\u62ec\u6d41\u7a0b\u3001\u6280\u672f\u7ec6\u8282\u548c\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p><strong>1.\u5143\u7ec4<\/strong><\/p>\n<p><strong>\u5b9a\u4e49\uff1a<\/strong> \u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\u3001\u6709\u5e8f\u7684\u5143\u7d20\u96c6\u5408\u3002\u5143\u7ec4\u4e0e\u5217\u8868\u7c7b\u4f3c\uff0c\u4f46\u4e3b\u8981\u533a\u522b\u5728\u4e8e\u5b83\u4eec\u5728\u521b\u5efa\u540e\u65e0\u6cd5\u4fee\u6539\u3002<\/p>\n<p><strong>\u7279\u70b9\uff1a<\/strong><\/p>\n<p><strong>\u4e0d\u53ef\u53d8\uff1a<\/strong> \u4e00\u65e6\u521b\u5efa\uff0c\u5143\u7ec4\u7684\u5143\u7d20\u5c31\u65e0\u6cd5\u66f4\u6539\uff08\u4e0d\u80fd\u5206\u914d\u3001\u6dfb\u52a0\u6216\u5220\u9664\u9879\u76ee\uff09\u3002<\/p>\n<p><strong>\u6709\u5e8f\uff1a<\/strong> \u5143\u7d20\u4fdd\u6301\u987a\u5e8f\uff0c\u5e76\u4e14\u652f\u6301\u7d22\u5f15\u3002<br \/> \u5f02\u6784\uff1a\u5143\u7ec4\u53ef\u4ee5\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>\u521b\u5efa\u5143\u7ec4<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u62ec\u53f7 () \u6216\u7b80\u5355\u5730\u901a\u8fc7\u7528\u9017\u53f7\u5206\u9694\u5143\u7d20\u6765\u521b\u5efa\u5143\u7ec4\u3002<\/p>\n<pre>python\n\nmy_tuple = (1, 2, 3)\nanother_tuple = \"a\", \"b\", \"c\"\nsingleton_tuple = (42,)  # note the comma, necessary for single element tuples\n<\/pre>\n<p><strong>\u8bbf\u95ee\u5143\u7d20<\/strong><\/p>\n<p>\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u8bbf\u95ee\uff0c\u7c7b\u4f3c\u4e8e\u5217\u8868\u3002<\/p>\n<pre>python\ncopy code\nfirst_element = my_tuple[0]\nlast_element = my_tuple[-1]\n<\/pre>\n<p><strong>\u5143\u7ec4\u62c6\u5305<\/strong><\/p>\n<p>\u5143\u7ec4\u5141\u8bb8\u4e00\u6b21\u5206\u914d\u591a\u4e2a\u53d8\u91cf\u3002<\/p>\n<pre>python\ncopy code\na, b, c = my_tuple\nprint(a)  # 1\nprint(b)  # 2\nprint(c)  # 3\n<\/pre>\n<p><strong>\u4e3a\u4ec0\u4e48\u4f7f\u7528\u5143\u7ec4\uff1f<\/strong><\/p>\n<p><strong>\u6027\u80fd\uff1a<\/strong> \u7531\u4e8e\u5176\u4e0d\u53d8\u6027\uff0c\u5143\u7ec4\u901a\u5e38\u6bd4\u5217\u8868\u66f4\u5feb\u3002<\/p>\n<p><strong>\u6570\u636e\u5b8c\u6574\u6027\uff1a<\/strong> \u4e0d\u53d8\u6027\u786e\u4fdd\u6570\u636e\u65e0\u6cd5\u66f4\u6539\uff0c\u4f7f\u5143\u7ec4\u6210\u4e3a\u56fa\u5b9a\u9879\u76ee\u96c6\u5408\u7684\u7406\u60f3\u9009\u62e9\u3002<\/p>\n<p><strong>hashable\uff1a<\/strong> \u56e0\u4e3a\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u6240\u4ee5\u53ef\u4ee5\u7528\u4f5c\u5b57\u5178\u4e2d\u7684\u952e\u6216\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241129\/173287326567498c3122f64.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>2.\u5957\u88c5<\/strong><\/p>\n<p><strong>\u5b9a\u4e49\uff1a<\/strong> \u96c6\u5408\u662f\u552f\u4e00\u5143\u7d20\u7684\u65e0\u5e8f\u96c6\u5408\u3002\u96c6\u5408\u901a\u5e38\u7528\u4e8e\u6210\u5458\u8d44\u683c\u6d4b\u8bd5\u548c\u6d88\u9664\u91cd\u590d\u6761\u76ee\u3002<\/p>\n<p><strong>\u7279\u70b9\uff1a<\/strong><\/p>\n<p><strong>\u65e0\u5e8f\uff1a<\/strong> \u6ca1\u6709\u7ef4\u62a4\u987a\u5e8f\uff0c\u56e0\u6b64\u65e0\u6cd5\u5efa\u7acb\u7d22\u5f15\u3002<\/p>\n<p><strong>\u72ec\u7279\u5143\u7d20\uff1a<\/strong>\u6bcf\u4e2a\u5143\u7d20\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff1b\u91cd\u590d\u9879\u4f1a\u81ea\u52a8\u5220\u9664\u3002<\/p>\n<p><strong>\u53ef\u53d8\uff1a<\/strong> \u53ef\u4ee5\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20\uff0c\u5c3d\u7ba1\u5143\u7d20\u672c\u8eab\u5fc5\u987b\u662f\u4e0d\u53ef\u53d8\u7684\u3002<\/p>\n<p><strong>\u521b\u5efa\u96c6\u5408<\/strong><\/p>\n<p>\u96c6\u5408\u662f\u4f7f\u7528\u5927\u62ec\u53f7 {} \u6216 set() \u51fd\u6570\u521b\u5efa\u7684\u3002<\/p>\n<pre>python\ncopy code\nmy_set = {1, 2, 3, 4}\nanother_set = set([4, 5, 6])  # creating a set from a list\nempty_set = set()  # note: {} creates an empty dictionary, not a set\n<\/pre>\n<p><strong>\u57fa\u672c\u96c6\u5408\u64cd\u4f5c<\/strong><\/p>\n<p>\u96c6\u5408\u652f\u6301\u5e76\u96c6\u3001\u4ea4\u96c6\u3001\u5dee\u96c6\u7b49\u591a\u79cd\u8fd0\u7b97\u3002<\/p>\n<pre>python\n\n# union\nunion_set = my_set | another_set\nprint(union_set)  # {1, 2, 3, 4, 5, 6}\n\n# intersection\nintersection_set = my_set &amp; another_set\nprint(intersection_set)  # {4}\n\n# difference\ndifference_set = my_set - another_set\nprint(difference_set)  # {1, 2, 3}\n<\/pre>\n<p><strong>\u4f1a\u5458\u6d4b\u8bd5<\/strong><\/p>\n<p>\u5957\u88c5\u9488\u5bf9\u5feb\u901f\u4f1a\u5458\u6d4b\u8bd5\u8fdb\u884c\u4e86\u4f18\u5316\u3002<\/p>\n<pre>python\n\nprint(3 in my_set)  # true\nprint(7 in my_set)  # false\n<\/pre>\n<p><strong>\u4e3a\u4ec0\u4e48\u4f7f\u7528\u96c6\u5408\uff1f<\/strong><\/p>\n<p><strong>\u72ec\u7279\u5143\u7d20\uff1a<\/strong>\u975e\u5e38\u9002\u5408\u5b58\u50a8\u9700\u8981\u72ec\u7279\u6027\u7684\u7269\u54c1\u3002<\/p>\n<p><strong>\u9ad8\u6548\u8fd0\u7b97\uff1a<\/strong> \u96b6\u5c5e\u5ea6\u6d4b\u8bd5\u548c\u96c6\u5408\u4ee3\u6570\uff08\u5e76\u96c6\u3001\u4ea4\u96c6\uff09\u7b49\u8fd0\u7b97\u6bd4\u5217\u8868\u66f4\u5feb\u3002<\/p>\n<p><strong>\u6d88\u9664\u91cd\u590d\uff1a<\/strong> \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u662f\u5220\u9664\u91cd\u590d\u7684\u5e38\u7528\u6280\u672f\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241129\/173287326767498c339870d.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>3.\u5b57\u5178<\/strong><\/p>\n<p>\u5b9a\u4e49\uff1a\u5b57\u5178\u662f\u952e\u503c\u5bf9\u7684\u65e0\u5e8f\u96c6\u5408\u3002\u5b57\u5178\u4e2d\u7684\u6bcf\u4e2a\u952e\u90fd\u662f\u552f\u4e00\u7684\u5e76\u6620\u5c04\u5230\u4e00\u4e2a\u503c\u3002<\/p>\n<p><strong>\u7279\u70b9\uff1a<\/strong><\/p>\n<p><strong>\u952e\u503c\u5bf9\uff1a<\/strong> \u952e\u662f\u552f\u4e00\u4e14\u4e0d\u53ef\u53d8\u7684\uff0c\u800c\u503c\u53ef\u4ee5\u662f\u4efb\u4f55\u7c7b\u578b\u3002<\/p>\n<p><strong>\u65e0\u5e8f\uff1a<\/strong> \u5728 python 3.7 \u4e4b\u524d\uff0c\u5b57\u5178\u662f\u65e0\u5e8f\u7684\u3002\u4ece python 3.7 \u5f00\u59cb\uff0c\u5b83\u4eec\u4fdd\u6301\u63d2\u5165\u987a\u5e8f\u3002<\/p>\n<p><strong>\u53ef\u53d8\uff1a<\/strong> \u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u3001\u5220\u9664\u6216\u66f4\u6539\u952e\u503c\u5bf9\u6765\u4fee\u6539\u5b57\u5178\u3002<\/p>\n<p><strong>\u521b\u5efa\u5b57\u5178<\/strong><\/p>\n<p>\u5b57\u5178\u662f\u4f7f\u7528\u5927\u62ec\u53f7 {} \u521b\u5efa\u7684\uff0c\u952e\u503c\u5bf9\u4e4b\u95f4\u7528\u5192\u53f7\u5206\u9694\u3002<\/p>\n<pre>python\n\nmy_dict = {\"name\": \"alice\", \"age\": 30, \"city\": \"new york\"}\nanother_dict = dict(name=\"bob\", age=25, city=\"los angeles\")\nempty_dict = {}\n<\/pre>\n<p><strong>\u8bbf\u95ee\u503c<\/strong><\/p>\n<p>\u4f7f\u7528\u952e\u6765\u8bbf\u95ee\u503c\u3002<\/p>\n<p>`\u87d2\u86c7<\/p>\n<p>\u540d\u79f0 = my_dict[&#8220;\u540d\u79f0&#8221;]<br \/> age = my_dict.get(&#8220;age&#8221;) # \u4f7f\u7528 get() \u907f\u514d keyerror`<\/p>\n<p><strong>\u6dfb\u52a0\u548c\u4fee\u6539\u6761\u76ee<\/strong><\/p>\n<p>\u5b57\u5178\u662f\u52a8\u6001\u7684\uff1b\u60a8\u53ef\u4ee5\u5373\u65f6\u6dfb\u52a0\u6216\u4fee\u6539\u6761\u76ee\u3002<\/p>\n<pre>python\n\nmy_dict[\"email\"] = \"alice@example.com\"  # adding a new key-value pair\nmy_dict[\"age\"] = 31  # modifying an existing value\n<\/pre>\n<p><strong>\u5220\u9664\u6761\u76ee<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 del \u6216 pop() \u65b9\u6cd5\u5220\u9664\u6761\u76ee\u3002<\/p>\n<pre>python\n\ndel my_dict[\"city\"]  # removing a key-value pair\nemail = my_dict.pop(\"email\", \"no email provided\")  # removes and returns the value\n<\/pre>\n<p>*<em>\u5b57\u5178\u65b9\u6cd5<br \/> *<\/em><br \/> \u5b57\u5178\u6709\u591a\u79cd\u6709\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre>python\n\nkeys = my_dict.keys()  # Returns a view of the dictionary's keys\nvalues = my_dict.values()  # Returns a view of the dictionary's values\nitems = my_dict.items()  # Returns a view of the dictionary's key-value pairs\n<\/pre>\n<p><strong>\u4e3a\u4ec0\u4e48\u4f7f\u7528\u5b57\u5178\uff1f<\/strong><\/p>\n<p><strong>\u57fa\u4e8e\u5bc6\u94a5\u7684\u8bbf\u95ee\uff1a<\/strong>\u975e\u5e38\u9002\u5408\u9700\u8981\u901a\u8fc7\u552f\u4e00\u6807\u8bc6\u7b26\uff08\u5bc6\u94a5\uff09\u5feb\u901f\u68c0\u7d22\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u52a8\u6001\u7ed3\u6784\uff1a<\/strong> \u5bf9\u4e8e\u9700\u8981\u968f\u65f6\u95f4\u589e\u957f\u548c\u53d8\u5316\u7684\u6570\u636e\u7ed3\u6784\u5f88\u6709\u7528\u3002<\/p>\n<p><strong>\u9ad8\u6548\uff1a<\/strong> \u57fa\u4e8e\u952e\u7684\u8bbf\u95ee\u901a\u5e38\u6bd4\u641c\u7d22\u5217\u8868\u6216\u5143\u7ec4\u66f4\u5feb\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241129\/173287326867498c34b99a8.jpg\" class=\"aligncenter\"><\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u5143\u7ec4\u3001\u96c6\u5408\u548c\u5b57\u5178 &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205351","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205351","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205351"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205351\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}