{"id":1066684,"date":"2024-12-31T16:28:37","date_gmt":"2024-12-31T08:28:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1066684.html"},"modified":"2024-12-31T16:28:39","modified_gmt":"2024-12-31T08:28:39","slug":"python%e5%a6%82%e4%bd%95%e7%94%9f%e6%88%90%e4%b8%8a%e4%b8%89%e8%a7%92%e7%9f%a9%e9%98%b5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1066684.html","title":{"rendered":"python\u5982\u4f55\u751f\u6210\u4e0a\u4e09\u89d2\u77e9\u9635"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/71d83647-f135-4a72-acde-b6a5ab809f06.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u751f\u6210\u4e0a\u4e09\u89d2\u77e9\u9635\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u751f\u6210\u4e0a\u4e09\u89d2\u77e9\u9635\u6709\u591a\u79cd\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u65b9\u4fbf\u5730\u521b\u5efa\u4e0a\u4e09\u89d2\u77e9\u9635\u3002<strong>\u4f7f\u7528NumPy\u7684<code>triu<\/code>\u51fd\u6570\u3001\u6784\u9020\u5faa\u73af\u548c\u6761\u4ef6\u5224\u65ad\u3001\u5229\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\u7b49\u65b9\u6cd5\u5747\u53ef\u4ee5\u5b9e\u73b0\u4e0a\u4e09\u89d2\u77e9\u9635\u7684\u751f\u6210\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528NumPy\u5e93\u7684<code>triu<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5feb\u901f\u751f\u6210\u4e0a\u4e09\u89d2\u77e9\u9635\u3002\u5728NumPy\u4e2d\uff0c<code>triu<\/code>\u51fd\u6570\u7528\u4e8e\u8fd4\u56de\u77e9\u9635\u7684\u4e0a\u4e09\u89d2\u90e8\u5206\uff08\u5373\u77e9\u9635\u4e2d\u4f4d\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u53ca\u5176\u4e0a\u65b9\u7684\u5143\u7d20\uff09\uff0c\u5176\u4f59\u5143\u7d20\u8bbe\u7f6e\u4e3a\u96f6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a5x5\u7684\u968f\u673a\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.random.rand(5, 5)<\/p>\n<h2><strong>\u5c06\u8be5\u77e9\u9635\u8f6c\u6362\u4e3a\u4e0a\u4e09\u89d2\u77e9\u9635<\/strong><\/h2>\n<p>upper_tri_matrix = np.triu(matrix)<\/p>\n<p>print(&quot;Original Matrix:\\n&quot;, matrix)<\/p>\n<p>print(&quot;Upper Triangular Matrix:\\n&quot;, upper_tri_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u751f\u6210\u4e0a\u4e09\u89d2\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528NumPy\u7684<code>triu<\/code>\u51fd\u6570\u751f\u6210\u4e0a\u4e09\u89d2\u77e9\u9635<\/h3>\n<\/p>\n<p><p><strong>NumPy\u5e93\u7684<code>triu<\/code>\u51fd\u6570<\/strong>\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u6cd5\u6765\u751f\u6210\u4e0a\u4e09\u89d2\u77e9\u9635\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u5feb\u901f\u751f\u6210\u6240\u9700\u7684\u77e9\u9635\uff0c\u8fd8\u80fd\u6709\u6548\u5730\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def generate_upper_triangular_matrix(matrix):<\/p>\n<p>    return np.triu(matrix)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>matrix = np.array([[1, 2, 3],<\/p>\n<p>                   [4, 5, 6],<\/p>\n<p>                   [7, 8, 9]])<\/p>\n<p>upper_tri_matrix = generate_upper_triangular_matrix(matrix)<\/p>\n<p>print(&quot;Upper Triangular Matrix:\\n&quot;, upper_tri_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>np.triu(matrix)<\/code>\u51fd\u6570\u5c06\u8f93\u5165\u77e9\u9635<code>matrix<\/code>\u8f6c\u6362\u4e3a\u4e0a\u4e09\u89d2\u77e9\u9635\u3002<code>matrix<\/code>\u7684\u5bf9\u89d2\u7ebf\u4e0b\u65b9\u5143\u7d20\u88ab\u7f6e\u4e3a\u96f6\uff0c\u800c\u5bf9\u89d2\u7ebf\u53ca\u5176\u4e0a\u65b9\u7684\u5143\u7d20\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5faa\u73af\u548c\u6761\u4ef6\u5224\u65ad\u751f\u6210\u4e0a\u4e09\u89d2\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528NumPy\u5e93\u7684\u51fd\u6570\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7<strong>\u5d4c\u5957\u5faa\u73af\u548c\u6761\u4ef6\u5224\u65ad<\/strong>\u6765\u751f\u6210\u4e0a\u4e09\u89d2\u77e9\u9635\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u8f83\u4e3a\u7e41\u7410\uff0c\u4f46\u4e5f\u80fd\u8fbe\u5230\u76f8\u540c\u7684\u6548\u679c\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def generate_upper_triangular_matrix(matrix):<\/p>\n<p>    rows, cols = len(matrix), len(matrix[0])<\/p>\n<p>    for i in range(rows):<\/p>\n<p>        for j in range(cols):<\/p>\n<p>            if i &gt; j:<\/p>\n<p>                matrix[i][j] = 0<\/p>\n<p>    return matrix<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>matrix = [[1, 2, 3],<\/p>\n<p>          [4, 5, 6],<\/p>\n<p>          [7, 8, 9]]<\/p>\n<p>upper_tri_matrix = generate_upper_triangular_matrix(matrix)<\/p>\n<p>print(&quot;Upper Triangular Matrix:\\n&quot;, upper_tri_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u904d\u5386\u8f93\u5165\u77e9\u9635<code>matrix<\/code>\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u5982\u679c\u5143\u7d20\u4f4d\u4e8e\u5bf9\u89d2\u7ebf\u4ee5\u4e0b\uff08\u5373<code>i &gt; j<\/code>\uff09\uff0c\u5219\u5c06\u5176\u503c\u8bbe\u7f6e\u4e3a\u96f6\u3002\u6700\u540e\u8fd4\u56de\u4fee\u6539\u540e\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e0a\u4e09\u89d2\u77e9\u9635<\/h3>\n<\/p>\n<p><p><strong>\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u6765\u751f\u6210\u4e0a\u4e09\u89d2\u77e9\u9635\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def generate_upper_triangular_matrix(matrix):<\/p>\n<p>    return [[matrix[i][j] if i &lt;= j else 0 for j in range(len(matrix[0]))] for i in range(len(matrix))]<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>matrix = [[1, 2, 3],<\/p>\n<p>          [4, 5, 6],<\/p>\n<p>          [7, 8, 9]]<\/p>\n<p>upper_tri_matrix = generate_upper_triangular_matrix(matrix)<\/p>\n<p>print(&quot;Upper Triangular Matrix:\\n&quot;, upper_tri_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u521b\u5efa\u65b0\u7684\u77e9\u9635\u3002\u5982\u679c\u5143\u7d20\u4f4d\u4e8e\u5bf9\u89d2\u7ebf\u53ca\u5176\u4e0a\u65b9\uff08\u5373<code>i &lt;= j<\/code>\uff09\uff0c\u5219\u4fdd\u7559\u539f\u503c\uff1b\u5426\u5219\u5c06\u5176\u8bbe\u7f6e\u4e3a\u96f6\u3002\u6700\u540e\u8fd4\u56de\u751f\u6210\u7684\u4e0a\u4e09\u89d2\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u751f\u6210\u5355\u4f4d\u77e9\u9635\u7684\u4e0a\u4e09\u89d2\u5f62\u5f0f<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u751f\u6210\u5355\u4f4d\u77e9\u9635\u7684\u4e0a\u4e09\u89d2\u5f62\u5f0f\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u521b\u5efa\u5355\u4f4d\u77e9\u9635\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u4e0a\u4e09\u89d2\u77e9\u9635\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def generate_identity_upper_triangular_matrix(size):<\/p>\n<p>    identity_matrix = np.eye(size)<\/p>\n<p>    return np.triu(identity_matrix)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>size = 3<\/p>\n<p>upper_tri_identity_matrix = generate_identity_upper_triangular_matrix(size)<\/p>\n<p>print(&quot;Upper Triangular Identity Matrix:\\n&quot;, upper_tri_identity_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528<code>np.eye(size)<\/code>\u51fd\u6570\u751f\u6210\u5355\u4f4d\u77e9\u9635\uff0c\u7136\u540e\u4f7f\u7528<code>np.triu(identity_matrix)<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u4e0a\u4e09\u89d2\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u751f\u6210\u968f\u673a\u77e9\u9635\u7684\u4e0a\u4e09\u89d2\u5f62\u5f0f<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u751f\u6210\u968f\u673a\u77e9\u9635\u7684\u4e0a\u4e09\u89d2\u5f62\u5f0f\u3002\u6211\u4eec\u53ef\u4ee5\u5148\u751f\u6210\u4e00\u4e2a\u968f\u673a\u77e9\u9635\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u4e0a\u4e09\u89d2\u77e9\u9635\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def generate_random_upper_triangular_matrix(rows, cols):<\/p>\n<p>    random_matrix = np.random.rand(rows, cols)<\/p>\n<p>    return np.triu(random_matrix)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>rows, cols = 3, 3<\/p>\n<p>upper_tri_random_matrix = generate_random_upper_triangular_matrix(rows, cols)<\/p>\n<p>print(&quot;Upper Triangular Random Matrix:\\n&quot;, upper_tri_random_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528<code>np.random.rand(rows, cols)<\/code>\u51fd\u6570\u751f\u6210\u968f\u673a\u77e9\u9635\uff0c\u7136\u540e\u4f7f\u7528<code>np.triu(random_matrix)<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u4e0a\u4e09\u89d2\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u751f\u6210\u6307\u5b9a\u5143\u7d20\u503c\u7684\u4e0a\u4e09\u89d2\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u751f\u6210\u6307\u5b9a\u5143\u7d20\u503c\u7684\u4e0a\u4e09\u89d2\u77e9\u9635\u3002\u6211\u4eec\u53ef\u4ee5\u5148\u521b\u5efa\u4e00\u4e2a\u77e9\u9635\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u4e0a\u4e09\u89d2\u77e9\u9635\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def generate_specified_upper_triangular_matrix(matrix, value):<\/p>\n<p>    rows, cols = len(matrix), len(matrix[0])<\/p>\n<p>    for i in range(rows):<\/p>\n<p>        for j in range(cols):<\/p>\n<p>            if i &lt;= j:<\/p>\n<p>                matrix[i][j] = value<\/p>\n<p>            else:<\/p>\n<p>                matrix[i][j] = 0<\/p>\n<p>    return matrix<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>matrix = [[0, 0, 0],<\/p>\n<p>          [0, 0, 0],<\/p>\n<p>          [0, 0, 0]]<\/p>\n<p>value = 5<\/p>\n<p>upper_tri_specified_matrix = generate_specified_upper_triangular_matrix(matrix, value)<\/p>\n<p>print(&quot;Upper Triangular Specified Matrix:\\n&quot;, upper_tri_specified_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u904d\u5386\u8f93\u5165\u77e9\u9635<code>matrix<\/code>\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u5982\u679c\u5143\u7d20\u4f4d\u4e8e\u5bf9\u89d2\u7ebf\u53ca\u5176\u4e0a\u65b9\uff08\u5373<code>i &lt;= j<\/code>\uff09\uff0c\u5219\u5c06\u5176\u503c\u8bbe\u7f6e\u4e3a\u6307\u5b9a\u503c\uff1b\u5426\u5219\u5c06\u5176\u8bbe\u7f6e\u4e3a\u96f6\u3002\u6700\u540e\u8fd4\u56de\u751f\u6210\u7684\u4e0a\u4e09\u89d2\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u751f\u6210\u5404\u79cd\u5f62\u5f0f\u7684\u4e0a\u4e09\u89d2\u77e9\u9635\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u65e0\u8bba\u662f\u4f7f\u7528NumPy\u5e93\u8fd8\u662f\u624b\u52a8\u7f16\u5199\u5faa\u73af\u548c\u6761\u4ef6\u5224\u65ad\uff0c\u90fd\u80fd\u591f\u6709\u6548\u5730\u751f\u6210\u4e0a\u4e09\u89d2\u77e9\u9635\u3002\u5e0c\u671b\u8fd9\u4e9b\u65b9\u6cd5\u5bf9\u4f60\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\u4e0a\u4e09\u89d2\u77e9\u9635\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u8f7b\u677e\u521b\u5efa\u4e0a\u4e09\u89d2\u77e9\u9635\u3002\u4f7f\u7528<code>numpy.triu()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u4e00\u4e2a\u7ed9\u5b9a\u7684\u77e9\u9635\u8f6c\u6362\u4e3a\u4e0a\u4e09\u89d2\u77e9\u9635\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a3&#215;3\u7684\u5355\u4f4d\u77e9\u9635\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u4e0a\u4e09\u89d2\u77e9\u9635\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\nmatrix = np.array([[1, 2, 3], \n                   [4, 5, 6], \n                   [7, 8, 9]])\n\nupper_triangular = np.triu(matrix)\nprint(upper_triangular)\n<\/code><\/pre>\n<p><strong>Python\u751f\u6210\u7684\u4e0a\u4e09\u89d2\u77e9\u9635\u53ef\u4ee5\u7528\u4e8e\u54ea\u4e9b\u5e94\u7528\u573a\u666f\uff1f<\/strong><br \/>\u4e0a\u4e09\u89d2\u77e9\u9635\u5728\u6570\u503c\u5206\u6790\u3001\u7ebf\u6027\u4ee3\u6570\u4ee5\u53ca<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u5b83\u4eec\u5e38\u7528\u4e8e\u89e3\u51b3\u7ebf\u6027\u65b9\u7a0b\u7ec4\uff0c\u7279\u522b\u662f\u5728\u9ad8\u65af\u6d88\u5143\u6cd5\u4e2d\u3002\u6b64\u5916\uff0c\u4e0a\u4e09\u89d2\u77e9\u9635\u4e5f\u5728\u7279\u5f81\u503c\u5206\u89e3\u548c\u5947\u5f02\u503c\u5206\u89e3\u4e2d\u53d1\u6325\u7740\u91cd\u8981\u4f5c\u7528\uff0c\u80fd\u591f\u7b80\u5316\u8ba1\u7b97\u8fc7\u7a0b\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u53ea\u60f3\u751f\u6210\u4e00\u4e2a\u7279\u5b9a\u5927\u5c0f\u7684\u4e0a\u4e09\u89d2\u77e9\u9635\uff0c\u8be5\u600e\u4e48\u505a\uff1f<\/strong><br \/>\u8981\u521b\u5efa\u4e00\u4e2a\u7279\u5b9a\u5927\u5c0f\u7684\u4e0a\u4e09\u89d2\u77e9\u9635\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u7684<code>numpy.triu()<\/code>\u51fd\u6570\u7ed3\u5408<code>numpy.zeros()<\/code>\u51fd\u6570\u6765\u5148\u751f\u6210\u4e00\u4e2a\u96f6\u77e9\u9635\u3002\u63a5\u7740\uff0c\u5229\u7528<code>numpy.fill_diagonal()<\/code>\u6216\u76f4\u63a5\u5728\u521b\u5efa\u8fc7\u7a0b\u4e2d\u586b\u5145\u77e9\u9635\u3002\u4f8b\u5982\uff0c\u751f\u6210\u4e00\u4e2a4&#215;4\u7684\u4e0a\u4e09\u89d2\u77e9\u9635\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\nsize = 4\nupper_triangular = np.triu(np.ones((size, size)))\nprint(upper_triangular)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a4&#215;4\u7684\u4e0a\u4e09\u89d2\u77e9\u9635\uff0c\u6240\u6709\u5143\u7d20\u5747\u4e3a1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u751f\u6210\u4e0a\u4e09\u89d2\u77e9\u9635\u6709\u591a\u79cd\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u65b9\u4fbf\u5730\u521b\u5efa\u4e0a\u4e09\u89d2\u77e9\u9635\u3002\u4f7f\u7528NumPy\u7684tr [&hellip;]","protected":false},"author":3,"featured_media":1066699,"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\/1066684"}],"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=1066684"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1066684\/revisions"}],"predecessor-version":[{"id":1066700,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1066684\/revisions\/1066700"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1066699"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1066684"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1066684"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1066684"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}