{"id":1162736,"date":"2025-01-15T14:47:00","date_gmt":"2025-01-15T06:47:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1162736.html"},"modified":"2025-01-15T14:47:04","modified_gmt":"2025-01-15T06:47:04","slug":"python%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86%e6%95%b0%e7%bb%84%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1162736.html","title":{"rendered":"python\u5982\u4f55\u5904\u7406\u6570\u7ec4\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25203654\/f1b25aa8-d5d0-4698-b6b1-a9a76eb18cbe.webp\" alt=\"python\u5982\u4f55\u5904\u7406\u6570\u7ec4\u6570\u636e\" \/><\/p>\n<p><p> <strong>Python\u5904\u7406\u6570\u7ec4\u6570\u636e\u7684\u6838\u5fc3\u5de5\u5177\u5305\u62ecnumpy\u3001\u5217\u8868\u89e3\u6790\u3001Pandas\u7b49\u3002<\/strong> \u5176\u4e2d\uff0cNumpy\u662f\u5904\u7406\u6570\u7ec4\u6570\u636e\u7684\u6807\u51c6\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5305\u62ec\u6570\u7ec4\u521b\u5efa\u3001\u5207\u7247\u3001\u7d22\u5f15\u3001\u6570\u5b66\u8fd0\u7b97\u7b49\u3002\u5217\u8868\u89e3\u6790\u4f7f\u5f97\u5bf9\u5217\u8868\u8fdb\u884c\u5feb\u901f\u5904\u7406\u6210\u4e3a\u53ef\u80fd\uff0c\u800cPandas\u5219\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u4efb\u52a1\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u6765\u5904\u7406\u6570\u7ec4\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001Numpy\u5e93\u7684\u4f7f\u7528<\/strong><\/h2>\n<p><h2>1\u3001Numpy\u6570\u7ec4\u7684\u521b\u5efa<\/h2>\n<\/p>\n<p><p>Numpy\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u521b\u5efa\u6570\u7ec4\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u76f4\u63a5\u4ece\u5217\u8868\u521b\u5efa\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u5982<code>arange<\/code>\u3001<code>linspace<\/code>\u3001<code>zeros<\/code>\u3001<code>ones<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4ece\u5217\u8868\u521b\u5efa\u6570\u7ec4<\/strong><\/h2>\n<p>arr1 = np.array([1, 2, 3, 4, 5])<\/p>\n<p>print(arr1)<\/p>\n<h2><strong>\u4f7f\u7528arange\u521b\u5efa\u6570\u7ec4<\/strong><\/h2>\n<p>arr2 = np.arange(10)<\/p>\n<p>print(arr2)<\/p>\n<h2><strong>\u4f7f\u7528linspace\u521b\u5efa\u6570\u7ec4<\/strong><\/h2>\n<p>arr3 = np.linspace(0, 1, 10)<\/p>\n<p>print(arr3)<\/p>\n<h2><strong>\u521b\u5efa\u5168\u96f6\u6570\u7ec4<\/strong><\/h2>\n<p>arr4 = np.zeros((3, 3))<\/p>\n<p>print(arr4)<\/p>\n<h2><strong>\u521b\u5efa\u5168\u4e00\u6570\u7ec4<\/strong><\/h2>\n<p>arr5 = np.ones((2, 2))<\/p>\n<p>print(arr5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u6570\u7ec4\u7684\u57fa\u672c\u64cd\u4f5c<\/h2>\n<\/p>\n<p><p>Numpy\u6570\u7ec4\u652f\u6301\u591a\u79cd\u57fa\u672c\u64cd\u4f5c\uff0c\u5305\u62ec\u6570\u7ec4\u5207\u7247\u3001\u6570\u7ec4\u7d22\u5f15\u3001\u6570\u7ec4\u5f62\u72b6\u53d8\u6362\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6570\u7ec4\u5207\u7247<\/p>\n<p>arr = np.array([1, 2, 3, 4, 5])<\/p>\n<p>print(arr[1:4])<\/p>\n<h2><strong>\u6570\u7ec4\u7d22\u5f15<\/strong><\/h2>\n<p>print(arr[2])<\/p>\n<h2><strong>\u6570\u7ec4\u5f62\u72b6\u53d8\u6362<\/strong><\/h2>\n<p>arr = np.arange(12).reshape((3, 4))<\/p>\n<p>print(arr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u6570\u7ec4\u7684\u6570\u5b66\u8fd0\u7b97<\/h2>\n<\/p>\n<p><p>Numpy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u5b66\u8fd0\u7b97\u51fd\u6570\uff0c\u53ef\u4ee5\u5bf9\u6570\u7ec4\u8fdb\u884c\u52a0\u51cf\u4e58\u9664\u3001\u77e9\u9635\u4e58\u6cd5\u3001\u7edf\u8ba1\u8fd0\u7b97\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6570\u7ec4\u52a0\u6cd5<\/p>\n<p>arr1 = np.array([1, 2, 3])<\/p>\n<p>arr2 = np.array([4, 5, 6])<\/p>\n<p>print(arr1 + arr2)<\/p>\n<h2><strong>\u6570\u7ec4\u4e58\u6cd5<\/strong><\/h2>\n<p>print(arr1 * arr2)<\/p>\n<h2><strong>\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>arr1 = np.array([[1, 2], [3, 4]])<\/p>\n<p>arr2 = np.array([[5, 6], [7, 8]])<\/p>\n<p>print(np.dot(arr1, arr2))<\/p>\n<h2><strong>\u7edf\u8ba1\u8fd0\u7b97<\/strong><\/h2>\n<p>arr = np.array([1, 2, 3, 4, 5])<\/p>\n<p>print(np.mean(arr))<\/p>\n<p>print(np.sum(arr))<\/p>\n<p>print(np.std(arr))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u5217\u8868\u89e3\u6790<\/strong><\/h2>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u521b\u5efa\u548c\u64cd\u4f5c\u5217\u8868\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u5feb\u901f\u751f\u6210\u5217\u8868\u3001\u8fdb\u884c\u6761\u4ef6\u7b5b\u9009\u7b49\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u751f\u6210\u5217\u8868<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u4e00\u4e2a\u5305\u542b0\u52309\u7684\u5217\u8868<\/p>\n<p>lst = [x for x in range(10)]<\/p>\n<p>print(lst)<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u5305\u542b0\u52309\u5e73\u65b9\u503c\u7684\u5217\u8868<\/strong><\/h2>\n<p>lst = [x2 for x in range(10)]<\/p>\n<p>print(lst)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u6761\u4ef6\u7b5b\u9009<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7b5b\u9009\u51fa\u5217\u8868\u4e2d\u6240\u6709\u7684\u5076\u6570<\/p>\n<p>lst = [x for x in range(10) if x % 2 == 0]<\/p>\n<p>print(lst)<\/p>\n<h2><strong>\u7b5b\u9009\u51fa\u5217\u8868\u4e2d\u6240\u6709\u5927\u4e8e5\u7684\u6570<\/strong><\/h2>\n<p>lst = [x for x in range(10) if x &gt; 5]<\/p>\n<p>print(lst)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001Pandas\u5e93\u7684\u4f7f\u7528<\/strong><\/h2>\n<p><p>Pandas\u5e93\u662f\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u7279\u522b\u9002\u7528\u4e8e\u64cd\u4f5c\u7ed3\u6784\u5316\u6570\u636e\uff0c\u5982\u8868\u683c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h2>1\u3001DataFrame\u7684\u521b\u5efa<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u4ece\u5b57\u5178\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>data = {&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6], &#39;C&#39;: [7, 8, 9]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(df)<\/p>\n<h2><strong>\u4eceNumpy\u6570\u7ec4\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p>df = pd.DataFrame(arr, columns=[&#39;A&#39;, &#39;B&#39;, &#39;C&#39;])<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001DataFrame\u7684\u57fa\u672c\u64cd\u4f5c<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9009\u62e9\u5217<\/p>\n<p>print(df[&#39;A&#39;])<\/p>\n<h2><strong>\u9009\u62e9\u884c<\/strong><\/h2>\n<p>print(df.iloc[0])<\/p>\n<h2><strong>\u6761\u4ef6\u7b5b\u9009<\/strong><\/h2>\n<p>print(df[df[&#39;A&#39;] &gt; 1])<\/p>\n<h2><strong>\u6dfb\u52a0\u65b0\u5217<\/strong><\/h2>\n<p>df[&#39;D&#39;] = df[&#39;A&#39;] + df[&#39;B&#39;]<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u6570\u636e\u7684\u7edf\u8ba1\u5206\u6790<\/h2>\n<\/p>\n<p><p>Pandas\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u7edf\u8ba1\u5206\u6790\u529f\u80fd\uff0c\u5305\u62ec\u63cf\u8ff0\u6027\u7edf\u8ba1\u3001\u5206\u7ec4\u805a\u5408\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u63cf\u8ff0\u6027\u7edf\u8ba1<\/p>\n<p>print(df.describe())<\/p>\n<h2><strong>\u5206\u7ec4\u805a\u5408<\/strong><\/h2>\n<p>data = {&#39;A&#39;: [&#39;foo&#39;, &#39;bar&#39;, &#39;foo&#39;, &#39;bar&#39;], &#39;B&#39;: [1, 2, 3, 4], &#39;C&#39;: [5, 6, 7, 8]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(df.groupby(&#39;A&#39;).sum())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001Matplotlib\u5e93\u7684\u4f7f\u7528<\/strong><\/h2>\n<p><p>Matplotlib\u5e93\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u5404\u79cd\u9759\u6001\u3001\u52a8\u6001\u548c\u4ea4\u4e92\u5f0f\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u7ed8\u5236\u57fa\u672c\u56fe\u8868<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7ed8\u5236\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u7ed8\u5236\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>x = np.random.rand(100)<\/p>\n<p>y = np.random.rand(100)<\/p>\n<p>plt.scatter(x, y)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u56fe\u8868\u7684\u7f8e\u5316<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.title(&#39;Sine Wave&#39;)<\/p>\n<p>plt.xlabel(&#39;x&#39;)<\/p>\n<p>plt.ylabel(&#39;sin(x)&#39;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u6dfb\u52a0\u56fe\u4f8b<\/strong><\/h2>\n<p>plt.plot(x, y, label=&#39;sin(x)&#39;)<\/p>\n<p>plt.plot(x, np.cos(x), label=&#39;cos(x)&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u5b50\u56fe\u7684\u7ed8\u5236<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u5b50\u56fe<\/p>\n<p>fig, axs = plt.subplots(2, 1)<\/p>\n<p>axs[0].plot(x, y)<\/p>\n<p>axs[1].plot(x, np.cos(x))<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u521b\u5efa\u5e26\u6709\u5171\u4eabx\u8f74\u7684\u5b50\u56fe<\/strong><\/h2>\n<p>fig, axs = plt.subplots(2, 1, sharex=True)<\/p>\n<p>axs[0].plot(x, y)<\/p>\n<p>axs[1].plot(x, np.cos(x))<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001Scipy\u5e93\u7684\u4f7f\u7528<\/strong><\/h2>\n<p><p>Scipy\u5e93\u662f\u57fa\u4e8eNumpy\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u591a\u9ad8\u7ea7\u7684\u6570\u5b66\u3001\u79d1\u5b66\u548c\u5de5\u7a0b\u8ba1\u7b97\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u7ebf\u6027\u4ee3\u6570<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.linalg import inv, eig<\/p>\n<h2><strong>\u8ba1\u7b97\u77e9\u9635\u7684\u9006<\/strong><\/h2>\n<p>arr = np.array([[1, 2], [3, 4]])<\/p>\n<p>print(inv(arr))<\/p>\n<h2><strong>\u8ba1\u7b97\u77e9\u9635\u7684\u7279\u5f81\u503c\u548c\u7279\u5f81\u5411\u91cf<\/strong><\/h2>\n<p>vals, vecs = eig(arr)<\/p>\n<p>print(vals)<\/p>\n<p>print(vecs)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u4f18\u5316<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.optimize import minimize<\/p>\n<h2><strong>\u5b9a\u4e49\u76ee\u6807\u51fd\u6570<\/strong><\/h2>\n<p>def func(x):<\/p>\n<p>    return x2 + 2*x + 1<\/p>\n<h2><strong>\u4f7f\u7528minimize\u51fd\u6570\u8fdb\u884c\u4f18\u5316<\/strong><\/h2>\n<p>result = minimize(func, 0)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u4fe1\u53f7\u5904\u7406<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.signal import find_peaks<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u4fe1\u53f7<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x) + 0.5 * np.random.randn(100)<\/p>\n<h2><strong>\u5bfb\u627e\u4fe1\u53f7\u7684\u5cf0\u503c<\/strong><\/h2>\n<p>peaks, _ = find_peaks(y)<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.plot(x[peaks], y[peaks], &#39;x&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u6765\u5904\u7406\u6570\u7ec4\u6570\u636e\u3002\u65e0\u8bba\u662f\u57fa\u672c\u7684\u6570\u7ec4\u64cd\u4f5c\u3001\u9ad8\u6548\u7684\u6570\u5b66\u8fd0\u7b97\u3001\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u8fd8\u662f\u9ad8\u7ea7\u7684\u79d1\u5b66\u8ba1\u7b97\uff0cPython\u90fd\u80fd\u63d0\u4f9b\u5f3a\u6709\u529b\u7684\u652f\u6301\u3002\u638c\u63e1\u8fd9\u4e9b\u5de5\u5177\u548c\u65b9\u6cd5\uff0c\u5c06\u6781\u5927\u5730\u63d0\u9ad8\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u548c\u521d\u59cb\u5316\u6570\u7ec4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\uff08list\uff09\u6765\u521b\u5efa\u548c\u521d\u59cb\u5316\u6570\u7ec4\u3002\u5217\u8868\u662f\u4e00\u79cd\u5185\u7f6e\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5141\u8bb8\u5b58\u50a8\u591a\u4e2a\u5143\u7d20\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u521b\u5efa\u4e00\u4e2a\u5217\u8868\uff1a  <\/p>\n<pre><code class=\"language-python\">my_array = [1, 2, 3, 4, 5]\n<\/code><\/pre>\n<p>\u6b64\u5916\uff0c\u4f7f\u7528NumPy\u5e93\u80fd\u591f\u66f4\u9ad8\u6548\u5730\u5904\u7406\u6570\u7ec4\u6570\u636e\uff0c\u7279\u522b\u662f\u5f53\u9700\u8981\u8fdb\u884c\u5927\u91cf\u6570\u5b66\u8fd0\u7b97\u65f6\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\nmy_array = np.array([1, 2, 3, 4, 5])\n<\/code><\/pre>\n<p>NumPy\u63d0\u4f9b\u4e86\u66f4\u591a\u529f\u80fd\u548c\u66f4\u597d\u7684\u6027\u80fd\uff0c\u9002\u5408\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u5bf9\u6570\u7ec4\u8fdb\u884c\u64cd\u4f5c\u548c\u5904\u7406\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6570\u7ec4\u7684\u64cd\u4f5c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\u3002\u4f7f\u7528\u5217\u8868\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u3001\u6dfb\u52a0\u3001\u5220\u9664\u548c\u4fee\u6539\u7b49\u64cd\u4f5c\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_array.append(6)  # \u6dfb\u52a0\u5143\u7d20\nmy_array.remove(2)  # \u5220\u9664\u5143\u7d20\nmy_array[0] = 10    # \u4fee\u6539\u5143\u7d20\n<\/code><\/pre>\n<p>\u5982\u679c\u4f7f\u7528NumPy\u6570\u7ec4\uff0c\u53ef\u4ee5\u5229\u7528NumPy\u63d0\u4f9b\u7684\u5404\u79cd\u51fd\u6570\u8fdb\u884c\u66f4\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u5982\u6570\u5b66\u8fd0\u7b97\u3001\u7edf\u8ba1\u5206\u6790\u7b49\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\nmy_array = np.array([1, 2, 3, 4, 5])\nmean_value = np.mean(my_array)  # \u8ba1\u7b97\u5e73\u5747\u503c\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u6570\u7ec4\u7684\u6392\u5e8f\u548c\u67e5\u627e\u529f\u80fd\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6570\u7ec4\u7684\u6392\u5e8f\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u7684\u5185\u7f6e\u65b9\u6cd5\u6216NumPy\u5e93\u6765\u5b9e\u73b0\u3002\u5bf9\u4e8e\u5217\u8868\uff0c\u4f7f\u7528<code>sort()<\/code>\u65b9\u6cd5\u6216<code>sorted()<\/code>\u51fd\u6570\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_array = [5, 3, 1, 4, 2]\nmy_array.sort()  # \u539f\u5730\u6392\u5e8f\n# \u6216\u8005\nsorted_array = sorted(my_array)  # \u8fd4\u56de\u65b0\u6392\u5e8f\u7684\u5217\u8868\n<\/code><\/pre>\n<p>\u5bf9\u4e8eNumPy\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.sort()<\/code>\u51fd\u6570\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\nmy_array = np.array([5, 3, 1, 4, 2])\nsorted_array = np.sort(my_array)\n<\/code><\/pre>\n<p>\u67e5\u627e\u529f\u80fd\u53ef\u4ee5\u901a\u8fc7<code>in<\/code>\u5173\u952e\u5b57\u8fdb\u884c\u7b80\u5355\u67e5\u627e\uff0c\u6216\u4f7f\u7528NumPy\u7684<code>numpy.where()<\/code>\u51fd\u6570\u6765\u67e5\u627e\u7279\u5b9a\u6761\u4ef6\u4e0b\u7684\u5143\u7d20\u4f4d\u7f6e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5904\u7406\u6570\u7ec4\u6570\u636e\u7684\u6838\u5fc3\u5de5\u5177\u5305\u62ecnumpy\u3001\u5217\u8868\u89e3\u6790\u3001Pandas\u7b49\u3002 \u5176\u4e2d\uff0cNumpy\u662f\u5904\u7406\u6570\u7ec4\u6570\u636e\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1162745,"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\/1162736"}],"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=1162736"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1162736\/revisions"}],"predecessor-version":[{"id":1162746,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1162736\/revisions\/1162746"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1162745"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1162736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1162736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1162736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}