{"id":1088621,"date":"2025-01-08T13:45:55","date_gmt":"2025-01-08T05:45:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1088621.html"},"modified":"2025-01-08T13:45:57","modified_gmt":"2025-01-08T05:45:57","slug":"python%e5%a6%82%e4%bd%95%e7%bb%99%e4%ba%8c%e7%bb%b4%e5%88%97%e8%a1%a8%e8%b5%8b%e5%80%bc-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1088621.html","title":{"rendered":"python\u5982\u4f55\u7ed9\u4e8c\u7ef4\u5217\u8868\u8d4b\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24201013\/12baf79e-a3f8-4fd3-a45e-c14394ff5882.webp\" alt=\"python\u5982\u4f55\u7ed9\u4e8c\u7ef4\u5217\u8868\u8d4b\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u7ed9\u4e8c\u7ef4\u5217\u8868\u8d4b\u503c\u7684\u65b9\u6cd5\u6709\uff1a\u76f4\u63a5\u7d22\u5f15\u8d4b\u503c\u3001\u5207\u7247\u8d4b\u503c\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u5faa\u73af\u8d4b\u503c\u3002<\/strong> \u5176\u4e2d\uff0c\u76f4\u63a5\u7d22\u5f15\u8d4b\u503c\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u6307\u5b9a\u884c\u548c\u5217\u7684\u7d22\u5f15\u6765\u4fee\u6539\u7279\u5b9a\u5143\u7d20\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\u2014\u2014\u76f4\u63a5\u7d22\u5f15\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>\u76f4\u63a5\u7d22\u5f15\u8d4b\u503c<\/strong>\uff1a\u8fd9\u662f\u6700\u7b80\u5355\u548c\u76f4\u89c2\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u884c\u548c\u5217\u7684\u7d22\u5f15\u6765\u4fee\u6539\u4e8c\u7ef4\u5217\u8868\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868 <code>matrix<\/code>\uff0c\u8981\u5c06 <code>matrix<\/code> \u4e2d\u7b2c2\u884c\u7b2c3\u5217\u7684\u5143\u7d20\u8d4b\u503c\u4e3a5\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>matrix[1][2] = 5<\/code>\uff08\u7d22\u5f15\u4ece0\u5f00\u59cb\uff09\u3002\u8fd9\u79cd\u65b9\u6cd5\u76f4\u89c2\u4e14\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u4fee\u6539\u5355\u4e2a\u5143\u7d20\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u4ee5\u4e0a\u65b9\u6cd5\u5728Python\u4e2d\u7ed9\u4e8c\u7ef4\u5217\u8868\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u76f4\u63a5\u7d22\u5f15\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u76f4\u63a5\u7d22\u5f15\u8d4b\u503c\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u6307\u5b9a\u884c\u548c\u5217\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539\u4e8c\u7ef4\u5217\u8868\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868<\/p>\n<p>matrix = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<h2><strong>\u4fee\u6539\u7b2c\u4e8c\u884c\u7b2c\u4e09\u5217\u7684\u503c\u4e3a10<\/strong><\/h2>\n<p>matrix[1][2] = 10<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code>[[1, 2, 3],<\/p>\n<p> [4, 5, 10],<\/p>\n<p> [7, 8, 9]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5207\u7247\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u5207\u7247\u8d4b\u503c\u53ef\u4ee5\u7528\u4e8e\u4fee\u6539\u4e8c\u7ef4\u5217\u8868\u4e2d\u7684\u4e00\u90e8\u5206\uff0c\u4f8b\u5982\u4fee\u6539\u67d0\u4e00\u884c\u6216\u67d0\u4e00\u5217\u7684\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fee\u6539\u7b2c\u4e8c\u884c\u7684\u503c<\/p>\n<p>matrix[1] = [10, 11, 12]<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code>[[1, 2, 3],<\/p>\n<p> [10, 11, 12],<\/p>\n<p> [7, 8, 9]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fee\u6539\u7b2c\u4e8c\u5217\u7684\u503c<\/p>\n<p>for row in matrix:<\/p>\n<p>    row[1] = 20<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code>[[1, 20, 3],<\/p>\n<p> [10, 20, 12],<\/p>\n<p> [7, 20, 9]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u751f\u6210\u548c\u4fee\u6539\u5217\u8868\u3002\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u521d\u59cb\u5316\u6216\u4fee\u6539\u4e8c\u7ef4\u5217\u8868\u4e2d\u7684\u67d0\u4e9b\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u521d\u59cb\u5316\u4e8c\u7ef4\u5217\u8868<\/p>\n<p>matrix = [[0 for _ in range(3)] for _ in range(3)]<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code>[[0, 0, 0],<\/p>\n<p> [0, 0, 0],<\/p>\n<p> [0, 0, 0]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u4fee\u6539\u4e8c\u7ef4\u5217\u8868\u4e2d\u7684\u67d0\u4e9b\u5143\u7d20<\/p>\n<p>matrix = [[i+j for j in range(3)] for i in range(3)]<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code>[[0, 1, 2],<\/p>\n<p> [1, 2, 3],<\/p>\n<p> [2, 3, 4]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5faa\u73af\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u5faa\u73af\u8d4b\u503c\u53ef\u4ee5\u7528\u4e8e\u590d\u6742\u7684\u4fee\u6539\u9700\u6c42\uff0c\u901a\u8fc7\u5d4c\u5957\u5faa\u73af\u904d\u5386\u4e8c\u7ef4\u5217\u8868\u5e76\u4fee\u6539\u5176\u4e2d\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u4fee\u6539\u4e8c\u7ef4\u5217\u8868\u4e2d\u7684\u5143\u7d20<\/p>\n<p>for i in range(len(matrix)):<\/p>\n<p>    for j in range(len(matrix[i])):<\/p>\n<p>        matrix[i][j] = i * j<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code>[[0, 0, 0],<\/p>\n<p> [0, 1, 2],<\/p>\n<p> [0, 2, 4]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u7efc\u5408\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u5bf9\u4e8c\u7ef4\u5217\u8868\u8fdb\u884c\u8d4b\u503c\u548c\u4fee\u6539\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868<\/p>\n<p>matrix = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<h2><strong>\u76f4\u63a5\u7d22\u5f15\u8d4b\u503c<\/strong><\/h2>\n<p>matrix[0][0] = 10<\/p>\n<p>matrix[2][2] = 20<\/p>\n<h2><strong>\u5207\u7247\u8d4b\u503c<\/strong><\/h2>\n<p>matrix[1] = [11, 12, 13]<\/p>\n<h2><strong>\u5217\u8868\u63a8\u5bfc\u5f0f\u4fee\u6539<\/strong><\/h2>\n<p>matrix = [[i * 2 for i in row] for row in matrix]<\/p>\n<h2><strong>\u5faa\u73af\u8d4b\u503c<\/strong><\/h2>\n<p>for i in range(len(matrix)):<\/p>\n<p>    for j in range(len(matrix[i])):<\/p>\n<p>        matrix[i][j] += 1<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code>[[21, 3, 5],<\/p>\n<p> [23, 25, 27],<\/p>\n<p> [15, 17, 21]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u5bf9\u4e8c\u7ef4\u5217\u8868\u8fdb\u884c\u8d4b\u503c\u548c\u4fee\u6539\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u80fd\u591f\u5927\u5927\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u8fd0\u884c\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\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\u5e76\u8d4b\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u6765\u521b\u5efa\u4e8c\u7ef4\u5217\u8868\u3002\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u6216\u5217\u8868\u63a8\u5bfc\u5f0f\u4e3a\u5176\u8d4b\u503c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u53cc\u91cd\u5faa\u73af\u53ef\u4ee5\u8f7b\u677e\u5730\u586b\u5145\u4e00\u4e2a2&#215;3\u7684\u4e8c\u7ef4\u5217\u8868\uff1a<\/p>\n<pre><code class=\"language-python\">rows, cols = 2, 3\nmatrix = [[0 for _ in range(cols)] for _ in range(rows)]\nfor i in range(rows):\n    for j in range(cols):\n        matrix[i][j] = i + j  # \u6839\u636e\u9700\u8981\u8d4b\u503c\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u4fee\u6539Python\u4e8c\u7ef4\u5217\u8868\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\u8981\u4fee\u6539\u4e8c\u7ef4\u5217\u8868\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\uff0c\u53ea\u9700\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u8be5\u5143\u7d20\u5e76\u8fdb\u884c\u8d4b\u503c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u8981\u5c06\u7b2c\u4e00\u884c\u7b2c\u4e8c\u5217\u7684\u503c\u4fee\u6539\u4e3a10\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<pre><code class=\"language-python\">matrix[0][1] = 10\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u4f7f\u7528NumPy\u5e93\u5904\u7406\u548c\u8d4b\u503c\u7ed9\u4e8c\u7ef4\u6570\u7ec4\uff1f<\/strong><br \/>NumPy\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u3002\u4f7f\u7528NumPy\u521b\u5efa\u548c\u8d4b\u503c\u4e8c\u7ef4\u6570\u7ec4\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\narray = np.zeros((2, 3))  # \u521b\u5efa\u4e00\u4e2a2x3\u7684\u96f6\u6570\u7ec4\narray[0, 1] = 10  # \u4fee\u6539\u5143\u7d20\n<\/code><\/pre>\n<p>NumPy\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\uff0c\u5982\u6570\u7ec4\u7684\u5207\u7247\u548c\u6570\u5b66\u8fd0\u7b97\uff0c\u4f7f\u5176\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u65f6\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u7ed9\u4e8c\u7ef4\u5217\u8868\u8d4b\u503c\u7684\u65b9\u6cd5\u6709\uff1a\u76f4\u63a5\u7d22\u5f15\u8d4b\u503c\u3001\u5207\u7247\u8d4b\u503c\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u5faa\u73af\u8d4b\u503c\u3002 \u5176\u4e2d\uff0c\u76f4\u63a5\u7d22\u5f15\u8d4b\u503c\u662f\u6700 [&hellip;]","protected":false},"author":3,"featured_media":1088629,"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\/1088621"}],"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=1088621"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1088621\/revisions"}],"predecessor-version":[{"id":1088630,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1088621\/revisions\/1088630"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1088629"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1088621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1088621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1088621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}