{"id":1113761,"date":"2025-01-08T17:51:02","date_gmt":"2025-01-08T09:51:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1113761.html"},"modified":"2025-01-08T17:51:04","modified_gmt":"2025-01-08T09:51:04","slug":"python-array%e5%a6%82%e4%bd%95%e7%bb%9f%e4%b8%80%e6%93%8d%e4%bd%9c%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1113761.html","title":{"rendered":"python array\u5982\u4f55\u7edf\u4e00\u64cd\u4f5c\u6570\u7ec4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075238\/6422e054-c98a-4db4-bd2b-47ff55e564a2.webp\" alt=\"python array\u5982\u4f55\u7edf\u4e00\u64cd\u4f5c\u6570\u7ec4\" \/><\/p>\n<p><p> <strong>Python\u6570\u7ec4\u64cd\u4f5c\u7edf\u4e00\u5316\u6709\u51e0\u4e2a\u5e38\u89c1\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528NumPy\u5e93\u3001\u5217\u8868\u89e3\u6790\u3001map()\u51fd\u6570\u3001\u5faa\u73af\u3002<\/strong>\u5176\u4e2d\uff0c<strong>NumPy\u5e93<\/strong>\u662f\u6700\u4e3a\u5f3a\u5927\u548c\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u4f7f\u7528NumPy\u5e93\uff0c\u60a8\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u6570\u7ec4\u8fdb\u884c\u7edf\u4e00\u64cd\u4f5c\uff0c\u501f\u52a9\u5176\u4e30\u5bcc\u7684\u51fd\u6570\u5e93\u548c\u9ad8\u6548\u7684\u8ba1\u7b97\u6027\u80fd\uff0c\u4f7f\u5f97\u6570\u7ec4\u64cd\u4f5c\u66f4\u52a0\u7b80\u4fbf\u548c\u9ad8\u6548\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u5c55\u5f00\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001NUMPY\u5e93<\/p>\n<\/p>\n<p><p>NumPy\u662fPython\u79d1\u5b66\u8ba1\u7b97\u7684\u57fa\u7840\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u652f\u6301\u9ad8\u6548\u6570\u7ec4\u64cd\u4f5c\u7684\u591a\u79cd\u529f\u80fd\u3002\u901a\u8fc7NumPy\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5bf9\u6570\u7ec4\u8fdb\u884c\u5404\u79cd\u7edf\u4e00\u64cd\u4f5c\uff0c\u5305\u62ec\u52a0\u3001\u51cf\u3001\u4e58\u3001\u9664\u3001\u5e7f\u64ad\u7b49\u3002<\/p>\n<\/p>\n<p><h3>1. \u521b\u5efa\u548c\u521d\u59cb\u5316\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u8981\u4f7f\u7528NumPy\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165NumPy\u5e93\uff0c\u5e76\u521b\u5efa\u6570\u7ec4\u3002\u53ef\u4ee5\u4ece\u5217\u8868\u6216\u5143\u7ec4\u521b\u5efa\u6570\u7ec4\uff0c\u6216\u8005\u4f7f\u7528NumPy\u63d0\u4f9b\u7684\u51fd\u6570\u6765\u751f\u6210\u6570\u7ec4\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>array_1 = np.array([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u4f7f\u7528arange\u751f\u6210\u6570\u7ec4<\/strong><\/h2>\n<p>array_2 = np.arange(0, 10, 2)<\/p>\n<h2><strong>\u4f7f\u7528linspace\u751f\u6210\u6570\u7ec4<\/strong><\/h2>\n<p>array_3 = np.linspace(0, 1, 5)<\/p>\n<h2><strong>\u521d\u59cb\u5316\u5168\u96f6\u6570\u7ec4<\/strong><\/h2>\n<p>zeros_array = np.zeros((3, 3))<\/p>\n<h2><strong>\u521d\u59cb\u5316\u5168\u4e00\u6570\u7ec4<\/strong><\/h2>\n<p>ones_array = np.ones((2, 2))<\/p>\n<h2><strong>\u521d\u59cb\u5316\u968f\u673a\u6570\u7ec4<\/strong><\/h2>\n<p>random_array = np.random.rand(4, 4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u6570\u7ec4\u57fa\u672c\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>NumPy\u6570\u7ec4\u652f\u6301\u57fa\u672c\u7684\u6570\u5b66\u8fd0\u7b97\uff0c\u53ef\u4ee5\u76f4\u63a5\u5bf9\u6570\u7ec4\u8fdb\u884c\u52a0\u3001\u51cf\u3001\u4e58\u3001\u9664\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6570\u7ec4\u52a0\u6cd5<\/p>\n<p>sum_array = array_1 + 10<\/p>\n<h2><strong>\u6570\u7ec4\u51cf\u6cd5<\/strong><\/h2>\n<p>diff_array = array_1 - 2<\/p>\n<h2><strong>\u6570\u7ec4\u4e58\u6cd5<\/strong><\/h2>\n<p>prod_array = array_1 * 3<\/p>\n<h2><strong>\u6570\u7ec4\u9664\u6cd5<\/strong><\/h2>\n<p>quot_array = array_1 \/ 2<\/p>\n<h2><strong>\u6570\u7ec4\u7684\u5e7f\u64ad\u673a\u5236<\/strong><\/h2>\n<p>broadcast_array = array_1 + np.array([1, 2, 3, 4, 5])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u6570\u7ec4\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>NumPy\u8fd8\u652f\u6301\u8bb8\u591a\u9ad8\u7ea7\u64cd\u4f5c\uff0c\u5982\u77e9\u9635\u4e58\u6cd5\u3001\u8f6c\u7f6e\u3001\u6c42\u9006\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u77e9\u9635\u4e58\u6cd5<\/p>\n<p>matrix_1 = np.array([[1, 2], [3, 4]])<\/p>\n<p>matrix_2 = np.array([[5, 6], [7, 8]])<\/p>\n<p>matrix_product = np.dot(matrix_1, matrix_2)<\/p>\n<h2><strong>\u77e9\u9635\u8f6c\u7f6e<\/strong><\/h2>\n<p>transposed_matrix = matrix_1.T<\/p>\n<h2><strong>\u77e9\u9635\u6c42\u9006<\/strong><\/h2>\n<p>inverse_matrix = np.linalg.inv(matrix_1)<\/p>\n<h2><strong>\u6570\u7ec4\u7684\u5207\u7247<\/strong><\/h2>\n<p>sliced_array = array_1[1:4]<\/p>\n<h2><strong>\u6570\u7ec4\u7684\u7d22\u5f15<\/strong><\/h2>\n<p>indexed_array = array_1[array_1 &gt; 2]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4. \u6570\u7ec4\u7684\u7edf\u8ba1\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7edf\u8ba1\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u6570\u7ec4\u8fdb\u884c\u7edf\u8ba1\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6570\u7ec4\u7684\u6c42\u548c<\/p>\n<p>sum_value = np.sum(array_1)<\/p>\n<h2><strong>\u6570\u7ec4\u7684\u5e73\u5747\u503c<\/strong><\/h2>\n<p>mean_value = np.mean(array_1)<\/p>\n<h2><strong>\u6570\u7ec4\u7684\u4e2d\u4f4d\u6570<\/strong><\/h2>\n<p>median_value = np.median(array_1)<\/p>\n<h2><strong>\u6570\u7ec4\u7684\u6807\u51c6\u5dee<\/strong><\/h2>\n<p>std_value = np.std(array_1)<\/p>\n<h2><strong>\u6570\u7ec4\u7684\u6700\u5927\u503c<\/strong><\/h2>\n<p>max_value = np.max(array_1)<\/p>\n<h2><strong>\u6570\u7ec4\u7684\u6700\u5c0f\u503c<\/strong><\/h2>\n<p>min_value = np.min(array_1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u5217\u8868\u89e3\u6790<\/p>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u751f\u6210\u548c\u64cd\u4f5c\u5217\u8868\u3002\u901a\u8fc7\u5217\u8868\u89e3\u6790\uff0c\u53ef\u4ee5\u5bf9\u6570\u7ec4\u8fdb\u884c\u7edf\u4e00\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u5217\u8868<\/p>\n<p>list_1 = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u5217\u8868\u7684\u7edf\u4e00\u64cd\u4f5c<\/strong><\/h2>\n<p>list_2 = [x * 2 for x in list_1]<\/p>\n<h2><strong>\u4f7f\u7528\u6761\u4ef6\u7684\u5217\u8868\u89e3\u6790<\/strong><\/h2>\n<p>list_3 = [x for x in list_1 if x &gt; 2]<\/p>\n<h2><strong>\u5d4c\u5957\u5217\u8868\u89e3\u6790<\/strong><\/h2>\n<p>nested_list = [[x * y for y in range(1, 4)] for x in range(1, 4)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001map()\u51fd\u6570<\/p>\n<\/p>\n<p><p>map()\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u9ad8\u9636\u51fd\u6570\uff0c\u7528\u4e8e\u5bf9\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u6307\u5b9a\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u5217\u8868<\/p>\n<p>list_1 = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528map()\u51fd\u6570\u8fdb\u884c\u7edf\u4e00\u64cd\u4f5c<\/strong><\/h2>\n<p>list_2 = list(map(lambda x: x * 2, list_1))<\/p>\n<h2><strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8fdb\u884c\u7edf\u4e00\u64cd\u4f5c<\/strong><\/h2>\n<p>list_3 = list(map(str, list_1))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5faa\u73af<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5faa\u73af\u53ef\u4ee5\u5bf9\u6570\u7ec4\u8fdb\u884c\u9010\u4e00\u64cd\u4f5c\uff0c\u867d\u7136\u4ee3\u7801\u76f8\u5bf9\u5197\u957f\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u662f\u5fc5\u8981\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u5217\u8868<\/p>\n<p>list_1 = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528for\u5faa\u73af\u8fdb\u884c\u7edf\u4e00\u64cd\u4f5c<\/strong><\/h2>\n<p>list_2 = []<\/p>\n<p>for x in list_1:<\/p>\n<p>    list_2.append(x * 2)<\/p>\n<h2><strong>\u4f7f\u7528while\u5faa\u73af\u8fdb\u884c\u7edf\u4e00\u64cd\u4f5c<\/strong><\/h2>\n<p>list_3 = []<\/p>\n<p>i = 0<\/p>\n<p>while i &lt; len(list_1):<\/p>\n<p>    list_3.append(list_1[i] * 2)<\/p>\n<p>    i += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5bf9\u6570\u7ec4\u8fdb\u884c\u7edf\u4e00\u64cd\u4f5c\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5176\u4e2dNumPy\u5e93\u662f\u6700\u4e3a\u5f3a\u5927\u548c\u5e38\u7528\u7684\u5de5\u5177\u3002\u901a\u8fc7NumPy\uff0c\u60a8\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5404\u79cd\u6570\u7ec4\u64cd\u4f5c\uff0c\u5305\u62ec\u57fa\u672c\u7684\u6570\u5b66\u8fd0\u7b97\u3001\u9ad8\u7ea7\u7684\u77e9\u9635\u64cd\u4f5c\u3001\u4e30\u5bcc\u7684\u7edf\u8ba1\u5206\u6790\u7b49\u3002\u5217\u8868\u89e3\u6790\u3001map()\u51fd\u6570\u548c\u5faa\u73af\u4e5f\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u64cd\u4f5c\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002\u65e0\u8bba\u60a8\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u6570\u636e\u7c7b\u578b\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u6570\u7ec4\u7684\u7edf\u4e00\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bf9\u6570\u7ec4\u8fdb\u884c\u6279\u91cf\u5904\u7406\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u9ad8\u6548\u5730\u64cd\u4f5c\u6570\u7ec4\u3002NumPy\u5141\u8bb8\u60a8\u5bf9\u6574\u4e2a\u6570\u7ec4\u8fdb\u884c\u5411\u91cf\u5316\u64cd\u4f5c\uff0c\u8fd9\u6837\u60a8\u53ef\u4ee5\u4e00\u6b21\u6027\u5bf9\u6240\u6709\u5143\u7d20\u6267\u884c\u76f8\u540c\u7684\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528NumPy\u7684\u6570\u7ec4\u8fd0\u7b97\u7b26\u5bf9\u6bcf\u4e2a\u5143\u7d20\u52a0\u3001\u51cf\u3001\u4e58\u6216\u9664\uff0c\u800c\u4e0d\u9700\u8981\u4f7f\u7528\u5faa\u73af\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5bf9\u6570\u7ec4\u8fdb\u884c\u7b5b\u9009\uff1f<\/strong><br \/>\u4f7f\u7528NumPy\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5e03\u5c14\u7d22\u5f15\u6765\u5feb\u901f\u7b5b\u9009\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6761\u4ef6\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528\u8be5\u6761\u4ef6\u6765\u8fc7\u6ee4\u539f\u59cb\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u901f\u5ea6\u66f4\u5feb\uff0c\u9002\u5408\u5904\u7406\u5927\u6570\u636e\u96c6\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u5b9e\u73b0\u6570\u7ec4\u7684\u6570\u5b66\u8fd0\u7b97\uff1f<\/strong><br \/>Python\u4e2d\u7684NumPy\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u5b66\u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u76f4\u63a5\u5728\u6570\u7ec4\u4e0a\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\uff0c\u5982\u6c42\u548c\u3001\u5e73\u5747\u503c\u3001\u6807\u51c6\u5dee\u7b49\u3002\u60a8\u53ea\u9700\u8c03\u7528\u76f8\u5e94\u7684\u51fd\u6570\u5e76\u4f20\u5165\u6570\u7ec4\uff0cNumPy\u4f1a\u81ea\u52a8\u5904\u7406\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u8fd4\u56de\u8ba1\u7b97\u7ed3\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u6570\u7ec4\u64cd\u4f5c\u7edf\u4e00\u5316\u6709\u51e0\u4e2a\u5e38\u89c1\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528NumPy\u5e93\u3001\u5217\u8868\u89e3\u6790\u3001map()\u51fd\u6570\u3001\u5faa\u73af\u3002\u5176\u4e2d\uff0cNumPy [&hellip;]","protected":false},"author":3,"featured_media":1113771,"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\/1113761"}],"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=1113761"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1113761\/revisions"}],"predecessor-version":[{"id":1113772,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1113761\/revisions\/1113772"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1113771"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1113761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1113761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1113761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}