{"id":1079940,"date":"2025-01-08T12:25:28","date_gmt":"2025-01-08T04:25:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1079940.html"},"modified":"2025-01-08T12:25:31","modified_gmt":"2025-01-08T04:25:31","slug":"python%e5%a6%82%e4%bd%95%e5%88%9b%e9%80%a0%e4%b8%80%e4%b8%aa%e6%95%b0%e7%bb%84-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1079940.html","title":{"rendered":"python\u5982\u4f55\u521b\u9020\u4e00\u4e2a\u6570\u7ec4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182727\/db51ce5f-ba53-44b2-a3e6-41c975b04d4f.webp\" alt=\"python\u5982\u4f55\u521b\u9020\u4e00\u4e2a\u6570\u7ec4\" \/><\/p>\n<p><p> <strong>\u5728 Python \u4e2d\uff0c\u521b\u9020\u4e00\u4e2a\u6570\u7ec4\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5217\u8868\uff08list\uff09\u3001NumPy \u5e93\u3001array \u6a21\u5757\u7b49\u3002\u5e38\u7528\u65b9\u6cd5\u6709\u5217\u8868\u3001NumPy \u6570\u7ec4\u3001array \u6a21\u5757\u6570\u7ec4\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528 NumPy \u5e93\u6765\u521b\u5efa\u6570\u7ec4\u3002<\/strong><\/p>\n<\/p>\n<p><p>NumPy \u662f Python \u4e2d\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u57fa\u7840\u5305\uff0c\u63d0\u4f9b\u4e86\u5bf9\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u7684\u652f\u6301\u3002\u901a\u8fc7 NumPy\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u548c\u64cd\u4f5c\u6570\u7ec4\u3002NumPy \u6570\u7ec4\u6bd4 Python \u5217\u8868\u66f4\u9ad8\u6548\uff0c\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\u5c24\u4e3a\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 Python \u5217\u8868\u521b\u5efa\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u5217\u8868\u662f\u4e00\u79cd\u5185\u7f6e\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u6709\u5e8f\u7684\u5143\u7d20\u96c6\u5408\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5c06\u5143\u7d20\u653e\u5728\u65b9\u62ec\u53f7\u4e2d\u6765\u521b\u5efa\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6574\u6570\u7684\u5217\u8868<\/p>\n<p>list1 = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6d6e\u70b9\u6570\u7684\u5217\u8868<\/strong><\/h2>\n<p>list2 = [1.1, 2.2, 3.3, 4.4, 5.5]<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u4e32\u7684\u5217\u8868<\/strong><\/h2>\n<p>list3 = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;]<\/p>\n<h2><strong>\u6253\u5370\u5217\u8868<\/strong><\/h2>\n<p>print(list1)<\/p>\n<p>print(list2)<\/p>\n<p>print(list3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 NumPy \u5e93\u521b\u5efa\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>NumPy \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5e93\uff0c\u7528\u4e8e\u5904\u7406\u5927\u578b\u6570\u7ec4\u548c\u77e9\u9635\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 NumPy \u7684 <code>array<\/code> \u51fd\u6570\u6765\u521b\u5efa\u6570\u7ec4\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5 NumPy \u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5bfc\u5165 NumPy \u5e76\u4f7f\u7528\u5176\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array1 = np.array([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array2 = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<h2><strong>\u6253\u5370\u6570\u7ec4<\/strong><\/h2>\n<p>print(array1)<\/p>\n<p>print(array2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 array \u6a21\u5757\u521b\u5efa\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>Python \u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a <code>array<\/code> \u7684\u6a21\u5757\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u6570\u7ec4\u3002\u4e0e\u5217\u8868\u4e0d\u540c\uff0c\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u5fc5\u987b\u662f\u76f8\u540c\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import array as arr<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6574\u6570\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>array1 = arr.array(&#39;i&#39;, [1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6d6e\u70b9\u6570\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>array2 = arr.array(&#39;d&#39;, [1.1, 2.2, 3.3, 4.4, 5.5])<\/p>\n<h2><strong>\u6253\u5370\u6570\u7ec4<\/strong><\/h2>\n<p>print(array1)<\/p>\n<p>print(array2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001NumPy \u6570\u7ec4\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>1\u3001\u6570\u7ec4\u7684\u57fa\u672c\u5c5e\u6027<\/h4>\n<\/p>\n<p><p>NumPy \u6570\u7ec4\u6709\u8bb8\u591a\u5c5e\u6027\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4e86\u89e3\u6570\u7ec4\u7684\u4fe1\u606f\uff0c\u4f8b\u5982\u5f62\u72b6\u3001\u6570\u636e\u7c7b\u578b\u3001\u5927\u5c0f\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<h2><strong>\u6253\u5370\u6570\u7ec4\u5f62\u72b6<\/strong><\/h2>\n<p>print(&quot;\u6570\u7ec4\u5f62\u72b6:&quot;, array.shape)<\/p>\n<h2><strong>\u6253\u5370\u6570\u7ec4\u6570\u636e\u7c7b\u578b<\/strong><\/h2>\n<p>print(&quot;\u6570\u636e\u7c7b\u578b:&quot;, array.dtype)<\/p>\n<h2><strong>\u6253\u5370\u6570\u7ec4\u5927\u5c0f\uff08\u5143\u7d20\u4e2a\u6570\uff09<\/strong><\/h2>\n<p>print(&quot;\u6570\u7ec4\u5927\u5c0f:&quot;, array.size)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6570\u7ec4\u7684\u57fa\u672c\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>NumPy \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u7ec4\u7684\u52a0\u51cf\u4e58\u9664\u3001\u5207\u7247\u3001\u7d22\u5f15\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array1 = np.array([1, 2, 3])<\/p>\n<p>array2 = np.array([4, 5, 6])<\/p>\n<h2><strong>\u6570\u7ec4\u52a0\u6cd5<\/strong><\/h2>\n<p>sum_array = array1 + array2<\/p>\n<p>print(&quot;\u6570\u7ec4\u52a0\u6cd5:&quot;, sum_array)<\/p>\n<h2><strong>\u6570\u7ec4\u51cf\u6cd5<\/strong><\/h2>\n<p>diff_array = array1 - array2<\/p>\n<p>print(&quot;\u6570\u7ec4\u51cf\u6cd5:&quot;, diff_array)<\/p>\n<h2><strong>\u6570\u7ec4\u4e58\u6cd5<\/strong><\/h2>\n<p>prod_array = array1 * array2<\/p>\n<p>print(&quot;\u6570\u7ec4\u4e58\u6cd5:&quot;, prod_array)<\/p>\n<h2><strong>\u6570\u7ec4\u9664\u6cd5<\/strong><\/h2>\n<p>quot_array = array1 \/ array2<\/p>\n<p>print(&quot;\u6570\u7ec4\u9664\u6cd5:&quot;, quot_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u6570\u7ec4\u7684\u5207\u7247\u548c\u7d22\u5f15<\/h4>\n<\/p>\n<p><p>NumPy \u6570\u7ec4\u652f\u6301\u5207\u7247\u548c\u7d22\u5f15\u64cd\u4f5c\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bbf\u95ee\u548c\u4fee\u6539\u6570\u7ec4\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<h2><strong>\u8bbf\u95ee\u6570\u7ec4\u7684\u5143\u7d20<\/strong><\/h2>\n<p>print(&quot;\u7b2c\u4e00\u4e2a\u5143\u7d20:&quot;, array[0, 0])<\/p>\n<p>print(&quot;\u7b2c\u4e8c\u884c\u7b2c\u4e09\u5217\u7684\u5143\u7d20:&quot;, array[1, 2])<\/p>\n<h2><strong>\u4fee\u6539\u6570\u7ec4\u7684\u5143\u7d20<\/strong><\/h2>\n<p>array[0, 0] = 10<\/p>\n<p>print(&quot;\u4fee\u6539\u540e\u7684\u6570\u7ec4:\\n&quot;, array)<\/p>\n<h2><strong>\u6570\u7ec4\u5207\u7247<\/strong><\/h2>\n<p>slice_array = array[0:2, 1:3]<\/p>\n<p>print(&quot;\u6570\u7ec4\u5207\u7247:\\n&quot;, slice_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u6570\u7ec4\u7684\u5f62\u72b6\u53d8\u6362<\/h4>\n<\/p>\n<p><p>NumPy \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u53d8\u6362\u6570\u7ec4\u7684\u5f62\u72b6\uff0c\u4f8b\u5982 <code>reshape<\/code>\u3001<code>ravel<\/code> \u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array([1, 2, 3, 4, 5, 6])<\/p>\n<h2><strong>\u5c06\u4e00\u7ef4\u6570\u7ec4\u53d8\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>reshaped_array = array.reshape(2, 3)<\/p>\n<p>print(&quot;\u53d8\u6362\u540e\u7684\u4e8c\u7ef4\u6570\u7ec4:\\n&quot;, reshaped_array)<\/p>\n<h2><strong>\u5c06\u4e8c\u7ef4\u6570\u7ec4\u5c55\u5e73\u4e3a\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>flattened_array = reshaped_array.ravel()<\/p>\n<p>print(&quot;\u5c55\u5e73\u540e\u7684\u6570\u7ec4:&quot;, flattened_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001NumPy \u6570\u7ec4\u7684\u9ad8\u7ea7\u529f\u80fd<\/h3>\n<\/p>\n<p><h4>1\u3001\u6570\u7ec4\u7684\u5e7f\u64ad\u673a\u5236<\/h4>\n<\/p>\n<p><p>NumPy \u7684\u5e7f\u64ad\u673a\u5236\u5141\u8bb8\u6211\u4eec\u5bf9\u4e0d\u540c\u5f62\u72b6\u7684\u6570\u7ec4\u8fdb\u884c\u7b97\u672f\u8fd0\u7b97\uff0c\u907f\u514d\u4e86\u663e\u5f0f\u5730\u590d\u5236\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4\u548c\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array1 = np.array([1, 2, 3])<\/p>\n<p>array2 = np.array([[4], [5], [6]])<\/p>\n<h2><strong>\u5229\u7528\u5e7f\u64ad\u673a\u5236\u8fdb\u884c\u52a0\u6cd5\u8fd0\u7b97<\/strong><\/h2>\n<p>broadcasted_array = array1 + array2<\/p>\n<p>print(&quot;\u5e7f\u64ad\u673a\u5236\u7ed3\u679c:\\n&quot;, broadcasted_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6570\u7ec4\u7684\u5411\u91cf\u5316\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>NumPy \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5411\u91cf\u5316\u64cd\u4f5c\u51fd\u6570\uff0c\u53ef\u4ee5\u5bf9\u6570\u7ec4\u8fdb\u884c\u5feb\u901f\u7684\u6279\u91cf\u64cd\u4f5c\uff0c\u907f\u514d\u4e86\u4f7f\u7528\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u8ba1\u7b97\u6570\u7ec4\u5143\u7d20\u7684\u5e73\u65b9<\/strong><\/h2>\n<p>squared_array = np.square(array)<\/p>\n<p>print(&quot;\u6570\u7ec4\u5143\u7d20\u7684\u5e73\u65b9:&quot;, squared_array)<\/p>\n<h2><strong>\u8ba1\u7b97\u6570\u7ec4\u5143\u7d20\u7684\u5e73\u65b9\u6839<\/strong><\/h2>\n<p>sqrt_array = np.sqrt(array)<\/p>\n<p>print(&quot;\u6570\u7ec4\u5143\u7d20\u7684\u5e73\u65b9\u6839:&quot;, sqrt_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u6570\u7ec4\u7684\u7edf\u8ba1\u51fd\u6570<\/h4>\n<\/p>\n<p><p>NumPy \u63d0\u4f9b\u4e86\u591a\u79cd\u7edf\u8ba1\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8ba1\u7b97\u6570\u7ec4\u7684\u7edf\u8ba1\u91cf\uff0c\u4f8b\u5982\u5747\u503c\u3001\u65b9\u5dee\u3001\u6807\u51c6\u5dee\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u8ba1\u7b97\u6570\u7ec4\u7684\u5747\u503c<\/strong><\/h2>\n<p>mean = np.mean(array)<\/p>\n<p>print(&quot;\u6570\u7ec4\u5747\u503c:&quot;, mean)<\/p>\n<h2><strong>\u8ba1\u7b97\u6570\u7ec4\u7684\u65b9\u5dee<\/strong><\/h2>\n<p>variance = np.var(array)<\/p>\n<p>print(&quot;\u6570\u7ec4\u65b9\u5dee:&quot;, variance)<\/p>\n<h2><strong>\u8ba1\u7b97\u6570\u7ec4\u7684\u6807\u51c6\u5dee<\/strong><\/h2>\n<p>std_dev = np.std(array)<\/p>\n<p>print(&quot;\u6570\u7ec4\u6807\u51c6\u5dee:&quot;, std_dev)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001NumPy \u6570\u7ec4\u7684\u9ad8\u7ea7\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001\u7ebf\u6027\u4ee3\u6570\u8fd0\u7b97<\/h4>\n<\/p>\n<p><p>NumPy \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ebf\u6027\u4ee3\u6570\u8fd0\u7b97\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u77e9\u9635\u4e58\u6cd5\u3001\u77e9\u9635\u9006\u3001\u7279\u5f81\u503c\u5206\u89e3\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>matrix1 = np.array([[1, 2], [3, 4]])<\/p>\n<p>matrix2 = np.array([[5, 6], [7, 8]])<\/p>\n<h2><strong>\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>product = np.dot(matrix1, matrix2)<\/p>\n<p>print(&quot;\u77e9\u9635\u4e58\u6cd5\u7ed3\u679c:\\n&quot;, product)<\/p>\n<h2><strong>\u8ba1\u7b97\u77e9\u9635\u7684\u9006<\/strong><\/h2>\n<p>inverse = np.linalg.inv(matrix1)<\/p>\n<p>print(&quot;\u77e9\u9635\u7684\u9006:\\n&quot;, inverse)<\/p>\n<h2><strong>\u8ba1\u7b97\u77e9\u9635\u7684\u7279\u5f81\u503c\u548c\u7279\u5f81\u5411\u91cf<\/strong><\/h2>\n<p>eigenvalues, eigenvectors = np.linalg.eig(matrix1)<\/p>\n<p>print(&quot;\u77e9\u9635\u7684\u7279\u5f81\u503c:&quot;, eigenvalues)<\/p>\n<p>print(&quot;\u77e9\u9635\u7684\u7279\u5f81\u5411\u91cf:\\n&quot;, eigenvectors)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u968f\u673a\u6570\u751f\u6210<\/h4>\n<\/p>\n<p><p>NumPy \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u968f\u673a\u6570\u751f\u6210\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u751f\u6210\u5404\u79cd\u5206\u5e03\u7684\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u5747\u5300\u5206\u5e03\u7684\u968f\u673a\u6570<\/strong><\/h2>\n<p>uniform_random = np.random.rand(5)<\/p>\n<p>print(&quot;\u5747\u5300\u5206\u5e03\u7684\u968f\u673a\u6570:&quot;, uniform_random)<\/p>\n<h2><strong>\u751f\u6210\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570<\/strong><\/h2>\n<p>normal_random = np.random.randn(5)<\/p>\n<p>print(&quot;\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570:&quot;, normal_random)<\/p>\n<h2><strong>\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570<\/strong><\/h2>\n<p>random_integers = np.random.randint(1, 10, size=5)<\/p>\n<p>print(&quot;\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570:&quot;, random_integers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u6570\u7ec4\u7684\u6392\u5e8f\u548c\u641c\u7d22<\/h4>\n<\/p>\n<p><p>NumPy \u63d0\u4f9b\u4e86\u591a\u79cd\u6392\u5e8f\u548c\u641c\u7d22\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u548c\u67e5\u627e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array([5, 2, 8, 1, 3])<\/p>\n<h2><strong>\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_array = np.sort(array)<\/p>\n<p>print(&quot;\u6392\u5e8f\u540e\u7684\u6570\u7ec4:&quot;, sorted_array)<\/p>\n<h2><strong>\u67e5\u627e\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u7d22\u5f15<\/strong><\/h2>\n<p>index = np.where(array == 8)<\/p>\n<p>print(&quot;\u5143\u7d20 8 \u7684\u7d22\u5f15:&quot;, index)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5728 Python \u4e2d\u521b\u5efa\u6570\u7ec4\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5217\u8868\u3001NumPy \u5e93\u3001array \u6a21\u5757\u7b49\u3002\u5c24\u5176\u662f NumPy \u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\u548c\u9ad8\u7ea7\u529f\u80fd\uff0c\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5904\u7406\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5bf9\u4f60\u5728 Python \u4e2d\u4f7f\u7528\u6570\u7ec4\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4\uff1f<\/strong><br \/>\u5728Python\u4e2d\u521b\u5efa\u4e00\u7ef4\u6570\u7ec4\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\uff08list\uff09\u6216NumPy\u5e93\u3002\u4f7f\u7528\u5217\u8868\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u65b9\u62ec\u53f7\u76f4\u63a5\u5b9a\u4e49\u5143\u7d20\uff0c\u5982 <code>arr = [1, 2, 3, 4, 5]<\/code>\u3002\u82e5\u4f7f\u7528NumPy\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5NumPy\u5e93\uff0c\u7136\u540e\u53ef\u4ee5\u4f7f\u7528 <code>numpy.array()<\/code> \u51fd\u6570\uff0c\u4f8b\u5982\uff1a<code>import numpy as np; arr = np.array([1, 2, 3, 4, 5])<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u53ef\u4ee5\u521b\u5efa\u591a\u7ef4\u6570\u7ec4\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u652f\u6301\u521b\u5efa\u591a\u7ef4\u6570\u7ec4\uff0c\u7279\u522b\u662f\u901a\u8fc7NumPy\u5e93\u3002\u4f7f\u7528NumPy\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4\uff08\u77e9\u9635\uff09\u6216\u66f4\u9ad8\u7ef4\u5ea6\u7684\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a2&#215;3\u7684\u4e8c\u7ef4\u6570\u7ec4\u53ef\u4ee5\u4f7f\u7528\uff1a<code>arr = np.array([[1, 2, 3], [4, 5, 6]])<\/code>\u3002\u8fd9\u6837\u4f60\u5c31\u5f97\u5230\u4e86\u4e00\u4e2a\u5305\u542b\u4e24\u884c\u4e09\u5217\u7684\u6570\u7ec4\u3002<\/p>\n<p><strong>\u5982\u4f55\u5411\u5df2\u6709\u7684Python\u6570\u7ec4\u4e2d\u6dfb\u52a0\u5143\u7d20\uff1f<\/strong><br \/>\u5bf9\u4e8e\u5217\u8868\u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>append()<\/code> \u65b9\u6cd5\u5c06\u5143\u7d20\u6dfb\u52a0\u5230\u672b\u5c3e\uff0c\u4f8b\u5982\uff1a<code>arr.append(6)<\/code>\u3002\u82e5\u4f7f\u7528NumPy\u6570\u7ec4\uff0c\u6dfb\u52a0\u5143\u7d20\u53ef\u4ee5\u4f7f\u7528 <code>numpy.append()<\/code> \u51fd\u6570\uff0c\u4f8b\u5982\uff1a<code>arr = np.append(arr, 6)<\/code>\uff0c\u8bf7\u6ce8\u610f\uff0c\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u539f\u6570\u7ec4\u4e0d\u4f1a\u88ab\u4fee\u6539\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728 Python \u4e2d\uff0c\u521b\u9020\u4e00\u4e2a\u6570\u7ec4\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5217\u8868\uff08list\uff09\u3001NumPy \u5e93\u3001array \u6a21\u5757\u7b49\u3002\u5e38 [&hellip;]","protected":false},"author":3,"featured_media":1079947,"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\/1079940"}],"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=1079940"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1079940\/revisions"}],"predecessor-version":[{"id":1079948,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1079940\/revisions\/1079948"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1079947"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1079940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1079940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1079940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}