{"id":1068343,"date":"2024-12-31T16:42:41","date_gmt":"2024-12-31T08:42:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1068343.html"},"modified":"2024-12-31T16:42:43","modified_gmt":"2024-12-31T08:42:43","slug":"python%e6%95%b0%e7%bb%84%e5%a6%82%e4%bd%95%e4%b8%80%e4%b8%80%e5%af%b9%e5%ba%94","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1068343.html","title":{"rendered":"Python\u6570\u7ec4\u5982\u4f55\u4e00\u4e00\u5bf9\u5e94"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/8b278f27-7bd6-48cb-bd7b-68499532ca08.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"Python\u6570\u7ec4\u5982\u4f55\u4e00\u4e00\u5bf9\u5e94\" \/><\/p>\n<p><p> <strong>Python\u6570\u7ec4\u4e00\u4e00\u5bf9\u5e94\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570zip()\u3001\u4f7f\u7528enumerate()\u51fd\u6570\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3002<\/strong> \u672c\u6587\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u3002\u4f7f\u7528\u5185\u7f6e\u51fd\u6570zip()\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u5b83\u5c06\u591a\u4e2a\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u6253\u5305\u6210\u5143\u7ec4\uff0c\u8fd9\u4e9b\u5143\u7ec4\u53ef\u4ee5\u8f7b\u677e\u5730\u904d\u5386\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u89e3\u91ca\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u8fdb\u884c\u6570\u7ec4\u7684\u4e00\u4e00\u5bf9\u5e94\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528zip()\u51fd\u6570<\/h3>\n<\/p>\n<p><h4>1\u3001\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>Python\u4e2d\u7684zip()\u51fd\u6570\u662f\u5c06\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u7b49\uff09\u6309\u5bf9\u5e94\u4f4d\u7f6e\u7ec4\u5408\u6210\u4e00\u4e2a\u5143\u7ec4\uff0c\u5e76\u8fd4\u56de\u8fd9\u4e9b\u5143\u7ec4\u7ec4\u6210\u7684\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002zip()\u51fd\u6570\u5728\u5904\u7406\u591a\u7ec4\u6570\u636e\u65f6\u975e\u5e38\u6709\u7528\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u591a\u4e2a\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u4e00\u4e00\u5bf9\u5e94\u8d77\u6765\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e24\u4e2a\u6570\u7ec4<\/p>\n<p>array1 = [1, 2, 3, 4]<\/p>\n<p>array2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]<\/p>\n<h2><strong>\u4f7f\u7528zip\u51fd\u6570\u5c06\u4e24\u4e2a\u6570\u7ec4\u5bf9\u5e94\u7684\u5143\u7d20\u6253\u5305\u6210\u4e00\u4e2a\u5143\u7ec4<\/strong><\/h2>\n<p>zipped = zip(array1, array2)<\/p>\n<h2><strong>\u904d\u5386\u5e76\u6253\u5370\u7ed3\u679c<\/strong><\/h2>\n<p>for item in zipped:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u7b80\u5355\u6613\u7528\uff0c\u4ee3\u7801\u7b80\u6d01\uff0c\u9002\u7528\u4e8e\u591a\u7ec4\u6570\u636e\u7684\u5bf9\u5e94\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong> \u5982\u679c\u6570\u7ec4\u957f\u5ea6\u4e0d\u4e00\u81f4\uff0c\u4f1a\u5bfc\u81f4\u8f83\u77ed\u7684\u6570\u7ec4\u51b3\u5b9a\u4e86\u8f93\u51fa\u7684\u957f\u5ea6\uff0c\u5269\u4f59\u7684\u5143\u7d20\u4f1a\u88ab\u5ffd\u7565\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528enumerate()\u51fd\u6570<\/h3>\n<\/p>\n<p><h4>1\u3001\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>enumerate()\u51fd\u6570\u7528\u4e8e\u904d\u5386\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5e76\u5728\u904d\u5386\u5143\u7d20\u7684\u540c\u65f6\u83b7\u53d6\u5143\u7d20\u7684\u7d22\u5f15\u3002\u901a\u8fc7\u7d22\u5f15\uff0c\u53ef\u4ee5\u5b9e\u73b0\u6570\u7ec4\u7684\u4e00\u4e00\u5bf9\u5e94\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u9700\u8981\u540c\u65f6\u83b7\u53d6\u5143\u7d20\u7d22\u5f15\u548c\u503c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e24\u4e2a\u6570\u7ec4<\/p>\n<p>array1 = [1, 2, 3, 4]<\/p>\n<p>array2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]<\/p>\n<h2><strong>\u4f7f\u7528enumerate\u51fd\u6570\u904d\u5386\u7b2c\u4e00\u4e2a\u6570\u7ec4\uff0c\u5e76\u901a\u8fc7\u7d22\u5f15\u83b7\u53d6\u7b2c\u4e8c\u4e2a\u6570\u7ec4\u7684\u5bf9\u5e94\u5143\u7d20<\/strong><\/h2>\n<p>for index, value in enumerate(array1):<\/p>\n<p>    print(value, array2[index])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u9002\u7528\u4e8e\u9700\u8981\u540c\u65f6\u83b7\u53d6\u5143\u7d20\u7d22\u5f15\u548c\u503c\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong> \u4ee3\u7801\u76f8\u5bf9\u590d\u6742\uff0c\u9002\u7528\u4e8e\u6570\u7ec4\u957f\u5ea6\u4e00\u81f4\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><h4>1\u3001\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662fPython\u4e2d\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u7528\u4e8e\u521b\u5efa\u65b0\u7684\u5217\u8868\u3002\u901a\u8fc7\u5c06\u591a\u4e2a\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u6309\u5bf9\u5e94\u4f4d\u7f6e\u7ec4\u5408\uff0c\u53ef\u4ee5\u5b9e\u73b0\u6570\u7ec4\u7684\u4e00\u4e00\u5bf9\u5e94\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e24\u4e2a\u6570\u7ec4<\/p>\n<p>array1 = [1, 2, 3, 4]<\/p>\n<p>array2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u4e24\u4e2a\u6570\u7ec4\u5bf9\u5e94\u7684\u5143\u7d20\u7ec4\u5408\u6210\u4e00\u4e2a\u5143\u7ec4<\/strong><\/h2>\n<p>zipped = [(array1[i], array2[i]) for i in range(len(array1))]<\/p>\n<h2><strong>\u904d\u5386\u5e76\u6253\u5370\u7ed3\u679c<\/strong><\/h2>\n<p>for item in zipped:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u4ee3\u7801\u7b80\u6d01\uff0c\u9002\u7528\u4e8e\u6570\u7ec4\u957f\u5ea6\u4e00\u81f4\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong> \u9700\u8981\u663e\u5f0f\u5730\u904d\u5386\u7d22\u5f15\uff0c\u4e0d\u9002\u7528\u4e8e\u6570\u7ec4\u957f\u5ea6\u4e0d\u4e00\u81f4\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528itertools\u5e93<\/h3>\n<\/p>\n<p><h4>1\u3001\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>itertools\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u591a\u4e2a\u7528\u4e8e\u64cd\u4f5c\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u51fd\u6570\u3002\u901a\u8fc7itertools.zip_longest()\u51fd\u6570\uff0c\u53ef\u4ee5\u89e3\u51b3\u6570\u7ec4\u957f\u5ea6\u4e0d\u4e00\u81f4\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<h2><strong>\u5b9a\u4e49\u4e24\u4e2a\u6570\u7ec4<\/strong><\/h2>\n<p>array1 = [1, 2, 3, 4]<\/p>\n<p>array2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<h2><strong>\u4f7f\u7528itertools.zip_longest\u51fd\u6570\u5c06\u4e24\u4e2a\u6570\u7ec4\u5bf9\u5e94\u7684\u5143\u7d20\u6253\u5305\u6210\u4e00\u4e2a\u5143\u7ec4<\/strong><\/h2>\n<p>zipped = itertools.zip_longest(array1, array2, fillvalue=None)<\/p>\n<h2><strong>\u904d\u5386\u5e76\u6253\u5370\u7ed3\u679c<\/strong><\/h2>\n<p>for item in zipped:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u9002\u7528\u4e8e\u6570\u7ec4\u957f\u5ea6\u4e0d\u4e00\u81f4\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u6307\u5b9a\u586b\u5145\u503c\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong> \u9700\u8981\u5bfc\u5165itertools\u6a21\u5757\uff0c\u4ee3\u7801\u76f8\u5bf9\u590d\u6742\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528pandas\u5e93<\/h3>\n<\/p>\n<p><h4>1\u3001\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>pandas\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u901a\u8fc7\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3aDataFrame\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0\u6570\u7ec4\u7684\u4e00\u4e00\u5bf9\u5e94\u3002pandas\u5e93\u7279\u522b\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5b9a\u4e49\u4e24\u4e2a\u6570\u7ec4<\/strong><\/h2>\n<p>array1 = [1, 2, 3, 4]<\/p>\n<p>array2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]<\/p>\n<h2><strong>\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3aDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame({&#39;Array1&#39;: array1, &#39;Array2&#39;: array2})<\/p>\n<h2><strong>\u6253\u5370DataFrame<\/strong><\/h2>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong> \u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u4efb\u52a1\uff0c\u63d0\u4f9b\u4e30\u5bcc\u7684\u6570\u636e\u64cd\u4f5c\u529f\u80fd\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong> \u9700\u8981\u5bfc\u5165pandas\u5e93\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6027\u80fd\u5bf9\u6bd4<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u65f6\uff0c\u8fd8\u9700\u8981\u8003\u8651\u6027\u80fd\u95ee\u9898\u3002\u4e0b\u9762\u5c06\u5bf9\u4e0a\u8ff0\u65b9\u6cd5\u8fdb\u884c\u6027\u80fd\u5bf9\u6bd4\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6d4b\u8bd5\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>array1 = list(range(1000000))<\/p>\n<p>array2 = list(range(1000000, 2000000))<\/p>\n<h2><strong>\u4f7f\u7528zip\u51fd\u6570<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>zipped = zip(array1, array2)<\/p>\n<p>for item in zipped:<\/p>\n<p>    pass<\/p>\n<p>print(&quot;zip\u51fd\u6570\u8017\u65f6\uff1a&quot;, time.time() - start_time)<\/p>\n<h2><strong>\u4f7f\u7528enumerate\u51fd\u6570<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>for index, value in enumerate(array1):<\/p>\n<p>    _ = value, array2[index]<\/p>\n<p>print(&quot;enumerate\u51fd\u6570\u8017\u65f6\uff1a&quot;, time.time() - start_time)<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>zipped = [(array1[i], array2[i]) for i in range(len(array1))]<\/p>\n<p>print(&quot;\u5217\u8868\u63a8\u5bfc\u5f0f\u8017\u65f6\uff1a&quot;, time.time() - start_time)<\/p>\n<h2><strong>\u4f7f\u7528itertools.zip_longest\u51fd\u6570<\/strong><\/h2>\n<p>import itertools<\/p>\n<p>start_time = time.time()<\/p>\n<p>zipped = itertools.zip_longest(array1, array2, fillvalue=None)<\/p>\n<p>for item in zipped:<\/p>\n<p>    pass<\/p>\n<p>print(&quot;itertools.zip_longest\u51fd\u6570\u8017\u65f6\uff1a&quot;, time.time() - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6d4b\u8bd5\u7ed3\u679c<\/h4>\n<\/p>\n<p><p>\u6839\u636e\u6d4b\u8bd5\u7ed3\u679c\uff0c\u4f7f\u7528zip()\u51fd\u6570\u7684\u6027\u80fd\u6700\u4f73\uff0c\u5176\u6b21\u662fenumerate()\u51fd\u6570\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\uff0citertools.zip_longest()\u51fd\u6570\u7684\u6027\u80fd\u6700\u5dee\u3002\u56e0\u6b64\uff0c\u5728\u5904\u7406\u6570\u7ec4\u957f\u5ea6\u4e00\u81f4\u7684\u60c5\u51b5\u4e0b\uff0c\u63a8\u8350\u4f7f\u7528zip()\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848<\/h3>\n<\/p>\n<p><h4>1\u3001\u6570\u7ec4\u957f\u5ea6\u4e0d\u4e00\u81f4<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6570\u7ec4\u957f\u5ea6\u4e0d\u4e00\u81f4\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528itertools.zip_longest()\u51fd\u6570\uff0c\u901a\u8fc7\u6307\u5b9a\u586b\u5145\u503c\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6570\u7ec4\u7c7b\u578b\u4e0d\u4e00\u81f4<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6570\u7ec4\u7c7b\u578b\u4e0d\u4e00\u81f4\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u663e\u5f0f\u5730\u8f6c\u6362\u6570\u7ec4\u7c7b\u578b\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528map()\u51fd\u6570\u5c06\u6570\u7ec4\u5143\u7d20\u8f6c\u6362\u4e3a\u76f8\u540c\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5185\u5b58\u5360\u7528\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6570\u7ec4\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u5185\u5b58\u5360\u7528\u95ee\u9898\u3002\u53ef\u4ee5\u901a\u8fc7\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u66ff\u4ee3\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u51cf\u5c11\u5185\u5b58\u5360\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u66ff\u4ee3\u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n<p>zipped = ((array1[i], array2[i]) for i in range(len(array1)))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u4e2d\u5b9e\u73b0\u6570\u7ec4\u4e00\u4e00\u5bf9\u5e94\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528zip()\u51fd\u6570\u3001enumerate()\u51fd\u6570\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001itertools\u5e93\u548cpandas\u5e93\u3002\u901a\u8fc7\u5bf9\u6bd4\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\u548c\u6027\u80fd\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5728\u5904\u7406\u6570\u7ec4\u957f\u5ea6\u4e0d\u4e00\u81f4\u3001\u6570\u7ec4\u7c7b\u578b\u4e0d\u4e00\u81f4\u548c\u5185\u5b58\u5360\u7528\u95ee\u9898\u65f6\uff0c\u63d0\u4f9b\u4e86\u76f8\u5e94\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u6269\u5c55\u9605\u8bfb<\/h3>\n<\/p>\n<p><h4>1\u3001Python\u5185\u7f6e\u51fd\u6570\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u4e86\u89e3\u66f4\u591a\u5173\u4e8ePython\u5185\u7f6e\u51fd\u6570\u7684\u4fe1\u606f\uff0c\u53ef\u4ee5\u53c2\u8003Python\u5b98\u65b9\u6587\u6863\uff1a<\/p>\n<\/p>\n<p><p><a href=\"https:\/\/docs.python.org\/3\/library\/functions.html\">Python Built-in Functions<\/a><\/p>\n<\/p>\n<p><h4>2\u3001itertools\u6a21\u5757\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u4e86\u89e3\u66f4\u591a\u5173\u4e8eitertools\u6a21\u5757\u7684\u4fe1\u606f\uff0c\u53ef\u4ee5\u53c2\u8003Python\u5b98\u65b9\u6587\u6863\uff1a<\/p>\n<\/p>\n<p><p><a href=\"https:\/\/docs.python.org\/3\/library\/itertools.html\">itertools \u2014 Functions creating iterators for efficient looping<\/a><\/p>\n<\/p>\n<p><h4>3\u3001pandas\u5e93\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u4e86\u89e3\u66f4\u591a\u5173\u4e8epandas\u5e93\u7684\u4fe1\u606f\uff0c\u53ef\u4ee5\u53c2\u8003pandas\u5b98\u65b9\u6587\u6863\uff1a<\/p>\n<\/p>\n<p><p><a href=\"https:\/\/pandas.pydata.org\/pandas-docs\/stable\/\">pandas Documentation<\/a><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u9605\u8bfb\u8fd9\u4e9b\u6587\u6863\uff0c\u53ef\u4ee5\u6df1\u5165\u4e86\u89e3Python\u4e2d\u5b9e\u73b0\u6570\u7ec4\u4e00\u4e00\u5bf9\u5e94\u7684\u5404\u79cd\u65b9\u6cd5\u548c\u6280\u5de7\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u5982\u679c\u6709\u4efb\u4f55\u95ee\u9898\u6216\u5efa\u8bae\uff0c\u6b22\u8fce\u5728\u8bc4\u8bba\u533a\u7559\u8a00\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u4e24\u4e2a\u6570\u7ec4\u7684\u5143\u7d20\u4e00\u4e00\u5bf9\u5e94\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>zip()<\/code>\u51fd\u6570\u5c06\u4e24\u4e2a\u6570\u7ec4\uff08\u5217\u8868\uff09\u8fdb\u884c\u4e00\u4e00\u5bf9\u5e94\u3002<code>zip()<\/code>\u51fd\u6570\u4f1a\u5c06\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\u6253\u5305\u6210\u4e00\u4e2a\u5143\u7ec4\uff0c\u8fd4\u56de\u4e00\u4e2a\u53ef\u8fed\u4ee3\u7684zip\u5bf9\u8c61\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e24\u4e2a\u5217\u8868<code>list1 = [1, 2, 3]<\/code>\u548c<code>list2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>for a, b in zip(list1, list2)<\/code>\u904d\u5386\u8fd9\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u5bf9\u5e94\u5143\u7d20\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u957f\u5ea6\u4e0d\u76f8\u7b49\u7684\u6570\u7ec4\uff1f<\/strong><br \/>\u5f53\u4e24\u4e2a\u6570\u7ec4\u957f\u5ea6\u4e0d\u76f8\u7b49\u65f6\uff0c<code>zip()<\/code>\u51fd\u6570\u4f1a\u6839\u636e\u6700\u77ed\u7684\u6570\u7ec4\u8fdb\u884c\u914d\u5bf9\uff0c\u8d85\u51fa\u90e8\u5206\u5c06\u88ab\u5ffd\u7565\u3002\u5982\u679c\u5e0c\u671b\u4fdd\u7559\u6240\u6709\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>itertools.zip_longest()<\/code>\uff0c\u8fd9\u4e2a\u51fd\u6570\u4f1a\u586b\u5145\u7f3a\u5931\u503c\uff0c\u9ed8\u8ba4\u4e3a<code>None<\/code>\uff0c\u4e5f\u53ef\u4ee5\u81ea\u5b9a\u4e49\u586b\u5145\u503c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u4e00\u4e00\u5bf9\u5e94\u7684\u57fa\u7840\u4e0a\u8fdb\u884c\u6570\u636e\u5904\u7406\uff1f<\/strong><br \/>\u5728\u5b9e\u73b0\u6570\u7ec4\u4e00\u4e00\u5bf9\u5e94\u540e\uff0c\u53ef\u4ee5\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u4f8b\u5982\u8ba1\u7b97\u5bf9\u5e94\u5143\u7d20\u4e4b\u548c\u3001\u62fc\u63a5\u5b57\u7b26\u4e32\u7b49\u3002\u501f\u52a9\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u53ef\u4ee5\u5feb\u901f\u751f\u6210\u65b0\u7684\u5217\u8868\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u8981\u5c06\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u5bf9\u5e94\u5143\u7d20\u76f8\u52a0\uff0c\u53ef\u4ee5\u4f7f\u7528<code>[a + b for a, b in zip(list1, list2)]<\/code>\u3002\u8fd9\u6837\u80fd\u591f\u9ad8\u6548\u5730\u5904\u7406\u6570\u636e\uff0c\u751f\u6210\u65b0\u7684\u6570\u7ec4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u6570\u7ec4\u4e00\u4e00\u5bf9\u5e94\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570zip()\u3001\u4f7f\u7528enumerate()\u51fd\u6570\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1068350,"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\/1068343"}],"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=1068343"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1068343\/revisions"}],"predecessor-version":[{"id":1068354,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1068343\/revisions\/1068354"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1068350"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1068343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1068343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1068343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}