{"id":931850,"date":"2024-12-26T17:46:05","date_gmt":"2024-12-26T09:46:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/931850.html"},"modified":"2024-12-26T17:46:07","modified_gmt":"2024-12-26T09:46:07","slug":"python-%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba%e5%90%91%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/931850.html","title":{"rendered":"python \u5982\u4f55\u8868\u793a\u5411\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070002\/042924e7-ee17-49b9-ab5d-f5d50817b782.webp\" alt=\"python \u5982\u4f55\u8868\u793a\u5411\u91cf\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u3001\u5143\u7ec4\u3001NumPy\u6570\u7ec4\u3001Pandas\u6570\u636e\u7ed3\u6784\u7b49\u591a\u79cd\u65b9\u5f0f\u6765\u8868\u793a\u5411\u91cf\u3002NumPy\u6570\u7ec4\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u5411\u91cf\u548c\u77e9\u9635\u8fd0\u7b97\u3002<\/strong>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528NumPy\u6570\u7ec4\u6765\u8868\u793a\u5411\u91cf\uff0c\u5e76\u8ba8\u8bba\u4f7f\u7528\u5217\u8868\u548c\u5143\u7ec4\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5217\u8868\u8868\u793a\u5411\u91cf<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5217\u8868\u662f\u4e00\u79cd\u57fa\u672c\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u6765\u8868\u793a\u5411\u91cf\u3002\u5217\u8868\u662f\u53ef\u53d8\u7684\uff0c\u53ef\u4ee5\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u5143\u7d20\uff0c\u4f46\u5728\u8868\u793a\u6570\u5b66\u5411\u91cf\u65f6\uff0c\u901a\u5e38\u8981\u6c42\u5143\u7d20\u4e3a\u540c\u4e00\u6570\u636e\u7c7b\u578b\uff08\u5982\u6574\u6570\u6216\u6d6e\u70b9\u6570\uff09\u3002<\/p>\n<\/p>\n<p><pre><code># \u4f7f\u7528\u5217\u8868\u8868\u793a\u4e00\u4e2a\u5411\u91cf<\/p>\n<p>vector_list = [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u5217\u8868\u8868\u793a\u5411\u91cf\u7684\u4f18\u70b9\u662f\u7b80\u5355\u76f4\u89c2\uff0c\u5e76\u4e14Python\u81ea\u5e26\u652f\u6301\uff0c\u4e0d\u9700\u8981\u989d\u5916\u5b89\u88c5\u7b2c\u4e09\u65b9\u5e93\u3002\u7136\u800c\uff0c\u5217\u8868\u5728\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u65f6\u6548\u7387\u8f83\u4f4e\uff0c\u56e0\u4e3a\u5b83\u4eec\u6ca1\u6709\u4e13\u95e8\u9488\u5bf9\u5411\u91cf\u5316\u64cd\u4f5c\u8fdb\u884c\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5143\u7ec4\u8868\u793a\u5411\u91cf<\/p>\n<\/p>\n<p><p>\u5143\u7ec4\u4e0e\u5217\u8868\u7c7b\u4f3c\uff0c\u4f46\u5b83\u662f\u4e0d\u53ef\u53d8\u7684\u3002\u8fd9\u610f\u5473\u7740\u4e00\u65e6\u521b\u5efa\u4e86\u4e00\u4e2a\u5143\u7ec4\uff0c\u5176\u5185\u5bb9\u5c31\u4e0d\u80fd\u88ab\u66f4\u6539\u3002\u56e0\u6b64\uff0c\u4f7f\u7528\u5143\u7ec4\u8868\u793a\u5411\u91cf\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u7684\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<p><pre><code># \u4f7f\u7528\u5143\u7ec4\u8868\u793a\u4e00\u4e2a\u5411\u91cf<\/p>\n<p>vector_tuple = (1, 2, 3, 4, 5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5143\u7ec4\u7684\u4e0d\u53ef\u53d8\u6027\u4f7f\u5176\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u6bd4\u5217\u8868\u66f4\u9002\u5408\uff0c\u6bd4\u5982\u5728\u9700\u8981\u4fdd\u8bc1\u6570\u636e\u4e0d\u88ab\u6539\u53d8\u7684\u573a\u666f\u3002\u4f46\u4e0e\u5217\u8868\u7c7b\u4f3c\uff0c\u5143\u7ec4\u5728\u6570\u5b66\u8fd0\u7b97\u65b9\u9762\u4e5f\u4e0d\u662f\u6700\u4f18\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528NumPy\u6570\u7ec4\u8868\u793a\u5411\u91cf<\/p>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\uff08ndarray\uff09\u4ee5\u53ca\u4e30\u5bcc\u7684\u6570\u5b66\u51fd\u6570\u5e93\uff0c\u7279\u522b\u9002\u5408\u7528\u4e8e\u6570\u503c\u8ba1\u7b97\u548c\u5411\u91cf\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code>import numpy as np<\/p>\n<h2><strong>\u4f7f\u7528NumPy\u6570\u7ec4\u8868\u793a\u4e00\u4e2a\u5411\u91cf<\/strong><\/h2>\n<p>vector_numpy = np.array([1, 2, 3, 4, 5])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>NumPy\u6570\u7ec4\u662f\u8868\u793a\u5411\u91cf\u7684\u9996\u9009\u5de5\u5177\uff0c\u56e0\u4e3a\u5b83\u4e13\u95e8\u9488\u5bf9\u5411\u91cf\u548c\u77e9\u9635\u8ba1\u7b97\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u652f\u6301\u9ad8\u6548\u7684\u77e2\u91cf\u5316\u8fd0\u7b97\u3002<\/strong>\u4f7f\u7528NumPy\u6570\u7ec4\u7684\u53e6\u4e00\u4e2a\u4f18\u70b9\u662f\u5176\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ebf\u6027\u4ee3\u6570\u51fd\u6570\u548c\u5176\u4ed6\u79d1\u5b66\u8ba1\u7b97\u529f\u80fd\uff0c\u4f7f\u5f97\u64cd\u4f5c\u5411\u91cf\u66f4\u4e3a\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4f7f\u7528NumPy\u53ef\u4ee5\u8f7b\u677e\u8fdb\u884c\u5411\u91cf\u7684\u52a0\u51cf\u8fd0\u7b97\u3001\u70b9\u79ef\u8ba1\u7b97\u3001\u6c42\u6a21\u7b49\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code># \u5411\u91cf\u52a0\u6cd5<\/p>\n<p>vector_a = np.array([1, 2, 3])<\/p>\n<p>vector_b = np.array([4, 5, 6])<\/p>\n<p>vector_sum = vector_a + vector_b<\/p>\n<h2><strong>\u5411\u91cf\u70b9\u79ef<\/strong><\/h2>\n<p>dot_product = np.dot(vector_a, vector_b)<\/p>\n<h2><strong>\u5411\u91cf\u6a21<\/strong><\/h2>\n<p>vector_norm = np.linalg.norm(vector_a)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528Pandas\u6570\u636e\u7ed3\u6784\u8868\u793a\u5411\u91cf<\/p>\n<\/p>\n<p><p>Pandas\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u901a\u5e38\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u6570\u636e\u64cd\u4f5c\u3002Pandas\u4e2d\u7684Series\u53ef\u4ee5\u7528\u6765\u8868\u793a\u4e00\u7ef4\u5411\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code>import pandas as pd<\/p>\n<h2><strong>\u4f7f\u7528Pandas Series\u8868\u793a\u4e00\u4e2a\u5411\u91cf<\/strong><\/h2>\n<p>vector_series = pd.Series([1, 2, 3, 4, 5])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u867d\u7136Pandas\u7684Series\u5728\u6027\u80fd\u4e0a\u4e0d\u5982NumPy\u6570\u7ec4\uff0c\u4f46\u5b83\u4eec\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u6570\u636e\u64cd\u4f5c\u529f\u80fd\uff0c\u9002\u5408\u7528\u4e8e\u9700\u8981\u5bf9\u5411\u91cf\u8fdb\u884c\u590d\u6742\u6570\u636e\u5904\u7406\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u9009\u62e9\u5408\u9002\u7684\u5411\u91cf\u8868\u793a\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u6765\u8868\u793a\u5411\u91cf\uff0c\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u7b80\u5355\u64cd\u4f5c\u548c\u5c0f\u89c4\u6a21\u8ba1\u7b97<\/strong>\uff1a\u5982\u679c\u53ea\u662f\u8fdb\u884c\u4e00\u4e9b\u7b80\u5355\u7684\u5411\u91cf\u64cd\u4f5c\uff0c\u5982\u904d\u5386\u6216\u8bbf\u95ee\u5143\u7d20\uff0c\u5217\u8868\u548c\u5143\u7ec4\u662f\u8db3\u591f\u7684\u3002<\/li>\n<li><strong>\u6570\u503c\u8ba1\u7b97\u548c\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406<\/strong>\uff1a\u5982\u679c\u6d89\u53ca\u5927\u91cf\u7684\u6570\u503c\u8ba1\u7b97\uff0cNumPy\u6570\u7ec4\u662f\u9996\u9009\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u77e2\u91cf\u5316\u8fd0\u7b97\u3002<\/li>\n<li><strong>\u6570\u636e\u5206\u6790\u548c\u5904\u7406<\/strong>\uff1a\u5982\u679c\u9700\u8981\u5bf9\u5411\u91cf\u8fdb\u884c\u590d\u6742\u7684\u6570\u636e\u64cd\u4f5c\uff08\u5982\u7d22\u5f15\u3001\u8fc7\u6ee4\u3001\u5206\u7ec4\u7b49\uff09\uff0cPandas\u7684Series\u66f4\u4e3a\u5408\u9002\u3002<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5411\u91cf\u8868\u793a\u6709\u591a\u79cd\u65b9\u5f0f\uff0c\u6bcf\u79cd\u65b9\u5f0f\u90fd\u6709\u5176\u7279\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u6570\u503c\u8ba1\u7b97\u4efb\u52a1\uff0cNumPy\u6570\u7ec4\u662f\u6700\u4f73\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u8ba1\u7b97\u6027\u80fd\u548c\u4e30\u5bcc\u7684\u6570\u5b66\u51fd\u6570\u3002\u5bf9\u4e8e\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4efb\u52a1\uff0cPandas\u7684Series\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u64cd\u4f5c\u80fd\u529b\u3002\u800c\u5bf9\u4e8e\u7b80\u5355\u7684\u5b58\u50a8\u548c\u8bbf\u95ee\u64cd\u4f5c\uff0c\u5217\u8868\u548c\u5143\u7ec4\u662f\u6700\u76f4\u89c2\u7684\u9009\u62e9\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u7406\u89e3\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\u662f\u5173\u952e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u4e2d\u5411\u91cf\u7684\u8868\u793a\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5411\u91cf\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8868\u793a\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5217\u8868\u6216\u5143\u7ec4\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u7528\u4e00\u4e2a\u5217\u8868 <code>[1, 2, 3]<\/code> \u6765\u8868\u793a\u4e00\u4e2a\u4e09\u7ef4\u5411\u91cf\u3002\u53e6\u4e00\u79cd\u6d41\u884c\u7684\u65b9\u6cd5\u662f\u4f7f\u7528NumPy\u5e93\uff0c\u4f7f\u7528<code>numpy.array()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u6570\u7ec4\uff0c\u8fd9\u79cd\u65b9\u5f0f\u5728\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u65f6\u66f4\u52a0\u9ad8\u6548\u3002\u6b64\u5916\uff0c\u4f7f\u7528Pandas\u5e93\u7684Series\u5bf9\u8c61\u4e5f\u53ef\u4ee5\u8868\u793a\u5411\u91cf\uff0c\u7279\u522b\u662f\u5728\u6570\u636e\u5206\u6790\u4e2d\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u5411\u91cf\u8fd0\u7b97\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5411\u91cf\u8fd0\u7b97\u901a\u5e38\u4f7f\u7528NumPy\u5e93\u6765\u5b9e\u73b0\u3002NumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u5b66\u8fd0\u7b97\u529f\u80fd\uff0c\u4f8b\u5982\u70b9\u79ef\u3001\u5411\u91cf\u52a0\u6cd5\u548c\u6807\u91cf\u4e58\u6cd5\u3002\u901a\u8fc7\u7b80\u5355\u7684\u8fd0\u7b97\u7b26\uff08\u5982<code>+<\/code>\u3001<code>-<\/code>\u3001<code>*<\/code>\uff09\u6216\u4f7f\u7528NumPy\u63d0\u4f9b\u7684\u51fd\u6570\uff08\u5982<code>np.dot()<\/code>\uff09\u53ef\u4ee5\u8f7b\u677e\u8fdb\u884c\u8fd9\u4e9b\u8fd0\u7b97\u3002\u5229\u7528NumPy\uff0c\u7528\u6237\u53ef\u4ee5\u5904\u7406\u9ad8\u7ef4\u5411\u91cf\uff0c\u5e76\u80fd\u591f\u5229\u7528\u5176\u5e7f\u64ad\u673a\u5236\u8fdb\u884c\u9ad8\u6548\u8ba1\u7b97\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u8868\u793a\u5411\u91cf\u65f6\u6709\u54ea\u4e9b\u6027\u80fd\u8003\u8651\uff1f<\/strong><br \/>\u5728\u8868\u793a\u5411\u91cf\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u5bf9\u6027\u80fd\u6709\u76f4\u63a5\u5f71\u54cd\u3002NumPy\u6570\u7ec4\u5728\u5b58\u50a8\u548c\u8fd0\u7b97\u4e0a\u6bd4\u666e\u901aPython\u5217\u8868\u66f4\u4e3a\u9ad8\u6548\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u3002\u7531\u4e8eNumPy\u6570\u7ec4\u662f\u540c\u8d28\u7684\uff0c\u5185\u5b58\u4f7f\u7528\u6548\u7387\u66f4\u9ad8\uff0c\u8ba1\u7b97\u901f\u5ea6\u4e5f\u66f4\u5feb\u3002\u6b64\u5916\uff0c\u4f7f\u7528NumPy\u65f6\uff0c\u5229\u7528\u5176\u5185\u7f6e\u7684\u5411\u91cf\u5316\u64cd\u4f5c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8fd0\u7b97\u6548\u7387\uff0c\u907f\u514d\u4f7f\u7528\u663e\u5f0f\u7684\u5faa\u73af\uff0c\u4ece\u800c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u4e0e\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u3001\u5143\u7ec4\u3001NumPy\u6570\u7ec4\u3001Pandas\u6570\u636e\u7ed3\u6784\u7b49\u591a\u79cd\u65b9\u5f0f\u6765\u8868\u793a\u5411\u91cf\u3002NumPy\u6570\u7ec4 [&hellip;]","protected":false},"author":3,"featured_media":931854,"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\/931850"}],"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=931850"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/931850\/revisions"}],"predecessor-version":[{"id":931855,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/931850\/revisions\/931855"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/931854"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=931850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=931850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=931850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}