{"id":204509,"date":"2025-05-29T14:52:33","date_gmt":"2025-05-29T06:52:33","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204509\/"},"modified":"2025-05-29T14:52:33","modified_gmt":"2025-05-29T06:52:33","slug":"%e6%8e%8c%e6%8f%a1-python-%e5%88%97%e8%a1%a8%ef%bc%9a%e6%82%a8%e9%9c%80%e8%a6%81%e4%ba%86%e8%a7%a3%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%8a%80%e6%9c%af","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204509\/","title":{"rendered":"\u638c\u63e1 Python \u5217\u8868\uff1a\u60a8\u9700\u8981\u4e86\u89e3\u7684\u57fa\u672c\u6280\u672f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u638c\u63e1 Python \u5217\u8868\uff1a\u60a8\u9700\u8981\u4e86\u89e3\u7684\u57fa\u672c\u6280\u672f<\/h1>\n<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662f\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u638c\u63e1 Python \u5217\u8868\uff1a\u60a8\u9700\u8981\u4e86\u89e3\u7684\u57fa\u672c\u6280\u672f\u300b<\/span>\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241109\/1731137447672f0fa7d1772.jpg\" class=\"aligncenter\"><\/p>\n<p>\u8fd9\u5c06\u5faa\u73af\u904d\u5386\u5217\u8868\uff0c\u5e76\u4e14\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\u90fd\u53ef\u4ee5\u4f5c\u4e3a\u53d8\u91cf\u4f7f\u7528\u3002\u5f53\u9700\u8981\u904d\u5386\u5217\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\u65f6\uff0c\u8fd9\u88ab\u5e7f\u6cdb\u4f7f\u7528\u3002<\/p>\n<pre>operating_systems = [\"windows\", \"mac\", \"linux\"]\nfor os in operating_systems:\n    print(os)`\n\n<\/pre>\n<pre># output\nwindows\nmac\nlinux\n<\/pre>\n<p>\u5f53\u9700\u8981\u6839\u636e\u7d22\u5f15\u8bbf\u95ee\u5e76\u4e14\u9700\u8981\u7d22\u5f15\u503c\u65f6\u3002<\/p>\n<pre>operating_systems = [\"windows\", \"mac\", \"linux\"]\nfor i in range(len(operating_systems)):\n    print(f\"index {i}: {operating_systems[i]}\")\n<\/pre>\n<pre># output\nindex 0: windows\nindex 1: mac\nindex 2: linux\n<\/pre>\n<p>\u5982\u679c\u60a8\u540c\u65f6\u9700\u8981\u7d22\u5f15\u548c\u503c\uff0c\u8fd9\u662f\u4e00\u79cd\u4f18\u96c5\u7684\u65b9\u5f0f<\/p>\n<pre>operating_systems = [\"windows\", \"mac\", \"linux\"]\nfor index, os in enumerate(operating_systems):\n    print(f\"index is {index} and value is {os}\")\n<\/pre>\n<pre># output\nindex is 0 and value is windows\nindex is 1 and value is mac\nindex is 2 and value is linux\n<\/pre>\n<pre>operating_systems = [\"windows\", \"mac\", \"linux\"]\ni = 0 # inital condition, required to start\nwhile i &lt; len(operating_systems):\n    print(f\"while looping {i} got the value {operating_systems[i]}\")\n    i = i + 1 # this is very important, dont forget about infinite loops\n<\/pre>\n<pre># output\nwhile looping 0 got the value windows\nwhile looping 1 got the value mac\nwhile looping 2 got the value linux\n<\/pre>\n<p>\u53ef\u4ee5\u5f88\u597d\u5730\u63a7\u5236\u4f55\u65f6\u5411\u524d\u79fb\u52a8\u8fed\u4ee3\u5668\uff0c\u5c3d\u7ba1\u6211\u4eec\u5fc5\u987b\u4f9d\u9760 stopiteration \u6765\u68c0\u67e5\u662f\u5426\u5230\u8fbe\u672b\u5c3e\u3002<\/p>\n<pre>operating_systems = [\"windows\", \"mac\", \"linux\"]\niterator = iter(operating_systems)\nwhile true:\n    try:\n        os = next(iterator)\n        print(f\"consumed form iterator {os}\")\n    except stopiteration:\n        print(\"consumed all from iterator\")\n        break\n<\/pre>\n<pre># output\nconsumed form iterator windows\nconsumed form iterator mac\nconsumed form iterator linux\nconsumed all from iterator\n<\/pre>\n<pre># hack to avoid stopiteration\niterator = iter(operating_systems)\nend_of_list = object()\nreached_end = false\nwhile not reached_end:\n    os = next(iterator, end_of_list)# a predefined object as end of the list\n    if os != end_of_list:\n        print(os)\n    else:\n        reached_end = true\n<\/pre>\n<p>\u9700\u8981\u8f6c\u578b\u65f6<\/p>\n<pre>operating_systems = [\"windows\", \"mac\", \"linux\"]\nos_uppercase = [os.upper() for os in operating_systems]\nprint(os_uppercase) \n<\/pre>\n<pre># output\n['windows', 'mac', 'linux']\n<\/pre>\n<p>\u5f53\u9700\u8981\u5faa\u73af\u6d4f\u89c8\u5217\u8868\u65f6\u3002\u4f7f\u7528\u9002\u5f53\u7684\u8fb9\u754c\u6761\u4ef6\u6765\u6253\u7834\u5faa\u73af<\/p>\n<pre>import itertools\noperating_systems = [\"windows\", \"mac\", \"linux\"]\nfor item in itertools.cycle(operating_systems):  \n    print(item)\n# infinite cycling loopmake sure to have proper boundary condition to break\n<\/pre>\n<pre># output\nwindows\nmac\nlinux\nwindows\nmac\nlinux\nwindows\nmac\nlinux\nwindows\nmac\nlinux\nwindows ....... infinite loop\n<\/pre>\n<p>\u540c\u65f6\u5faa\u73af\u591a\u4e2a\u5217\u8868\u3002\u5982\u679c\u5217\u8868\u5927\u5c0f\u4e0d\u540c\uff0c\u8bf7\u6ce8\u610f\u8f93\u51fa\u3002<\/p>\n<pre>operating_systems = [\"windows\", \"mac\", \"linux\"]\nmobile_operating_systems = [\"android\", \"ios\"]\n\nfor os, mobile_os in zip(operating_systems,mobile_operating_systems):\n    print(os, mobile_os)\n<\/pre>\n<pre># output\nwindows android\nmac ios\n<\/pre>\n<pre>operating_systems = [\"windows\", \"mac\", \"linux\"]\nfor reversed_os in reversed(operating_systems):\n    print(reversed_os)\n<\/pre>\n<pre># Output\nlinux\nmac\nwindows\n<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u638c\u63e1 Python \u5217\u8868\uff1a\u60a8\u9700\u8981\u4e86\u89e3\u7684\u57fa\u672c\u6280\u672f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/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>\u638c\u63e1 Python \u5217\u8868\uff1a\u60a8\u9700\u8981&#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-204509","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204509","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=204509"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204509\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}