{"id":979142,"date":"2024-12-27T06:43:57","date_gmt":"2024-12-26T22:43:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/979142.html"},"modified":"2024-12-27T06:43:59","modified_gmt":"2024-12-26T22:43:59","slug":"python%e5%a6%82%e4%bd%95%e9%81%8d%e5%8e%86list%e6%9c%80%e5%bf%ab","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/979142.html","title":{"rendered":"python\u5982\u4f55\u904d\u5386list\u6700\u5feb"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205127\/e6d4c96e-fd29-4474-a4f7-7d6e0034386f.webp\" alt=\"python\u5982\u4f55\u904d\u5386list\u6700\u5feb\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u904d\u5386list\u6700\u5feb\u7684\u65b9\u6cd5\u901a\u5e38\u662f\u4f7f\u7528for\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001enumerate\u51fd\u6570\u3002\u4f7f\u7528for\u5faa\u73af\u662f\u6700\u5e38\u7528\u548c\u76f4\u89c2\u7684\u65b9\u6cd5\uff1b\u5217\u8868\u63a8\u5bfc\u5f0f\u5728\u9700\u8981\u751f\u6210\u65b0\u5217\u8868\u65f6\u6548\u7387\u9ad8\uff1benumerate\u51fd\u6570\u5728\u9700\u8981\u5143\u7d20\u7d22\u5f15\u65f6\u975e\u5e38\u6709\u7528\u3002<\/strong>\u5176\u4e2d\uff0cfor\u5faa\u73af\u662f\u6700\u57fa\u7840\u7684\u904d\u5386\u65b9\u6cd5\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u63d0\u4f9b\u66f4\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u53ca\u5176\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001FOR\u5faa\u73af<\/p>\n<\/p>\n<p><p>For\u5faa\u73af\u662f\u904d\u5386\u5217\u8868\u7684\u7ecf\u5178\u65b9\u6cd5\u3002\u5b83\u7b80\u5355\u3001\u76f4\u89c2\uff0c\u5e76\u4e14\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u6027\u80fd\u8868\u73b0\u826f\u597d\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u7840\u4f7f\u7528<\/strong><\/p>\n<p>For\u5faa\u73af\u5141\u8bb8\u6211\u4eec\u76f4\u63a5\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u65e0\u9700\u989d\u5916\u7684\u7d22\u5f15\u64cd\u4f5c\u3002\u5176\u8bed\u6cd5\u7b80\u5355\uff0c\u6613\u4e8e\u7406\u89e3\uff0c\u975e\u5e38\u9002\u5408\u521d\u5b66\u8005\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>for item in my_list:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6548\u7387\u4e0e\u6027\u80fd<\/strong><\/p>\n<p>\u867d\u7136for\u5faa\u73af\u4e0d\u662f\u6700\u5feb\u7684\uff0c\u4f46\u5b83\u7684\u6027\u80fd\u901a\u5e38\u8db3\u4ee5\u6ee1\u8db3\u5927\u591a\u6570\u9700\u6c42\u3002\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u5217\u8868\uff0cfor\u5faa\u73af\u53ef\u80fd\u4f1a\u663e\u5f97\u7a0d\u6162\uff0c\u56e0\u6b64\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u53ef\u4ee5\u8003\u8651\u5176\u4ed6\u66f4\u9ad8\u6548\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u53ef\u4ee5\u5728\u4e00\u884c\u5185\u751f\u6210\u4e00\u4e2a\u65b0\u5217\u8868\u3002\u5b83\u5728\u5904\u7406\u7b80\u5355\u7684\u5faa\u73af\u548c\u6620\u5c04\u4efb\u52a1\u65f6\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u65b0\u5217\u8868<\/strong><\/p>\n<p>\u5f53\u4f60\u9700\u8981\u57fa\u4e8e\u539f\u6709\u5217\u8868\u751f\u6210\u4e00\u4e2a\u65b0\u5217\u8868\u65f6\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u4e2a\u6781\u4e3a\u9ad8\u6548\u7684\u9009\u62e9\u3002\u5b83\u4e0d\u4ec5\u7b80\u5316\u4e86\u4ee3\u7801\uff0c\u8fd8\u63d0\u9ad8\u4e86\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>new_list = [item * 2 for item in my_list]<\/p>\n<p>print(new_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\u4f18\u52bf<\/strong><\/p>\n<p>\u5217\u8868\u63a8\u5bfc\u5f0f\u5728\u6027\u80fd\u4e0a\u901a\u5e38\u4f18\u4e8e\u4f20\u7edffor\u5faa\u73af\uff0c\u56e0\u4e3a\u5b83\u5728\u5e95\u5c42\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u51cf\u5c11\u4e86\u5faa\u73af\u8fc7\u7a0b\u4e2d\u4e0d\u5fc5\u8981\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001ENUMERATE\u51fd\u6570<\/p>\n<\/p>\n<p><p>Enumerate\u51fd\u6570\u5728\u904d\u5386\u5217\u8868\u7684\u540c\u65f6\u63d0\u4f9b\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u9002\u5408\u5728\u9700\u8981\u8bbf\u95ee\u5143\u7d20\u4f4d\u7f6e\u7684\u573a\u5408\u4f7f\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u65b9\u6cd5<\/strong><\/p>\n<p>Enumerate\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5bf9\u8c61\uff0c\u4ea7\u751f\u4e00\u5bf9\u5305\u62ec\u7d22\u5f15\u548c\u5143\u7d20\u7684\u5143\u7ec4\u3002\u8fd9\u6837\u53ef\u4ee5\u5728\u904d\u5386\u7684\u540c\u65f6\u65b9\u4fbf\u5730\u83b7\u53d6\u5143\u7d20\u7684\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p>for index, value in enumerate(my_list):<\/p>\n<p>    print(index, value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u52bf<\/strong><\/p>\n<p>\u4f7f\u7528enumerate\u907f\u514d\u4e86\u624b\u52a8\u7ba1\u7406\u7d22\u5f15\u7684\u9ebb\u70e6\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u53ef\u8bfb\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001ITERTOOLS\u5e93<\/p>\n<\/p>\n<p><p>Itertools\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u9ad8\u6548\u7684\u8fed\u4ee3\u5de5\u5177\uff0c\u53ef\u4ee5\u5728\u590d\u6742\u7684\u8fed\u4ee3\u4efb\u52a1\u4e2d\u63d0\u4f9b\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>CH<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>N\u51fd\u6570<\/strong><\/p>\n<p>Chain\u51fd\u6570\u53ef\u4ee5\u5c06\u591a\u4e2a\u5217\u8868\u6216\u8fed\u4ee3\u5668\u4e32\u8054\u8d77\u6765\u8fdb\u884c\u904d\u5386\uff0c\u907f\u514d\u4e86\u591a\u91cd\u5faa\u73af\u7684\u590d\u6742\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>list1 = [1, 2, 3]<\/p>\n<p>list2 = [4, 5, 6]<\/p>\n<p>for item in itertools.chain(list1, list2):<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5176\u4ed6\u5de5\u5177<\/strong><\/p>\n<p>Itertools\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u6709\u7528\u7684\u5de5\u5177\uff0c\u6bd4\u5982islice\u3001cycle\u7b49\uff0c\u5b83\u4eec\u53ef\u4ee5\u5728\u7279\u5b9a\u7684\u573a\u666f\u4e0b\u63d0\u9ad8\u904d\u5386\u6548\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001NUMPY\u5e93<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6570\u503c\u578b\u5217\u8868\uff0cNumPy\u5e93\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u904d\u5386\u7684\u6548\u7387\u3002NumPy\u7684\u6570\u7ec4\u64cd\u4f5c\u5728\u5e95\u5c42\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u9002\u5408\u5927\u89c4\u6a21\u6570\u503c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u7ec4\u64cd\u4f5c<\/strong><\/p>\n<p>NumPy\u63d0\u4f9b\u7684\u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b8c\u6210\u590d\u6742\u7684\u8ba1\u7b97\u4efb\u52a1\uff0c\u907f\u514d\u4e86for\u5faa\u73af\u7684\u5f00\u9500\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>my_array = np.array([1, 2, 3, 4, 5])<\/p>\n<p>print(my_array * 2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\u8868\u73b0<\/strong><\/p>\n<p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u503c\u6570\u636e\u65f6\uff0cNumPy\u7684\u6548\u7387\u4f18\u52bf\u975e\u5e38\u660e\u663e\uff0c\u662f\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u7684\u9996\u9009\u5de5\u5177\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u7ed3\u8bba<\/p>\n<\/p>\n<p><p>\u904d\u5386list\u7684\u6548\u7387\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u4f7f\u7528\u573a\u666f\u548c\u9700\u6c42\u3002\u5728\u4e00\u822c\u60c5\u51b5\u4e0b\uff0cfor\u5faa\u73af\u8db3\u4ee5\u80dc\u4efb\u4efb\u52a1\uff0c\u4f46\u5728\u9700\u8981\u751f\u6210\u65b0\u5217\u8868\u6216\u5904\u7406\u590d\u6742\u8fed\u4ee3\u4efb\u52a1\u65f6\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u548citertools\u5e93\u53ef\u4ee5\u63d0\u4f9b\u66f4\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002\u800c\u5bf9\u4e8e\u6570\u503c\u578b\u6570\u636e\uff0cNumPy\u5e93\u7684\u6027\u80fd\u4f18\u52bf\u65e0\u53ef\u66ff\u4ee3\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u4fdd\u8bc1\u4ee3\u7801\u7b80\u6d01\u6027\u7684\u540c\u65f6\uff0c\u83b7\u5f97\u6700\u4f73\u7684\u6027\u80fd\u8868\u73b0\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u63d0\u9ad8\u904d\u5386\u5217\u8868\u7684\u901f\u5ea6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u904d\u5386\u5217\u8868\u7684\u901f\u5ea6\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u63d0\u5347\u3002\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u6765\u66ff\u4ee3\u4f20\u7edf\u7684for\u5faa\u73af\uff0c\u901a\u5e38\u80fd\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002\u6b64\u5916\uff0c\u8003\u8651\u4f7f\u7528NumPy\u5e93\uff0c\u5b83\u4e13\u4e3a\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u800c\u8bbe\u8ba1\uff0c\u80fd\u591f\u66f4\u5feb\u5730\u6267\u884c\u6570\u7ec4\u64cd\u4f5c\u3002\u901a\u8fc7\u9002\u5f53\u9009\u62e9\u6570\u636e\u7ed3\u6784\uff0c\u5982\u96c6\u5408\uff08set\uff09\u6216\u5b57\u5178\uff08dict\uff09\uff0c\u4e5f\u53ef\u4ee5\u63d0\u9ad8\u67e5\u627e\u548c\u904d\u5386\u7684\u6548\u7387\u3002<\/p>\n<p><strong>\u5728\u904d\u5386\u5217\u8868\u65f6\uff0c\u4f7f\u7528\u54ea\u4e9b\u5185\u7f6e\u51fd\u6570\u53ef\u4ee5\u63d0\u9ad8\u6548\u7387\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u51fd\u6570\uff0c\u5982<code>map()<\/code>\u548c<code>filter()<\/code>\uff0c\u5b83\u4eec\u53ef\u4ee5\u5728\u904d\u5386\u5217\u8868\u65f6\u663e\u8457\u51cf\u5c11\u4ee3\u7801\u7684\u884c\u6570\uff0c\u5e76\u63d0\u9ad8\u6267\u884c\u901f\u5ea6\u3002\u8fd9\u4e9b\u51fd\u6570\u662f\u7528C\u8bed\u8a00\u5b9e\u73b0\u7684\uff0c\u56e0\u6b64\u5728\u5904\u7406\u5927\u6570\u636e\u65f6\uff0c\u5b83\u4eec\u7684\u6027\u80fd\u901a\u5e38\u4f18\u4e8ePython\u7684for\u5faa\u73af\u3002\u540c\u65f6\uff0c\u4f7f\u7528\u5185\u7f6e\u7684<code>sum()<\/code>\u3001<code>min()<\/code>\u548c<code>max()<\/code>\u7b49\u51fd\u6570\u6765\u5904\u7406\u5217\u8868\u4e5f\u80fd\u63d0\u5347\u6548\u7387\u3002<\/p>\n<p><strong>\u5728\u904d\u5386\u5927\u578b\u5217\u8868\u65f6\uff0c\u6709\u54ea\u4e9b\u4f18\u5316\u6280\u5de7\u53ef\u4ee5\u4f7f\u7528\uff1f<\/strong><br \/>\u5904\u7406\u5927\u578b\u5217\u8868\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u64cd\u4f5c\u6765\u4f18\u5316\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u907f\u514d\u5728\u5faa\u73af\u4e2d\u8fdb\u884c\u590d\u6742\u7684\u8ba1\u7b97\u548c\u6570\u636e\u8bbf\u95ee\uff0c\u5c3d\u91cf\u5c06\u8fd9\u4e9b\u64cd\u4f5c\u79fb\u51fa\u5faa\u73af\u3002\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\u53ef\u4ee5\u52a0\u5feb\u8bbf\u95ee\u901f\u5ea6\uff0c\u6b64\u5916\uff0c\u8003\u8651\u5c06\u6570\u636e\u5206\u5757\u5904\u7406\uff0c\u4ee5\u4fbf\u5728\u5185\u5b58\u4e2d\u66f4\u6709\u6548\u5730\u7ba1\u7406\u5927\u578b\u6570\u636e\u96c6\u3002\u8fd9\u4e9b\u6280\u5de7\u53ef\u4ee5\u5e2e\u52a9\u63d0\u5347\u904d\u5386\u5927\u578b\u5217\u8868\u7684\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u904d\u5386list\u6700\u5feb\u7684\u65b9\u6cd5\u901a\u5e38\u662f\u4f7f\u7528for\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001enumerate\u51fd\u6570\u3002\u4f7f\u7528for\u5faa [&hellip;]","protected":false},"author":3,"featured_media":979146,"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\/979142"}],"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=979142"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/979142\/revisions"}],"predecessor-version":[{"id":979149,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/979142\/revisions\/979149"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/979146"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=979142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=979142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=979142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}