{"id":1032253,"date":"2024-12-31T11:32:11","date_gmt":"2024-12-31T03:32:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1032253.html"},"modified":"2024-12-31T11:32:13","modified_gmt":"2024-12-31T03:32:13","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e5%be%aa%e7%8e%af%e4%b8%ba%e5%88%97%e8%a1%a8%e8%b5%8b%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1032253.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u4f7f\u7528\u5faa\u73af\u4e3a\u5217\u8868\u8d4b\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/1efe5eb2-330e-4fd9-a6ee-00f4b4fc43fe.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5982\u4f55\u4f7f\u7528\u5faa\u73af\u4e3a\u5217\u8868\u8d4b\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u4e3a\u5217\u8868\u8d4b\u503c\u3002\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u3001while\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u591a\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u8fd9\u4e00\u64cd\u4f5c\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528for\u5faa\u73af\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528for\u5faa\u73af\u4e3a\u5217\u8868\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528for\u5faa\u73af\u4e3a\u5217\u8868\u8d4b\u503c<\/h2>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0cfor\u5faa\u73af\u53ef\u4ee5\u7528\u6765\u904d\u5386\u4e00\u4e2a\u8303\u56f4\u6216\u4e00\u4e2a\u5e8f\u5217\uff0c\u5e76\u5bf9\u5217\u8868\u8fdb\u884c\u8d4b\u503c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868<\/p>\n<p>my_list = []<\/p>\n<h2><strong>\u4f7f\u7528for\u5faa\u73af\u4e3a\u5217\u8868\u8d4b\u503c<\/strong><\/h2>\n<p>for i in range(10):<\/p>\n<p>    my_list.append(i)<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b0\u52309\u7684\u5217\u8868\u3002\u8fd9\u6bb5\u4ee3\u7801\u7684\u5de5\u4f5c\u539f\u7406\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868<code>my_list<\/code>\u3002<\/li>\n<li>\u4f7f\u7528<code>range(10)<\/code>\u751f\u6210\u4e00\u4e2a\u5305\u542b0\u52309\u7684\u5e8f\u5217\u3002<\/li>\n<li>\u904d\u5386\u8fd9\u4e2a\u5e8f\u5217\uff0c\u5e76\u5c06\u6bcf\u4e2a\u5143\u7d20\u6dfb\u52a0\u5230<code>my_list<\/code>\u4e2d\u3002<\/li>\n<\/ol>\n<p><h3>2\u3001\u4f7f\u7528\u7d22\u5f15\u4e3a\u5217\u8868\u7279\u5b9a\u4f4d\u7f6e\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u7d22\u5f15\u6765\u4e3a\u5217\u8868\u7684\u7279\u5b9a\u4f4d\u7f6e\u8d4b\u503c\u3002\u53ef\u4ee5\u4f7f\u7528<code>range<\/code>\u51fd\u6570\u751f\u6210\u7d22\u5f15\uff0c\u5e76\u5728for\u5faa\u73af\u4e2d\u4f7f\u7528\u7d22\u5f15\u8fdb\u884c\u8d4b\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u521d\u59cb\u5217\u8868<\/p>\n<p>my_list = [0] * 10<\/p>\n<h2><strong>\u4f7f\u7528for\u5faa\u73af\u548c\u7d22\u5f15\u4e3a\u5217\u8868\u8d4b\u503c<\/strong><\/h2>\n<p>for i in range(10):<\/p>\n<p>    my_list[i] = i * 2<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b10\u4e2a\u5143\u7d20\u7684\u5217\u8868\uff0c\u6bcf\u4e2a\u5143\u7d20\u7684\u503c\u662f\u5176\u7d22\u5f15\u7684\u4e24\u500d\u3002\u5177\u4f53\u64cd\u4f5c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u5305\u542b10\u4e2a0\u7684\u5217\u8868<code>my_list<\/code>\u3002<\/li>\n<li>\u4f7f\u7528<code>range(10)<\/code>\u751f\u6210\u4e00\u4e2a\u5305\u542b0\u52309\u7684\u5e8f\u5217\u3002<\/li>\n<li>\u904d\u5386\u8fd9\u4e2a\u5e8f\u5217\uff0c\u5e76\u5c06\u6bcf\u4e2a\u7d22\u5f15\u7684\u4e24\u500d\u8d4b\u503c\u7ed9<code>my_list<\/code>\u7684\u76f8\u5e94\u4f4d\u7f6e\u3002<\/li>\n<\/ol>\n<p><h2>\u4e8c\u3001\u4f7f\u7528while\u5faa\u73af\u4e3a\u5217\u8868\u8d4b\u503c<\/h2>\n<\/p>\n<p><p>\u9664\u4e86for\u5faa\u73af\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528while\u5faa\u73af\u4e3a\u5217\u8868\u8d4b\u503c\u3002while\u5faa\u73af\u5728\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\u4f1a\u4e00\u76f4\u6267\u884c\uff0c\u56e0\u6b64\u9700\u8981\u5728\u5faa\u73af\u4f53\u5185\u624b\u52a8\u66f4\u65b0\u5faa\u73af\u6761\u4ef6\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528while\u5faa\u73af\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868<\/p>\n<p>my_list = []<\/p>\n<h2><strong>\u4f7f\u7528while\u5faa\u73af\u4e3a\u5217\u8868\u8d4b\u503c<\/strong><\/h2>\n<p>i = 0<\/p>\n<p>while i &lt; 10:<\/p>\n<p>    my_list.append(i * 3)<\/p>\n<p>    i += 1<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b0\u523027\u7684\u5217\u8868\uff0c\u6bcf\u4e2a\u5143\u7d20\u7684\u503c\u662f\u5176\u7d22\u5f15\u7684\u4e09\u500d\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868<code>my_list<\/code>\u3002<\/li>\n<li>\u521d\u59cb\u5316\u5faa\u73af\u53d8\u91cf<code>i<\/code>\u4e3a0\u3002<\/li>\n<li>\u5728while\u5faa\u73af\u4e2d\uff0c\u6bcf\u6b21\u5c06<code>i<\/code>\u7684\u4e09\u500d\u6dfb\u52a0\u5230<code>my_list<\/code>\u4e2d\uff0c\u5e76\u9012\u589e<code>i<\/code>\u3002<\/li>\n<li>\u5f53<code>i<\/code>\u8fbe\u523010\u65f6\uff0c\u5faa\u73af\u7ec8\u6b62\u3002<\/li>\n<\/ol>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u4e3a\u5217\u8868\u8d4b\u503c<\/h2>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662fPython\u4e2d\u7684\u4e00\u79cd\u7b80\u6d01\u8bed\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u5217\u8868\u3002\u5b83\u7ed3\u5408\u4e86for\u5faa\u73af\u548c\u6761\u4ef6\u5224\u65ad\uff0c\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b8c\u6210\u5217\u8868\u8d4b\u503c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u4e3a\u5217\u8868\u8d4b\u503c<\/p>\n<p>my_list = [i * 4 for i in range(10)]<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b0\u523036\u7684\u5217\u8868\uff0c\u6bcf\u4e2a\u5143\u7d20\u7684\u503c\u662f\u5176\u7d22\u5f15\u7684\u56db\u500d\u3002\u5177\u4f53\u64cd\u4f5c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<code>[i * 4 for i in range(10)]<\/code>\u751f\u6210\u4e00\u4e2a\u5305\u542b0\u52309\u7684\u5e8f\u5217\uff0c\u5e76\u5c06\u6bcf\u4e2a\u5143\u7d20\u7684\u56db\u500d\u4f5c\u4e3a\u65b0\u5217\u8868\u7684\u5143\u7d20\u3002<\/li>\n<li>\u6253\u5370\u751f\u6210\u7684\u5217\u8868<code>my_list<\/code>\u3002<\/li>\n<\/ol>\n<p><h2>\u56db\u3001\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u4e3a\u5217\u8868\u8d4b\u503c<\/h2>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u6765\u4e3a\u5217\u8868\u8d4b\u503c\u3002\u53ef\u4ee5\u5728for\u5faa\u73af\u6216\u5217\u8868\u63a8\u5bfc\u5f0f\u4e2d\u6dfb\u52a0\u6761\u4ef6\u5224\u65ad\u8bed\u53e5\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><h3>1\u3001\u5728for\u5faa\u73af\u4e2d\u7ed3\u5408\u6761\u4ef6\u5224\u65ad<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868<\/p>\n<p>my_list = []<\/p>\n<h2><strong>\u4f7f\u7528for\u5faa\u73af\u548c\u6761\u4ef6\u5224\u65ad\u4e3a\u5217\u8868\u8d4b\u503c<\/strong><\/h2>\n<p>for i in range(10):<\/p>\n<p>    if i % 2 == 0:<\/p>\n<p>        my_list.append(i * 5)<\/p>\n<p>    else:<\/p>\n<p>        my_list.append(i * 3)<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b0\u52309\u7684\u5217\u8868\uff0c\u5076\u6570\u7d22\u5f15\u7684\u503c\u4e3a\u7d22\u5f15\u7684\u4e94\u500d\uff0c\u5947\u6570\u7d22\u5f15\u7684\u503c\u4e3a\u7d22\u5f15\u7684\u4e09\u500d\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868<code>my_list<\/code>\u3002<\/li>\n<li>\u4f7f\u7528<code>range(10)<\/code>\u751f\u6210\u4e00\u4e2a\u5305\u542b0\u52309\u7684\u5e8f\u5217\u3002<\/li>\n<li>\u904d\u5386\u8fd9\u4e2a\u5e8f\u5217\uff0c\u5e76\u6839\u636e\u7d22\u5f15\u7684\u5947\u5076\u6027\u8fdb\u884c\u4e0d\u540c\u7684\u8d4b\u503c\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p><h3>2\u3001\u5728\u5217\u8868\u63a8\u5bfc\u5f0f\u4e2d\u7ed3\u5408\u6761\u4ef6\u5224\u65ad<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u6761\u4ef6\u5224\u65ad\u4e3a\u5217\u8868\u8d4b\u503c<\/p>\n<p>my_list = [i * 5 if i % 2 == 0 else i * 3 for i in range(10)]<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b0\u52309\u7684\u5217\u8868\uff0c\u5076\u6570\u7d22\u5f15\u7684\u503c\u4e3a\u7d22\u5f15\u7684\u4e94\u500d\uff0c\u5947\u6570\u7d22\u5f15\u7684\u503c\u4e3a\u7d22\u5f15\u7684\u4e09\u500d\u3002\u5177\u4f53\u64cd\u4f5c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<code>[i * 5 if i % 2 == 0 else i * 3 for i in range(10)]<\/code>\u751f\u6210\u4e00\u4e2a\u5305\u542b0\u52309\u7684\u5e8f\u5217\uff0c\u5e76\u6839\u636e\u7d22\u5f15\u7684\u5947\u5076\u6027\u8fdb\u884c\u4e0d\u540c\u7684\u8d4b\u503c\u64cd\u4f5c\u3002<\/li>\n<li>\u6253\u5370\u751f\u6210\u7684\u5217\u8868<code>my_list<\/code>\u3002<\/li>\n<\/ol>\n<p><h2>\u4e94\u3001\u4f7f\u7528\u591a\u91cd\u5faa\u73af\u4e3a\u591a\u7ef4\u5217\u8868\u8d4b\u503c<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u591a\u7ef4\u5217\u8868\uff08\u5982\u4e8c\u7ef4\u5217\u8868\uff09\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u91cd\u5faa\u73af\u8fdb\u884c\u8d4b\u503c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u591a\u91cd\u5faa\u73af\u4e3a\u4e8c\u7ef4\u5217\u8868\u8d4b\u503c\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528for\u5faa\u73af\u4e3a\u4e8c\u7ef4\u5217\u8868\u8d4b\u503c<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a3x3\u7684\u7a7a\u4e8c\u7ef4\u5217\u8868<\/p>\n<p>my_list = [[0 for _ in range(3)] for _ in range(3)]<\/p>\n<h2><strong>\u4f7f\u7528\u591a\u91cdfor\u5faa\u73af\u4e3a\u4e8c\u7ef4\u5217\u8868\u8d4b\u503c<\/strong><\/h2>\n<p>for i in range(3):<\/p>\n<p>    for j in range(3):<\/p>\n<p>        my_list[i][j] = i * j<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a3&#215;3\u7684\u4e8c\u7ef4\u5217\u8868\uff0c\u6bcf\u4e2a\u5143\u7d20\u7684\u503c\u4e3a\u5176\u884c\u7d22\u5f15\u548c\u5217\u7d22\u5f15\u7684\u4e58\u79ef\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u521b\u5efa\u4e00\u4e2a3&#215;3\u7684\u7a7a\u4e8c\u7ef4\u5217\u8868<code>my_list<\/code>\u3002<\/li>\n<li>\u4f7f\u7528\u5916\u5c42for\u5faa\u73af\u904d\u5386\u884c\u7d22\u5f15<code>i<\/code>\u3002<\/li>\n<li>\u4f7f\u7528\u5185\u5c42for\u5faa\u73af\u904d\u5386\u5217\u7d22\u5f15<code>j<\/code>\uff0c\u5e76\u5c06\u6bcf\u4e2a\u5143\u7d20\u7684\u503c\u8bbe\u7f6e\u4e3a<code>i * j<\/code>\u3002<\/li>\n<\/ol>\n<p><h3>2\u3001\u4f7f\u7528while\u5faa\u73af\u4e3a\u4e8c\u7ef4\u5217\u8868\u8d4b\u503c<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a3x3\u7684\u7a7a\u4e8c\u7ef4\u5217\u8868<\/p>\n<p>my_list = [[0 for _ in range(3)] for _ in range(3)]<\/p>\n<h2><strong>\u4f7f\u7528\u591a\u91cdwhile\u5faa\u73af\u4e3a\u4e8c\u7ef4\u5217\u8868\u8d4b\u503c<\/strong><\/h2>\n<p>i = 0<\/p>\n<p>while i &lt; 3:<\/p>\n<p>    j = 0<\/p>\n<p>    while j &lt; 3:<\/p>\n<p>        my_list[i][j] = i + j<\/p>\n<p>        j += 1<\/p>\n<p>    i += 1<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a3&#215;3\u7684\u4e8c\u7ef4\u5217\u8868\uff0c\u6bcf\u4e2a\u5143\u7d20\u7684\u503c\u4e3a\u5176\u884c\u7d22\u5f15\u548c\u5217\u7d22\u5f15\u7684\u548c\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u521b\u5efa\u4e00\u4e2a3&#215;3\u7684\u7a7a\u4e8c\u7ef4\u5217\u8868<code>my_list<\/code>\u3002<\/li>\n<li>\u521d\u59cb\u5316\u5916\u5c42\u5faa\u73af\u53d8\u91cf<code>i<\/code>\u4e3a0\uff0c\u5e76\u5728while\u5faa\u73af\u4e2d\u904d\u5386\u884c\u7d22\u5f15\u3002<\/li>\n<li>\u521d\u59cb\u5316\u5185\u5c42\u5faa\u73af\u53d8\u91cf<code>j<\/code>\u4e3a0\uff0c\u5e76\u5728while\u5faa\u73af\u4e2d\u904d\u5386\u5217\u7d22\u5f15\uff0c\u5c06\u6bcf\u4e2a\u5143\u7d20\u7684\u503c\u8bbe\u7f6e\u4e3a<code>i + j<\/code>\u3002<\/li>\n<li>\u9012\u589e\u5faa\u73af\u53d8\u91cf<code>i<\/code>\u548c<code>j<\/code>\uff0c\u76f4\u5230\u904d\u5386\u5b8c\u6574\u4e2a\u4e8c\u7ef4\u5217\u8868\u3002<\/li>\n<\/ol>\n<p><h2>\u516d\u3001\u4f7f\u7528\u679a\u4e3e\u4e3a\u5217\u8868\u8d4b\u503c<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>enumerate<\/code>\u51fd\u6570\u4e3a\u5217\u8868\u8d4b\u503c\u3002<code>enumerate<\/code>\u51fd\u6570\u53ef\u4ee5\u540c\u65f6\u83b7\u53d6\u5217\u8868\u5143\u7d20\u53ca\u5176\u7d22\u5f15\uff0c\u975e\u5e38\u9002\u5408\u5728\u5faa\u73af\u4e2d\u8fdb\u884c\u8d4b\u503c\u64cd\u4f5c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528<code>enumerate<\/code>\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u521d\u59cb\u5217\u8868<\/p>\n<p>my_list = [0] * 10<\/p>\n<h2><strong>\u4f7f\u7528enumerate\u4e3a\u5217\u8868\u8d4b\u503c<\/strong><\/h2>\n<p>for index, value in enumerate(range(10)):<\/p>\n<p>    my_list[index] = value * 6<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b0\u523054\u7684\u5217\u8868\uff0c\u6bcf\u4e2a\u5143\u7d20\u7684\u503c\u662f\u5176\u7d22\u5f15\u7684\u516d\u500d\u3002\u5177\u4f53\u64cd\u4f5c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u5305\u542b10\u4e2a0\u7684\u5217\u8868<code>my_list<\/code>\u3002<\/li>\n<li>\u4f7f\u7528<code>enumerate(range(10))<\/code>\u751f\u6210\u4e00\u4e2a\u5305\u542b\u7d22\u5f15\u548c\u503c\u7684\u5e8f\u5217\u3002<\/li>\n<li>\u904d\u5386\u8fd9\u4e2a\u5e8f\u5217\uff0c\u5e76\u5c06\u6bcf\u4e2a\u7d22\u5f15\u7684\u516d\u500d\u8d4b\u503c\u7ed9<code>my_list<\/code>\u7684\u76f8\u5e94\u4f4d\u7f6e\u3002<\/li>\n<\/ol>\n<p><h2>\u4e03\u3001\u4f7f\u7528zip\u4e3a\u591a\u4e2a\u5217\u8868\u8d4b\u503c<\/h2>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u9700\u8981\u540c\u65f6\u4e3a\u591a\u4e2a\u5217\u8868\u8d4b\u503c\u3002\u53ef\u4ee5\u4f7f\u7528<code>zip<\/code>\u51fd\u6570\u5c06\u591a\u4e2a\u5217\u8868\u6253\u5305\u6210\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5e76\u5728\u5faa\u73af\u4e2d\u8fdb\u884c\u8d4b\u503c\u64cd\u4f5c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528<code>zip<\/code>\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e24\u4e2a\u521d\u59cb\u5217\u8868<\/p>\n<p>list1 = [0] * 5<\/p>\n<p>list2 = [0] * 5<\/p>\n<h2><strong>\u4f7f\u7528zip\u4e3a\u591a\u4e2a\u5217\u8868\u8d4b\u503c<\/strong><\/h2>\n<p>for i, (a, b) in enumerate(zip(range(5), range(5, 10))):<\/p>\n<p>    list1[i] = a<\/p>\n<p>    list2[i] = b<\/p>\n<p>print(list1)<\/p>\n<p>print(list2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e86\u4e24\u4e2a\u5217\u8868<code>list1<\/code>\u548c<code>list2<\/code>\uff0c<code>list1<\/code>\u5305\u542b0\u52304\uff0c<code>list2<\/code>\u5305\u542b5\u52309\u3002\u5177\u4f53\u64cd\u4f5c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u4e24\u4e2a\u5305\u542b5\u4e2a0\u7684\u5217\u8868<code>list1<\/code>\u548c<code>list2<\/code>\u3002<\/li>\n<li>\u4f7f\u7528<code>zip(range(5), range(5, 10))<\/code>\u5c06\u4e24\u4e2a\u8303\u56f4\u6253\u5305\u6210\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002<\/li>\n<li>\u4f7f\u7528<code>enumerate<\/code>\u904d\u5386\u8fd9\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5e76\u5c06\u6bcf\u4e2a\u7d22\u5f15\u7684\u503c\u8d4b\u503c\u7ed9<code>list1<\/code>\u548c<code>list2<\/code>\u7684\u76f8\u5e94\u4f4d\u7f6e\u3002<\/li>\n<\/ol>\n<p><h2>\u516b\u3001\u4f7f\u7528\u51fd\u6570\u548c\u9012\u5f52\u4e3a\u5217\u8868\u8d4b\u503c<\/h2>\n<\/p>\n<p><p>\u5728\u590d\u6742\u7684\u573a\u666f\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u548c\u9012\u5f52\u4e3a\u5217\u8868\u8d4b\u503c\u3002\u9012\u5f52\u662f\u4e00\u79cd\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u7684\u7f16\u7a0b\u6280\u672f\uff0c\u9002\u7528\u4e8e\u89e3\u51b3\u5177\u6709\u91cd\u590d\u5b50\u7ed3\u6784\u7684\u95ee\u9898\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u9012\u5f52\u4e3a\u5217\u8868\u8d4b\u503c\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u9012\u5f52\u51fd\u6570\u4e3a\u5217\u8868\u8d4b\u503c<\/p>\n<p>def fill_list(lst, n):<\/p>\n<p>    if n &lt; 0:<\/p>\n<p>        return<\/p>\n<p>    lst[n] = n * 7<\/p>\n<p>    fill_list(lst, n - 1)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u521d\u59cb\u5217\u8868<\/strong><\/h2>\n<p>my_list = [0] * 10<\/p>\n<h2><strong>\u8c03\u7528\u9012\u5f52\u51fd\u6570\u4e3a\u5217\u8868\u8d4b\u503c<\/strong><\/h2>\n<p>fill_list(my_list, len(my_list) - 1)<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b0\u523063\u7684\u5217\u8868\uff0c\u6bcf\u4e2a\u5143\u7d20\u7684\u503c\u662f\u5176\u7d22\u5f15\u7684\u4e03\u500d\u3002\u5177\u4f53\u64cd\u4f5c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b9a\u4e49\u4e00\u4e2a\u9012\u5f52\u51fd\u6570<code>fill_list<\/code>\uff0c\u63a5\u6536\u5217\u8868<code>lst<\/code>\u548c\u7d22\u5f15<code>n<\/code>\u4f5c\u4e3a\u53c2\u6570\u3002<\/li>\n<li>\u5728\u9012\u5f52\u51fd\u6570\u4e2d\uff0c\u68c0\u67e5\u7d22\u5f15<code>n<\/code>\u662f\u5426\u5c0f\u4e8e0\uff0c\u5982\u679c\u662f\uff0c\u5219\u7ec8\u6b62\u9012\u5f52\u3002<\/li>\n<li>\u5426\u5219\uff0c\u5c06\u7d22\u5f15<code>n<\/code>\u7684\u4e03\u500d\u8d4b\u503c\u7ed9<code>lst[n]<\/code>\uff0c\u5e76\u9012\u5f52\u8c03\u7528<code>fill_list<\/code>\uff0c\u7d22\u5f15\u51cf1\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u5305\u542b10\u4e2a0\u7684\u5217\u8868<code>my_list<\/code>\u3002<\/li>\n<li>\u8c03\u7528\u9012\u5f52\u51fd\u6570<code>fill_list<\/code>\uff0c\u4e3a<code>my_list<\/code>\u8d4b\u503c\u3002<\/li>\n<\/ol>\n<p><h2>\u4e5d\u3001\u4f7f\u7528itertools\u4e3a\u5217\u8868\u8d4b\u503c<\/h2>\n<\/p>\n<p><p><code>itertools<\/code>\u662fPython\u7684\u4e00\u4e2a\u6807\u51c6\u5e93\uff0c\u63d0\u4f9b\u4e86\u591a\u4e2a\u8fed\u4ee3\u5668\u751f\u6210\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u4e8e\u9ad8\u6548\u5730\u5904\u7406\u5e8f\u5217\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528<code>itertools<\/code>\u4e3a\u5217\u8868\u8d4b\u503c\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u521d\u59cb\u5217\u8868<\/strong><\/h2>\n<p>my_list = [0] * 10<\/p>\n<h2><strong>\u4f7f\u7528itertools\u4e3a\u5217\u8868\u8d4b\u503c<\/strong><\/h2>\n<p>for i, value in zip(range(10), itertools.count(start=0, step=8)):<\/p>\n<p>    my_list[i] = value<\/p>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b0\u523072\u7684\u5217\u8868\uff0c\u6bcf\u4e2a\u5143\u7d20\u7684\u503c\u662f\u5176\u7d22\u5f15\u7684\u516b\u500d\u3002\u5177\u4f53\u64cd\u4f5c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165<code>itertools<\/code>\u5e93\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u5305\u542b10\u4e2a0\u7684\u5217\u8868<code>my_list<\/code>\u3002<\/li>\n<li>\u4f7f\u7528<code>itertools.count(start=0, step=8)<\/code>\u751f\u6210\u4e00\u4e2a\u4ece0\u5f00\u59cb\uff0c\u6bcf\u6b21\u9012\u589e8\u7684\u8fed\u4ee3\u5668\u3002<\/li>\n<li>\u4f7f\u7528<code>zip(range(10), itertools.count(start=0, step=8))<\/code>\u5c06\u7d22\u5f15\u548c\u8fed\u4ee3\u5668\u6253\u5305\u6210\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002<\/li>\n<li>\u904d\u5386\u8fd9\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5e76\u5c06\u6bcf\u4e2a\u7d22\u5f15\u7684\u503c\u8d4b\u503c\u7ed9<code>my_list<\/code>\u7684\u76f8\u5e94\u4f4d\u7f6e\u3002<\/li>\n<\/ol>\n<p><h2>\u5341\u3001\u4f7f\u7528numpy\u4e3a\u6570\u7ec4\u8d4b\u503c<\/h2>\n<\/p>\n<p><p>\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u4e2d\uff0c\u901a\u5e38\u4f7f\u7528<code>numpy<\/code>\u5e93\u6765\u5904\u7406\u6570\u7ec4\u3002<code>numpy<\/code>\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u6570\u7ec4\u8d4b\u503c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528<code>numpy<\/code>\u4e3a\u6570\u7ec4\u8d4b\u503c\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u521d\u59cb\u6570\u7ec4<\/strong><\/h2>\n<p>my_array = np.zeros(10)<\/p>\n<h2><strong>\u4f7f\u7528numpy\u4e3a\u6570\u7ec4\u8d4b\u503c<\/strong><\/h2>\n<p>for i in range(10):<\/p>\n<p>    my_array[i] = i * 9<\/p>\n<p>print(my_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b0\u523081\u7684\u6570\u7ec4\uff0c\u6bcf\u4e2a\u5143\u7d20\u7684\u503c\u662f\u5176\u7d22\u5f15\u7684\u4e5d\u500d\u3002\u5177\u4f53\u64cd\u4f5c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165<code>numpy<\/code>\u5e93\u3002<\/li>\n<li>\u4f7f\u7528<code>np.zeros(10)<\/code>\u521b\u5efa\u4e00\u4e2a\u5305\u542b10\u4e2a0\u7684\u6570\u7ec4<code>my_array<\/code>\u3002<\/li>\n<li>\u4f7f\u7528<code>range(10)<\/code>\u751f\u6210\u4e00\u4e2a\u5305\u542b0\u52309\u7684\u5e8f\u5217\u3002<\/li>\n<li>\u904d\u5386\u8fd9\u4e2a\u5e8f\u5217\uff0c\u5e76\u5c06\u6bcf\u4e2a\u7d22\u5f15\u7684\u4e5d\u500d\u8d4b\u503c\u7ed9<code>my_array<\/code>\u7684\u76f8\u5e94\u4f4d\u7f6e\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u591a\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u4f7f\u7528\u5faa\u73af\u4e3a\u5217\u8868\u6216\u6570\u7ec4\u8d4b\u503c\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u6709\u54ea\u4e9b\u5faa\u73af\u7ed3\u6784\u53ef\u4ee5\u7528\u6765\u4e3a\u5217\u8868\u8d4b\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4e3b\u8981\u6709\u4e24\u79cd\u5faa\u73af\u7ed3\u6784\u53ef\u4ee5\u7528\u6765\u4e3a\u5217\u8868\u8d4b\u503c\uff1a<code>for<\/code>\u5faa\u73af\u548c<code>while<\/code>\u5faa\u73af\u3002\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u65f6\uff0c\u53ef\u4ee5\u904d\u5386\u4e00\u4e2a\u8303\u56f4\u6216\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u4f9d\u6b21\u4e3a\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8d4b\u503c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>for i in range(5)<\/code>\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5305\u542b5\u4e2a\u5143\u7d20\u7684\u5217\u8868\u3002<code>while<\/code>\u5faa\u73af\u5219\u66f4\u9002\u5408\u5728\u4e0d\u786e\u5b9a\u5faa\u73af\u6b21\u6570\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\uff0c\u4f8b\u5982\uff0c\u76f4\u5230\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u4e3a\u6b62\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5feb\u901f\u4e3a\u5217\u8868\u8d4b\u503c\uff1f<\/strong><br \/>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u53ef\u4ee5\u5feb\u901f\u751f\u6210\u65b0\u7684\u5217\u8868\u3002\u901a\u8fc7\u5728\u65b9\u62ec\u53f7\u5185\u7ed3\u5408\u5faa\u73af\u548c\u6761\u4ef6\u5224\u65ad\uff0c\u53ef\u4ee5\u8f7b\u677e\u4e3a\u5217\u8868\u8d4b\u503c\u3002\u4f8b\u5982\uff0c<code>squared_numbers = [x**2 for x in range(10)]<\/code>\u4f1a\u751f\u6210\u4e00\u4e2a\u5305\u542b0\u52309\u5e73\u65b9\u503c\u7684\u5217\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u901a\u5e38\u6027\u80fd\u66f4\u4f18\u3002<\/p>\n<p><strong>\u5728\u5faa\u73af\u4e2d\u5982\u4f55\u907f\u514d\u4e3a\u5217\u8868\u8d4b\u503c\u65f6\u51fa\u73b0\u91cd\u590d\u5143\u7d20\uff1f<\/strong><br \/>\u4e3a\u4e86\u907f\u514d\u5728\u5faa\u73af\u4e2d\u4e3a\u5217\u8868\u8d4b\u503c\u65f6\u51fa\u73b0\u91cd\u590d\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u6765\u8f85\u52a9\u7ba1\u7406\u6570\u636e\u3002\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u5148\u68c0\u67e5\u5143\u7d20\u662f\u5426\u5728\u96c6\u5408\u4e2d\uff0c\u5982\u679c\u4e0d\u5728\u5219\u6dfb\u52a0\u5230\u5217\u8868\u548c\u96c6\u5408\u4e2d\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>if item not in my_set<\/code>\u53ef\u4ee5\u6709\u6548\u9632\u6b62\u6dfb\u52a0\u91cd\u590d\u9879\u3002\u540c\u65f6\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u8ddf\u8e2a\u5df2\u7ecf\u6dfb\u52a0\u7684\u5143\u7d20\uff0c\u4ece\u800c\u4fdd\u8bc1\u5217\u8868\u7684\u552f\u4e00\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u4e3a\u5217\u8868\u8d4b\u503c\u3002\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u3001while\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u591a\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u8fd9\u4e00\u64cd [&hellip;]","protected":false},"author":3,"featured_media":1032259,"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\/1032253"}],"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=1032253"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1032253\/revisions"}],"predecessor-version":[{"id":1032260,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1032253\/revisions\/1032260"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1032259"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1032253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1032253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1032253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}