{"id":1081701,"date":"2025-01-08T12:41:58","date_gmt":"2025-01-08T04:41:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1081701.html"},"modified":"2025-01-08T12:42:00","modified_gmt":"2025-01-08T04:42:00","slug":"python%e5%a6%82%e4%bd%95%e7%94%a8%e5%ba%8f%e5%8f%b7%e5%bd%93%e5%ad%97%e5%85%b8%e7%9a%84%e9%94%ae-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1081701.html","title":{"rendered":"Python\u5982\u4f55\u7528\u5e8f\u53f7\u5f53\u5b57\u5178\u7684\u952e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183612\/48e48b84-ce5d-454e-a487-4aa4bcc5751d.webp\" alt=\"Python\u5982\u4f55\u7528\u5e8f\u53f7\u5f53\u5b57\u5178\u7684\u952e\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u4f7f\u7528\u5e8f\u53f7\u4f5c\u4e3a\u5b57\u5178\u7684\u952e\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>range<\/code>\u548c<code>enumerate<\/code>\u51fd\u6570\u3001\u624b\u52a8\u521b\u5efa\u5b57\u5178\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u7b49\u3002<\/strong>\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528<code>enumerate<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u548c\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u76f4\u63a5\u751f\u6210\u5e26\u6709\u5e8f\u53f7\u7684\u952e\u503c\u5bf9\uff0c\u5e76\u4e14\u4ee3\u7801\u7b80\u6d01\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u89e3\u91ca<code>enumerate<\/code>\u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>enumerate<\/code>\u51fd\u6570\u751f\u6210\u5e26\u6709\u5e8f\u53f7\u7684\u5b57\u5178<\/strong><\/p>\n<\/p>\n<p><p><code>enumerate<\/code>\u51fd\u6570\u53ef\u4ee5\u8ba9\u6211\u4eec\u5728\u904d\u5386\u4e00\u4e2a\u5217\u8868\u6216\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u65f6\uff0c\u540c\u65f6\u83b7\u5f97\u5143\u7d20\u7684\u7d22\u5f15\u548c\u503c\u3002\u901a\u8fc7\u5c06\u8fd9\u4e9b\u7d22\u5f15\u548c\u503c\u7ec4\u5408\u8d77\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u751f\u6210\u4e00\u4e2a\u4ee5\u5e8f\u53f7\u4e3a\u952e\u7684\u5b57\u5178\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>items = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<h2><strong>\u4f7f\u7528 enumerate \u751f\u6210\u5e26\u6709\u5e8f\u53f7\u7684\u5b57\u5178<\/strong><\/h2>\n<p>items_dict = {index: value for index, value in enumerate(items)}<\/p>\n<p>print(items_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5305\u542b\u4e00\u4e9b\u6c34\u679c\u540d\u79f0\u7684\u5217\u8868<code>items<\/code>\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>enumerate<\/code>\u51fd\u6570\u904d\u5386\u8fd9\u4e2a\u5217\u8868\uff0c<code>index<\/code>\u8868\u793a\u5e8f\u53f7\uff0c<code>value<\/code>\u8868\u793a\u5bf9\u5e94\u7684\u6c34\u679c\u540d\u79f0\u3002\u901a\u8fc7\u5b57\u5178\u63a8\u5bfc\u5f0f\u5c06\u8fd9\u4e9b\u7d22\u5f15\u548c\u503c\u7ec4\u5408\u8d77\u6765\uff0c\u751f\u6210\u4e86\u4e00\u4e2a\u4ee5\u5e8f\u53f7\u4e3a\u952e\u7684\u5b57\u5178<code>items_dict<\/code>\u3002\u6700\u7ec8\u8f93\u51fa\u7684\u5b57\u5178\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{0: &#39;apple&#39;, 1: &#39;banana&#39;, 2: &#39;cherry&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u624b\u52a8\u521b\u5efa\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u867d\u7136<code>enumerate<\/code>\u51fd\u6570\u975e\u5e38\u65b9\u4fbf\uff0c\u4f46\u6709\u65f6\u5019\u6211\u4eec\u53ef\u80fd\u9700\u8981\u624b\u52a8\u521b\u5efa\u5b57\u5178\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178\uff0c\u5e76\u624b\u52a8\u6dfb\u52a0\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u624b\u52a8\u521b\u5efa\u5b57\u5178<\/p>\n<p>items_dict = {<\/p>\n<p>    0: &#39;apple&#39;,<\/p>\n<p>    1: &#39;banana&#39;,<\/p>\n<p>    2: &#39;cherry&#39;<\/p>\n<p>}<\/p>\n<p>print(items_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u7b80\u5355\u76f4\u63a5\uff0c\u4f46\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u53ef\u80fd\u4e0d\u592a\u9ad8\u6548\u3002\u56e0\u6b64\uff0c\u624b\u52a8\u521b\u5efa\u5b57\u5178\u901a\u5e38\u53ea\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u7684\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u4ee5\u5e8f\u53f7\u4e3a\u952e\u7684\u5b57\u5178\u3002\u901a\u8fc7\u7ed3\u5408<code>range<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6307\u5b9a\u8303\u56f4\u5185\u5e8f\u53f7\u7684\u5b57\u5178\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>items = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u548c range \u51fd\u6570\u751f\u6210\u5e26\u6709\u5e8f\u53f7\u7684\u5b57\u5178<\/strong><\/h2>\n<p>items_dict = {i: items[i] for i in range(len(items))}<\/p>\n<p>print(items_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>range(len(items))<\/code>\u751f\u6210\u4e00\u4e2a\u4ece0\u5230\u5217\u8868\u957f\u5ea6\u51cf1\u7684\u5e8f\u5217\uff0c\u7136\u540e\u901a\u8fc7\u5b57\u5178\u63a8\u5bfc\u5f0f\u5c06\u8fd9\u4e9b\u5e8f\u53f7\u4e0e\u5bf9\u5e94\u7684\u6c34\u679c\u540d\u79f0\u7ec4\u5408\u8d77\u6765\uff0c\u751f\u6210\u4e86\u4e00\u4e2a\u4ee5\u5e8f\u53f7\u4e3a\u952e\u7684\u5b57\u5178<code>items_dict<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>zip<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>zip<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u6253\u5305\u6210\u4e00\u4e2a\u5143\u7ec4\u7684\u8fed\u4ee3\u5668\u3002\u901a\u8fc7\u5c06<code>range<\/code>\u51fd\u6570\u548c\u5217\u8868\u7ed3\u5408\u4f7f\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u4ee5\u5e8f\u53f7\u4e3a\u952e\u7684\u5b57\u5178\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5217\u8868<\/p>\n<p>items = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<h2><strong>\u4f7f\u7528 zip \u51fd\u6570\u751f\u6210\u5e26\u6709\u5e8f\u53f7\u7684\u5b57\u5178<\/strong><\/h2>\n<p>items_dict = dict(zip(range(len(items)), items))<\/p>\n<p>print(items_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>zip(range(len(items)), items)<\/code>\u5c06\u5e8f\u53f7\u548c\u6c34\u679c\u540d\u79f0\u6253\u5305\u6210\u5143\u7ec4\uff0c\u7136\u540e\u4f7f\u7528<code>dict<\/code>\u51fd\u6570\u5c06\u8fd9\u4e9b\u5143\u7ec4\u8f6c\u6362\u6210\u5b57\u5178<code>items_dict<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7efc\u5408\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u751f\u6210\u4ee5\u5e8f\u53f7\u4e3a\u952e\u7684\u5b57\u5178\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5b66\u751f\u6210\u7ee9\u7684\u5217\u8868\uff0c\u6211\u4eec\u5e0c\u671b\u5c06\u6bcf\u4e2a\u5b66\u751f\u7684\u6210\u7ee9\u5b58\u50a8\u5728\u4e00\u4e2a\u4ee5\u5e8f\u53f7\u4e3a\u952e\u7684\u5b57\u5178\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u5b66\u751f\u6210\u7ee9\u5217\u8868<\/p>\n<p>grades = [85, 92, 78, 90, 88]<\/p>\n<h2><strong>\u4f7f\u7528 enumerate \u751f\u6210\u5e26\u6709\u5e8f\u53f7\u7684\u5b57\u5178<\/strong><\/h2>\n<p>grades_dict = {index: grade for index, grade in enumerate(grades)}<\/p>\n<p>print(grades_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5305\u542b\u5b66\u751f\u6210\u7ee9\u7684\u5217\u8868<code>grades<\/code>\uff0c\u5e76\u4f7f\u7528<code>enumerate<\/code>\u51fd\u6570\u751f\u6210\u4e86\u4e00\u4e2a\u4ee5\u5e8f\u53f7\u4e3a\u952e\u7684\u5b57\u5178<code>grades_dict<\/code>\u3002\u6700\u7ec8\u8f93\u51fa\u7684\u5b57\u5178\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">{0: 85, 1: 92, 2: 78, 3: 90, 4: 88}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u751f\u6210\u4ee5\u5e8f\u53f7\u4e3a\u952e\u7684\u5b57\u5178\u3002\u65e0\u8bba\u662f\u4f7f\u7528<code>enumerate<\/code>\u51fd\u6570\u3001\u624b\u52a8\u521b\u5efa\u5b57\u5178\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\uff0c\u8fd8\u662f\u4f7f\u7528<code>zip<\/code>\u51fd\u6570\uff0c\u90fd\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u9ad8\u6548\u5730\u5b8c\u6210\u8fd9\u4e00\u4efb\u52a1\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u6570\u5b57\u5e8f\u53f7\u4f5c\u4e3a\u5b57\u5178\u7684\u952e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u5faa\u73af\u6216\u63a8\u5bfc\u5f0f\u6765\u751f\u6210\u5b57\u5178\uff0c\u5176\u4e2d\u6570\u5b57\u5e8f\u53f7\u4f5c\u4e3a\u952e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>range()<\/code>\u51fd\u6570\u751f\u6210\u5e8f\u53f7\uff0c\u5e76\u5c06\u5176\u4e0e\u76f8\u5e94\u7684\u503c\u8fdb\u884c\u914d\u5bf9\u3002\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-python\">my_dict = {i: f&#39;value_{i}&#39; for i in range(5)}\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u952e\u4e3a0\u52304\u7684\u5e8f\u53f7\uff0c\u503c\u4e3a\u5bf9\u5e94\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>\u4f7f\u7528\u5e8f\u53f7\u4f5c\u4e3a\u5b57\u5178\u952e\u65f6\uff0c\u5982\u4f55\u907f\u514d\u952e\u503c\u91cd\u590d\uff1f<\/strong><br \/>\u5728\u4f7f\u7528\u6570\u5b57\u5e8f\u53f7\u4f5c\u4e3a\u5b57\u5178\u7684\u952e\u65f6\uff0c\u786e\u4fdd\u5e8f\u53f7\u7684\u552f\u4e00\u6027\u975e\u5e38\u91cd\u8981\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u521b\u5efa\u5b57\u5178\u65f6\u4f7f\u7528<code>enumerate()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\uff0c\u5b83\u4f1a\u4e3a\u6bcf\u4e2a\u5143\u7d20\u81ea\u52a8\u751f\u6210\u552f\u4e00\u7684\u5e8f\u53f7\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]\nmy_dict = {index: value for index, value in enumerate(my_list)}\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u5b57\u5178\u7684\u952e\u5c06\u662f\u4ece0\u5f00\u59cb\u7684\u552f\u4e00\u5e8f\u53f7\uff0c\u800c\u503c\u5219\u4e3a\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>\u5f53\u5b57\u5178\u7684\u952e\u662f\u5e8f\u53f7\u65f6\uff0c\u5982\u4f55\u5feb\u901f\u8bbf\u95ee\u548c\u4fee\u6539\u503c\uff1f<\/strong><br \/>\u8bbf\u95ee\u548c\u4fee\u6539\u5b57\u5178\u4e2d\u4ee5\u5e8f\u53f7\u4e3a\u952e\u7684\u503c\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u5e8f\u53f7\u6765\u7d22\u5f15\u5b57\u5178\u3002\u4f8b\u5982\uff0c\u8981\u8bbf\u95ee\u952e\u4e3a2\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-python\">value = my_dict[2]\n<\/code><\/pre>\n<p>\u82e5\u60f3\u4fee\u6539\u8be5\u503c\uff0c\u53ef\u4ee5\u76f4\u63a5\u8d4b\u503c\uff1a<\/p>\n<pre><code class=\"language-python\">my_dict[2] = &#39;new_value&#39;\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u64cd\u4f5c\u5728Python\u4e2d\u975e\u5e38\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b57\u5178\u7684\u5b9e\u73b0\u4f7f\u5f97\u901a\u8fc7\u952e\u8bbf\u95ee\u503c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(1)\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u4f7f\u7528\u5e8f\u53f7\u4f5c\u4e3a\u5b57\u5178\u7684\u952e\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528range\u548cenumerate\u51fd\u6570\u3001\u624b\u52a8\u521b\u5efa\u5b57\u5178\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1081707,"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\/1081701"}],"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=1081701"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1081701\/revisions"}],"predecessor-version":[{"id":1081711,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1081701\/revisions\/1081711"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1081707"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1081701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1081701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1081701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}