{"id":1185159,"date":"2025-01-15T19:36:47","date_gmt":"2025-01-15T11:36:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1185159.html"},"modified":"2025-01-15T19:36:50","modified_gmt":"2025-01-15T11:36:50","slug":"python%e5%a6%82%e4%bd%95%e5%be%aa%e7%8e%af%e7%bb%99%e5%88%97%e8%a1%a8%e8%b5%8b%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1185159.html","title":{"rendered":"Python\u5982\u4f55\u5faa\u73af\u7ed9\u5217\u8868\u8d4b\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25134353\/4bb9611e-29cf-415d-84e9-a68b6da8acb7.webp\" alt=\"Python\u5982\u4f55\u5faa\u73af\u7ed9\u5217\u8868\u8d4b\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5faa\u73af\u7ed9\u5217\u8868\u8d4b\u503c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ecfor\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u548cmap\u51fd\u6570\u7b49\u3002<\/strong> \u5176\u4e2d\uff0cfor\u5faa\u73af\u662f\u4e00\u79cd\u6700\u5e38\u89c1\u4e14\u6613\u4e8e\u7406\u89e3\u7684\u65b9\u5f0f\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u5219\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u52a0\u7b80\u6d01\u7684\u8bed\u6cd5\u3002\u800cmap\u51fd\u6570\u5219\u7ed3\u5408\u4e86\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u601d\u60f3\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u51e0\u79cd\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528for\u5faa\u73af\u7ed9\u5217\u8868\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>for\u5faa\u73af\u662fPython\u4e2d\u6700\u57fa\u7840\u7684\u5faa\u73af\u7ed3\u6784\u4e4b\u4e00\uff0c\u975e\u5e38\u9002\u5408\u7528\u6765\u904d\u5386\u5217\u8868\u5e76\u8fdb\u884c\u8d4b\u503c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u7840\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u7a7a\u5217\u8868\uff0c\u5e76\u60f3\u901a\u8fc7for\u5faa\u73af\u7ed9\u5b83\u8d4b\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = []<\/p>\n<p>for i in range(10):<\/p>\n<p>    my_list.append(i)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0crange(10)\u751f\u6210\u4e86\u4ece0\u52309\u7684\u6570\u5b57\u5e8f\u5217\uff0cfor\u5faa\u73af\u4f9d\u6b21\u904d\u5386\u8fd9\u4e9b\u6570\u5b57\uff0c\u5e76\u5c06\u6bcf\u4e2a\u6570\u5b57\u6dfb\u52a0\u5230my_list\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7ed3\u5408\u6761\u4ef6\u8bed\u53e5<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5728\u5faa\u73af\u4e2d\u52a0\u5165\u6761\u4ef6\u5224\u65ad\uff0c\u4ece\u800c\u6709\u9009\u62e9\u5730\u7ed9\u5217\u8868\u8d4b\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = []<\/p>\n<p>for i in range(10):<\/p>\n<p>    if i % 2 == 0:  # \u4ec5\u5f53i\u662f\u5076\u6570\u65f6\u6dfb\u52a0\u5230\u5217\u8868\u4e2d<\/p>\n<p>        my_list.append(i)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [0, 2, 4, 6, 8]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4ec5\u5c06\u5076\u6570\u6dfb\u52a0\u5230\u4e86\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\uff08List Comprehension\uff09\u662f\u4e00\u79cd\u66f4\u52a0\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u7528\u4e8e\u751f\u6210\u5217\u8868\u3002\u5b83\u4e0d\u4ec5\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u80fd\u7b80\u5316\u4ee3\u7801\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u7840\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\u4e0e\u4e0a\u9762\u76f8\u540c\u7684\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [i for i in range(10)]<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u770b\u5230\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u8bed\u6cd5\u975e\u5e38\u7b80\u6d01\uff0c\u4ec5\u7528\u4e00\u884c\u4ee3\u7801\u5c31\u5b8c\u6210\u4e86\u5217\u8868\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7ed3\u5408\u6761\u4ef6\u8bed\u53e5<\/h4>\n<\/p>\n<p><p>\u4e0efor\u5faa\u73af\u7c7b\u4f3c\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u4e5f\u53ef\u4ee5\u7ed3\u5408\u6761\u4ef6\u8bed\u53e5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [i for i in range(10) if i % 2 == 0]<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [0, 2, 4, 6, 8]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4ec5\u5c06\u5076\u6570\u6dfb\u52a0\u5230\u4e86\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528map\u51fd\u6570<\/h3>\n<\/p>\n<p><p>map\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u9ad8\u9636\u51fd\u6570\uff0c\u7528\u4e8e\u5c06\u4e00\u4e2a\u51fd\u6570\u5e94\u7528\u5230\u4e00\u4e2a\u5e8f\u5217\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u7840\u7528\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def square(x):<\/p>\n<p>    return x * x<\/p>\n<p>my_list = list(map(square, range(10)))<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5e73\u65b9\u51fd\u6570\uff0c\u5e76\u4f7f\u7528map\u51fd\u6570\u5c06\u5176\u5e94\u7528\u5230\u4ece0\u52309\u7684\u6570\u5b57\u5e8f\u5217\u4e2d\uff0c\u6700\u7ec8\u751f\u6210\u4e86\u4e00\u4e2a\u5305\u542b\u5e73\u65b9\u6570\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7ed3\u5408lambda\u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u7b80\u5316\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u6765\u66ff\u4ee3\u72ec\u7acb\u5b9a\u4e49\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = list(map(lambda x: x * x, range(10)))<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5176\u4ed6\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u4e09\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u5faa\u73af\u7ed9\u5217\u8868\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528while\u5faa\u73af<\/h4>\n<\/p>\n<p><p>\u867d\u7136for\u5faa\u73af\u662f\u6700\u5e38\u89c1\u7684\uff0c\u4f46while\u5faa\u73af\u4e5f\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = []<\/p>\n<p>i = 0<\/p>\n<p>while i &lt; 10:<\/p>\n<p>    my_list.append(i)<\/p>\n<p>    i += 1<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528NumPy\u5e93<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528NumPy\u5e93\uff0c\u5b83\u5728\u5904\u7406\u6570\u503c\u6570\u636e\u65f6\u6548\u7387\u66f4\u9ad8\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>my_array = np.arange(10)<\/p>\n<p>print(my_array)  # \u8f93\u51fa: [0 1 2 3 4 5 6 7 8 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>NumPy\u5e93\u7684arange\u51fd\u6570\u7c7b\u4f3c\u4e8ePython\u7684\u5185\u7f6erange\u51fd\u6570\uff0c\u4f46\u5b83\u751f\u6210\u7684\u662fNumPy\u6570\u7ec4\uff0c\u8fd9\u5728\u6570\u503c\u8ba1\u7b97\u4e2d\u66f4\u4e3a\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5e94\u7528\u573a\u666f\u4e0e\u9009\u62e9<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u4e2a\u4eba\u4e60\u60ef\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4ee3\u7801\u53ef\u8bfb\u6027<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u7b80\u5355\u7684\u5217\u8868\u8d4b\u503c\u64cd\u4f5c\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u7531\u4e8e\u5176\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u901a\u5e38\u662f\u9996\u9009\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [i for i in range(10)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u590d\u6742\u903b\u8f91<\/h4>\n<\/p>\n<p><p>\u5f53\u8d4b\u503c\u64cd\u4f5c\u6d89\u53ca\u590d\u6742\u7684\u903b\u8f91\u6216\u6761\u4ef6\u5224\u65ad\u65f6\uff0cfor\u5faa\u73af\u53ef\u80fd\u66f4\u4e3a\u5408\u9002\uff0c\u56e0\u4e3a\u5b83\u66f4\u76f4\u89c2\u4e14\u6613\u4e8e\u8c03\u8bd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = []<\/p>\n<p>for i in range(10):<\/p>\n<p>    if i % 2 == 0:<\/p>\n<p>        my_list.append(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u6027\u80fd\u9700\u6c42<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u89c4\u6a21\u7684\u6570\u636e\u5904\u7406\uff0cNumPy\u5e93\u901a\u5e38\u662f\u6700\u4f18\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u5728\u6267\u884c\u6570\u503c\u8ba1\u7b97\u65f6\u5177\u6709\u663e\u8457\u7684\u6027\u80fd\u4f18\u52bf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>my_array = np.arange(1000000)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4ee3\u7801\u4f18\u5316\u4e0e\u6027\u80fd\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0cPython\u7684\u5185\u7f6e\u65b9\u6cd5\u5df2\u7ecf\u8db3\u591f\u9ad8\u6548\uff0c\u4f46\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u6027\u80fd\u4f18\u5316\u4ecd\u7136\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u4e0d\u540c\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u80fd\u6709\u6240\u4e0d\u540c\u3002for\u5faa\u73af\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u4e3aO(n)\uff0c\u800cNumPy\u7684\u77e2\u91cf\u5316\u64cd\u4f5c\u7531\u4e8e\u5176\u5e95\u5c42\u5b9e\u73b0\u7684\u4f18\u5316\uff0c\u5f80\u5f80\u80fd\u63d0\u4f9b\u66f4\u9ad8\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5185\u5b58\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u5185\u5b58\u4f7f\u7528\u4e5f\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u5728\u751f\u6210\u5217\u8868\u7684\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u4f1a\u6d88\u8017\u8f83\u591a\u7684\u5185\u5b58\uff0c\u800cNumPy\u6570\u7ec4\u7531\u4e8e\u5176\u6570\u636e\u7ed3\u6784\u7684\u4f18\u5316\uff0c\u901a\u5e38\u80fd\u66f4\u9ad8\u6548\u5730\u5229\u7528\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5b9e\u9645\u5e94\u7528\u6848\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u7684\u5e94\u7528\u573a\u666f\uff0c\u4e0b\u9762\u5c06\u901a\u8fc7\u51e0\u4e2a\u5b9e\u9645\u6848\u4f8b\u6765\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u8fdb\u884c\u5217\u8868\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u751f\u6210\u5e73\u65b9\u6570\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u4e00\u4e2a\u5305\u542b\u524d10\u4e2a\u6574\u6570\u7684\u5e73\u65b9\u6570\u7684\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">squares = [i  2 for i in range(10)]<\/p>\n<p>print(squares)  # \u8f93\u51fa: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8fc7\u6ee4\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u4ece\u4e00\u4e2a\u5305\u542b\u6574\u6570\u7684\u5217\u8868\u4e2d\u7b5b\u9009\u51fa\u5076\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>evens = [num for num in numbers if num % 2 == 0]<\/p>\n<p>print(evens)  # \u8f93\u51fa: [2, 4, 6, 8, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u7ed3\u5408\u51fd\u6570\u4e0emap<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528map\u51fd\u6570\u5c06\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8f6c\u6362\u4e3a\u5176\u5e73\u65b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>squares = list(map(lambda x: x  2, numbers))<\/p>\n<p>print(squares)  # \u8f93\u51fa: [1, 4, 9, 16, 25]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001Python\u4e2d\u7684\u4e00\u4e9b\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u8fdb\u4e00\u6b65\u63d0\u5347\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\uff0cPython\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u7528\u6cd5\u548c\u6280\u5de7\uff0c\u53ef\u4ee5\u5728\u5217\u8868\u8d4b\u503c\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u4e0e\u5217\u8868\u63a8\u5bfc\u5f0f\u7c7b\u4f3c\uff0c\u4f46\u5b83\u4e0d\u662f\u7acb\u5373\u751f\u6210\u5217\u8868\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\uff0c\u4ece\u800c\u5728\u9700\u8981\u65f6\u624d\u751f\u6210\u5143\u7d20\uff0c\u8282\u7701\u5185\u5b58\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">gen = (i for i in range(10))<\/p>\n<p>for val in gen:<\/p>\n<p>    print(val, end=&#39; &#39;)  # \u8f93\u51fa: 0 1 2 3 4 5 6 7 8 9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528itertools\u6a21\u5757<\/h4>\n<\/p>\n<p><p>itertools\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7528\u4e8e\u64cd\u4f5c\u8fed\u4ee3\u5668\u7684\u51fd\u6570\uff0c\u975e\u5e38\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u8fed\u4ee3\u903b\u8f91\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<h2><strong>\u751f\u6210\u4ece0\u52309\u7684\u65e0\u9650\u5faa\u73af\u8fed\u4ee3\u5668<\/strong><\/h2>\n<p>cycle_iter = itertools.cycle(range(10))<\/p>\n<p>for _ in range(15):<\/p>\n<p>    print(next(cycle_iter), end=&#39; &#39;)  # \u8f93\u51fa: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\uff0c\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u4e86Python\u4e2d\u5faa\u73af\u7ed9\u5217\u8868\u8d4b\u503c\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ecfor\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u548cmap\u51fd\u6570\u7b49\u3002<strong>\u5bf9\u4e8e\u7b80\u5355\u7684\u8d4b\u503c\u64cd\u4f5c\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u7531\u4e8e\u5176\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u901a\u5e38\u662f\u9996\u9009\u3002\u5bf9\u4e8e\u590d\u6742\u7684\u903b\u8f91\u6216\u6761\u4ef6\u5224\u65ad\uff0cfor\u5faa\u73af\u66f4\u4e3a\u76f4\u89c2\u4e14\u6613\u4e8e\u8c03\u8bd5\u3002\u800c\u5728\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u65f6\uff0cNumPy\u5e93\u5f80\u5f80\u80fd\u63d0\u4f9b\u663e\u8457\u7684\u6027\u80fd\u4f18\u52bf\u3002<\/strong> <\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u7528\u6cd5\u548c\u6280\u5de7\uff0c\u5982\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u548citertools\u6a21\u5757\uff0c\u8fdb\u4e00\u6b65\u63d0\u5347\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u624d\u80fd\u7f16\u5199\u51fa\u9ad8\u6548\u3001\u53ef\u7ef4\u62a4\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u503c\u7684\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u5faa\u73af\u6765\u5feb\u901f\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u503c\u7684\u5217\u8868\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u8bed\u6cd5\u975e\u5e38\u7b80\u6d01\uff0c\u4f8b\u5982\u53ef\u4ee5\u4f7f\u7528<code>[x for x in range(10)]<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u5305\u542b0\u52309\u7684\u5217\u8868\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u8d4b\u503c\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = []\nfor i in range(10):\n    my_list.append(i)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u4f1a\u521b\u5efa\u4e00\u4e2a\u5305\u542b0\u52309\u7684\u5217\u8868\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5faa\u73af\u4e2d\u52a8\u6001\u66f4\u65b0\u5217\u8868\u7684\u503c\uff1f<\/strong><br \/>\u5728\u5faa\u73af\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5217\u8868\u5143\u7d20\u5e76\u66f4\u65b0\u5176\u503c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u5c06\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u4e58\u4ee52\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]\nfor i in range(len(my_list)):\n    my_list[i] *= 2\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u66f4\u65b0<code>my_list<\/code>\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u4f7f\u5176\u53d8\u4e3a\u539f\u6765\u7684\u4e24\u500d\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u4f7f\u7528<code>enumerate<\/code>\u5728\u5faa\u73af\u4e2d\u540c\u65f6\u83b7\u53d6\u7d22\u5f15\u548c\u503c\uff1f<\/strong><br \/>\u4f7f\u7528<code>enumerate<\/code>\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u540c\u65f6\u83b7\u53d6\u5143\u7d20\u7684\u7d22\u5f15\u548c\u503c\uff0c\u8fd9\u5bf9\u4e8e\u9700\u8981\u540c\u65f6\u4fee\u6539\u5217\u8868\u548c\u77e5\u9053\u5143\u7d20\u4f4d\u7f6e\u7684\u573a\u666f\u975e\u5e38\u6709\u7528\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]\nfor index, value in enumerate(my_list):\n    my_list[index] = value.upper()\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u628a\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3a\u5927\u5199\u5b57\u6bcd\uff0c\u540c\u65f6\u4fdd\u6301\u5176\u539f\u6765\u7684\u7d22\u5f15\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5faa\u73af\u7ed9\u5217\u8868\u8d4b\u503c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ecfor\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u548cmap\u51fd\u6570\u7b49\u3002 \u5176\u4e2d\uff0cfor [&hellip;]","protected":false},"author":3,"featured_media":1185166,"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\/1185159"}],"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=1185159"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1185159\/revisions"}],"predecessor-version":[{"id":1185169,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1185159\/revisions\/1185169"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1185166"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1185159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1185159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1185159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}