{"id":1095560,"date":"2025-01-08T14:52:19","date_gmt":"2025-01-08T06:52:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1095560.html"},"modified":"2025-01-08T14:52:21","modified_gmt":"2025-01-08T06:52:21","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8a%e5%88%97%e8%a1%a8%e4%b8%ad%e5%a4%9a%e7%bb%b4%e6%95%b0%e6%8d%ae%e5%88%86%e7%a6%bb-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1095560.html","title":{"rendered":"python\u5982\u4f55\u628a\u5217\u8868\u4e2d\u591a\u7ef4\u6570\u636e\u5206\u79bb"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211144\/3ed4f38f-1baf-499f-a4e9-d2670e4aa573.webp\" alt=\"python\u5982\u4f55\u628a\u5217\u8868\u4e2d\u591a\u7ef4\u6570\u636e\u5206\u79bb\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u628a\u5217\u8868\u4e2d\u591a\u7ef4\u6570\u636e\u5206\u79bb\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u6bd4\u5982\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u4f7f\u7528numpy\u5e93\u3001\u4f7f\u7528itertools\u5e93\u3002<\/strong> <\/p>\n<\/p>\n<p><p>\u5728Python\u7f16\u7a0b\u4e2d\uff0c\u5904\u7406\u591a\u7ef4\u6570\u636e\u662f\u5e38\u89c1\u7684\u4efb\u52a1\u4e4b\u4e00\u3002\u5982\u679c\u6211\u4eec\u62e5\u6709\u4e00\u4e2a\u5305\u542b\u591a\u7ef4\u6570\u636e\u7684\u5217\u8868\uff0c\u5e76\u4e14\u5e0c\u671b\u5c06\u5176\u5206\u79bb\u6210\u5355\u72ec\u7684\u7ef4\u5ea6\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528<code>numpy<\/code>\u5e93\u6765\u5206\u79bb\u591a\u7ef4\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528numpy\u5e93\u6765\u5206\u79bb\u591a\u7ef4\u6570\u636e\uff1a<\/strong> <code>numpy<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u4e13\u95e8\u7528\u4e8e\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u3002\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u591a\u7ef4\u6570\u636e\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528<code>numpy<\/code>\u5e93\u6765\u5c06\u591a\u7ef4\u6570\u636e\u5206\u79bb\u6210\u5355\u72ec\u7684\u7ef4\u5ea6\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165numpy\u5e93<\/h2>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86numpy\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728Python\u4ee3\u7801\u4e2d\u5bfc\u5165numpy\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u521b\u5efa\u591a\u7ef4\u6570\u636e\u5217\u8868<\/h2>\n<\/p>\n<p><p>\u4e3a\u4e86\u6f14\u793a\u5982\u4f55\u5206\u79bb\u591a\u7ef4\u6570\u636e\uff0c\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u591a\u7ef4\u6570\u636e\u7684\u5217\u8868\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u4e09\u7ef4\u6570\u636e\u7684\u5217\u8868\uff0c\u6bcf\u4e2a\u5143\u7d20\u4ee3\u8868\u4e00\u4e2a\u70b9\u7684\u5750\u6807\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9],<\/p>\n<p>    [10, 11, 12]<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u5750\u6807\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528numpy\u5206\u79bb\u591a\u7ef4\u6570\u636e<\/h2>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u4f7f\u7528<code>numpy<\/code>\u5e93\u5c06\u591a\u7ef4\u6570\u636e\u5206\u79bb\u6210\u5355\u72ec\u7684\u7ef4\u5ea6\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>numpy.array<\/code>\u51fd\u6570\u5c06\u5217\u8868\u8f6c\u6362\u4e3a<code>numpy<\/code>\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528\u6570\u7ec4\u7684\u5207\u7247\u64cd\u4f5c\u6765\u5206\u79bb\u7ef4\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u5217\u8868\u8f6c\u6362\u4e3anumpy\u6570\u7ec4<\/p>\n<p>np_data = np.array(data)<\/p>\n<h2><strong>\u5206\u79bb\u7ef4\u5ea6<\/strong><\/h2>\n<p>x = np_data[:, 0]<\/p>\n<p>y = np_data[:, 1]<\/p>\n<p>z = np_data[:, 2]<\/p>\n<p>print(&quot;X:&quot;, x)<\/p>\n<p>print(&quot;Y:&quot;, y)<\/p>\n<p>print(&quot;Z:&quot;, z)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u5217\u8868\u8f6c\u6362\u4e3a<code>numpy<\/code>\u6570\u7ec4\u3002\u7136\u540e\uff0c\u901a\u8fc7\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\uff0c\u6211\u4eec\u5206\u79bb\u4e86\u6bcf\u4e2a\u7ef4\u5ea6\u7684\u6570\u636e\u3002<code>np_data[:, 0]<\/code>\u8868\u793a\u9009\u62e9\u6240\u6709\u884c\u7684\u7b2c\u4e00\u5217\u6570\u636e\uff0c\u4f9d\u6b64\u7c7b\u63a8\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u5206\u79bb\u591a\u7ef4\u6570\u636e<\/h2>\n<\/p>\n<p><p>\u9664\u4e86<code>numpy<\/code>\u5e93\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5217\u8868\u89e3\u6790\u6765\u5206\u79bb\u591a\u7ef4\u6570\u636e\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u9700\u8981\u989d\u5916\u7684\u5e93\uff0c\u975e\u5e38\u9002\u5408\u5904\u7406\u7b80\u5355\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5206\u79bb\u7ef4\u5ea6<\/p>\n<p>x = [point[0] for point in data]<\/p>\n<p>y = [point[1] for point in data]<\/p>\n<p>z = [point[2] for point in data]<\/p>\n<p>print(&quot;X:&quot;, x)<\/p>\n<p>print(&quot;Y:&quot;, y)<\/p>\n<p>print(&quot;Z:&quot;, z)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u89e3\u6790\u4ece\u6bcf\u4e2a\u70b9\u4e2d\u63d0\u53d6\u76f8\u5e94\u7684\u5750\u6807\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528itertools\u5e93\u5206\u79bb\u591a\u7ef4\u6570\u636e<\/h2>\n<\/p>\n<p><p><code>itertools<\/code>\u5e93\u662fPython\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u8fed\u4ee3\u5668\u5de5\u5177\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>itertools<\/code>\u5e93\u4e2d\u7684<code>zip<\/code>\u51fd\u6570\u6765\u5206\u79bb\u591a\u7ef4\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<h2><strong>\u5206\u79bb\u7ef4\u5ea6<\/strong><\/h2>\n<p>x, y, z = zip(*data)<\/p>\n<p>print(&quot;X:&quot;, x)<\/p>\n<p>print(&quot;Y:&quot;, y)<\/p>\n<p>print(&quot;Z:&quot;, z)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>zip<\/code>\u51fd\u6570\u5c06\u6570\u636e\u8fdb\u884c\u8f6c\u7f6e\uff0c\u4ece\u800c\u5c06\u6bcf\u4e2a\u7ef4\u5ea6\u7684\u6570\u636e\u5206\u79bb\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u6027\u80fd\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>timeit<\/code>\u6a21\u5757\u6765\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<h2><strong>\u4f7f\u7528numpy\u5e93<\/strong><\/h2>\n<p>def numpy_method():<\/p>\n<p>    np_data = np.array(data)<\/p>\n<p>    x = np_data[:, 0]<\/p>\n<p>    y = np_data[:, 1]<\/p>\n<p>    z = np_data[:, 2]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/strong><\/h2>\n<p>def list_comprehension_method():<\/p>\n<p>    x = [point[0] for point in data]<\/p>\n<p>    y = [point[1] for point in data]<\/p>\n<p>    z = [point[2] for point in data]<\/p>\n<h2><strong>\u4f7f\u7528itertools\u5e93<\/strong><\/h2>\n<p>def itertools_method():<\/p>\n<p>    x, y, z = zip(*data)<\/p>\n<h2><strong>\u6d4b\u8bd5\u6027\u80fd<\/strong><\/h2>\n<p>print(&quot;Numpy method:&quot;, timeit.timeit(numpy_method, number=10000))<\/p>\n<p>print(&quot;List comprehension method:&quot;, timeit.timeit(list_comprehension_method, number=10000))<\/p>\n<p>print(&quot;Itertools method:&quot;, timeit.timeit(itertools_method, number=10000))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e09\u4e2a\u51fd\u6570\uff0c\u6bcf\u4e2a\u51fd\u6570\u4f7f\u7528\u4e0d\u540c\u7684\u65b9\u6cd5\u5206\u79bb\u591a\u7ef4\u6570\u636e\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>timeit.timeit<\/code>\u51fd\u6570\u6d4b\u8bd5\u6bcf\u4e2a\u65b9\u6cd5\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5c06Python\u5217\u8868\u4e2d\u7684\u591a\u7ef4\u6570\u636e\u5206\u79bb\u3002\u6211\u4eec\u9996\u5148\u4ecb\u7ecd\u4e86<code>numpy<\/code>\u5e93\uff0c\u7136\u540e\u4ecb\u7ecd\u4e86\u4f7f\u7528\u5217\u8868\u89e3\u6790\u548c<code>itertools<\/code>\u5e93\u7684\u65b9\u6cd5\u3002\u6700\u540e\uff0c\u6211\u4eec\u8fd8\u6bd4\u8f83\u4e86\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528numpy\u5e93\u5206\u79bb\u591a\u7ef4\u6570\u636e\u975e\u5e38\u65b9\u4fbf\uff0c\u9002\u5408\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u3002<\/strong> \u5217\u8868\u89e3\u6790\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u5408\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u636e\u3002<code>itertools<\/code>\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u8fed\u4ee3\u5668\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u5206\u79bb\u591a\u7ef4\u6570\u636e\u3002\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u6570\u636e\u89c4\u6a21\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u6570\u636e\u5206\u79bb\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5982\u679c\u4f60\u6709\u4efb\u4f55\u95ee\u9898\u6216\u5efa\u8bae\uff0c\u8bf7\u968f\u65f6\u7559\u8a00\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u591a\u7ef4\u5217\u8868\u6570\u636e\uff1f<\/strong><br \/>\u5904\u7406\u591a\u7ef4\u5217\u8868\u6570\u636e\u7684\u65b9\u5f0f\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u89e3\u6790\u3001\u5faa\u73af\u548cNumpy\u7b49\u5de5\u5177\u6765\u5b9e\u73b0\u3002\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u8bbf\u95ee\u548c\u64cd\u4f5c\u5217\u8868\u4e2d\u7684\u5d4c\u5957\u6570\u636e\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5217\u8868\u89e3\u6790\u53ef\u4ee5\u8f7b\u677e\u63d0\u53d6\u7279\u5b9a\u7ef4\u5ea6\u7684\u6570\u636e\uff0c\u6216\u8005\u901a\u8fc7Numpy\u6570\u7ec4\u6765\u7b80\u5316\u590d\u6742\u7684\u6570\u636e\u64cd\u4f5c\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5206\u79bb\u591a\u7ef4\u5217\u8868\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u9664\u4e86\u5185\u7f6e\u7684\u5217\u8868\u64cd\u4f5c\u5916\uff0c\u8fd8\u6709\u8bb8\u591a\u5e93\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5904\u7406\u591a\u7ef4\u6570\u636e\u3002Numpy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5e93\uff0c\u4e13\u95e8\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\uff0c\u63d0\u4f9b\u4e86\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u548c\u591a\u79cd\u64cd\u4f5c\u6570\u7ec4\u7684\u51fd\u6570\u3002Pandas\u5219\u66f4\u9002\u5408\u5904\u7406\u8868\u683c\u6570\u636e\uff0c\u80fd\u591f\u8f7b\u677e\u5206\u79bb\u548c\u64cd\u4f5c\u591a\u7ef4\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u591a\u7ef4\u5217\u8868\u8f6c\u6362\u4e3a\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u4ee5\u4fbf\u4e8e\u5904\u7406\uff1f<\/strong><br \/>\u591a\u7ef4\u5217\u8868\u53ef\u4ee5\u8f6c\u6362\u4e3a\u5404\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982NumPy\u6570\u7ec4\u6216Pandas DataFrame\u3002\u8fd9\u4e9b\u7ed3\u6784\u53ef\u4ee5\u63d0\u4f9b\u66f4\u5f3a\u5927\u7684\u6570\u636e\u64cd\u4f5c\u529f\u80fd\uff0c\u4f8b\u5982\u7b5b\u9009\u3001\u805a\u5408\u548c\u6570\u636e\u5206\u6790\u7b49\u3002\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u60a8\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u5904\u7406\u548c\u5206\u6790\u591a\u7ef4\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u628a\u5217\u8868\u4e2d\u591a\u7ef4\u6570\u636e\u5206\u79bb\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u6bd4\u5982\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u4f7f\u7528numpy\u5e93\u3001\u4f7f\u7528itertoo [&hellip;]","protected":false},"author":3,"featured_media":1095562,"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\/1095560"}],"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=1095560"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095560\/revisions"}],"predecessor-version":[{"id":1095563,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095560\/revisions\/1095563"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1095562"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1095560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1095560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1095560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}