{"id":1148036,"date":"2025-01-13T16:32:16","date_gmt":"2025-01-13T08:32:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1148036.html"},"modified":"2025-01-13T16:32:18","modified_gmt":"2025-01-13T08:32:18","slug":"%e8%a1%8c%e5%88%97%e8%a1%a8%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%bapython","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1148036.html","title":{"rendered":"\u884c\u5217\u8868\u5982\u4f55\u8868\u793apython"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25170219\/259f89a2-4a7c-45e9-84de-11c06facc99a.webp\" alt=\"\u884c\u5217\u8868\u5982\u4f55\u8868\u793apython\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u884c\u5217\u8868\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5d4c\u5957\u5217\u8868\uff08\u5373\u5217\u8868\u4e2d\u7684\u5217\u8868\uff09\u6765\u8868\u793a\u3002<strong>\u884c\u5217\u8868\u662f\u4e00\u79cd\u4e8c\u7ef4\u6570\u636e\u7ed3\u6784\u3001\u6bcf\u4e2a\u5b50\u5217\u8868\u4ee3\u8868\u4e00\u884c\u3001\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u548c\u5217\u8868\u7406\u89e3\u7b49\u65b9\u5f0f\u8fdb\u884c\u64cd\u4f5c<\/strong>\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u8868\u793a\u884c\u5217\u8868\uff0c\u5e76\u5c55\u793a\u4e00\u4e9b\u5e38\u89c1\u7684\u64cd\u4f5c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u521b\u5efa\u884c\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u521b\u5efa\u884c\u5217\u8868\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u5373\u53ef\u3002\u6bcf\u4e2a\u5b50\u5217\u8868\u4ee3\u8868\u4e00\u884c\uff0c\u6574\u4e2a\u5217\u8868\u4ee3\u8868\u6574\u4e2a\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a3x3\u7684\u884c\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<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bbf\u95ee\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u6765\u8bbf\u95ee\u884c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<strong>\u884c\u5217\u8868\u662f\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\uff0c\u901a\u8fc7\u4e24\u4e2a\u7d22\u5f15\u6765\u8bbf\u95ee\u5177\u4f53\u7684\u5143\u7d20\uff1a\u7b2c\u4e00\u4e2a\u7d22\u5f15\u8868\u793a\u884c\uff0c\u7b2c\u4e8c\u4e2a\u7d22\u5f15\u8868\u793a\u5217<\/strong>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbf\u95ee\u7b2c\u4e00\u884c\u7b2c\u4e8c\u5217\u7684\u5143\u7d20<\/p>\n<p>element = matrix[0][1]<\/p>\n<p>print(element)  # \u8f93\u51fa: 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4fee\u6539\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u4e0e\u8bbf\u95ee\u5143\u7d20\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u4fee\u6539\u884c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fee\u6539\u7b2c\u4e09\u884c\u7b2c\u4e09\u5217\u7684\u5143\u7d20<\/p>\n<p>matrix[2][2] = 10<\/p>\n<p>print(matrix)  # \u8f93\u51fa: [[1, 2, 3], [4, 5, 6], [7, 8, 10]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6dfb\u52a0\u548c\u5220\u9664\u884c\u6216\u5217<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u7684\u65b9\u6cd5\u6765\u6dfb\u52a0\u6216\u5220\u9664\u884c\u548c\u5217\u3002<\/p>\n<\/p>\n<p><h4>\u6dfb\u52a0\u884c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u65b0\u884c<\/p>\n<p>new_row = [10, 11, 12]<\/p>\n<p>matrix.append(new_row)<\/p>\n<p>print(matrix)  # \u8f93\u51fa: [[1, 2, 3], [4, 5, 6], [7, 8, 10], [10, 11, 12]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5220\u9664\u884c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5220\u9664\u7b2c\u4e8c\u884c<\/p>\n<p>del matrix[1]<\/p>\n<p>print(matrix)  # \u8f93\u51fa: [[1, 2, 3], [7, 8, 10], [10, 11, 12]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6dfb\u52a0\u5217<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u65b0\u5217<\/p>\n<p>for row in matrix:<\/p>\n<p>    row.append(0)<\/p>\n<p>print(matrix)  # \u8f93\u51fa: [[1, 2, 3, 0], [7, 8, 10, 0], [10, 11, 12, 0]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5220\u9664\u5217<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5220\u9664\u7b2c\u4e8c\u5217<\/p>\n<p>for row in matrix:<\/p>\n<p>    del row[1]<\/p>\n<p>print(matrix)  # \u8f93\u51fa: [[1, 3, 0], [7, 10, 0], [10, 12, 0]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u904d\u5386\u884c\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u6765\u904d\u5386\u884c\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u904d\u5386\u884c\u5217\u8868<\/p>\n<p>for row in matrix:<\/p>\n<p>    for element in row:<\/p>\n<p>        print(element, end=&#39; &#39;)<\/p>\n<p>    print()  # \u8f93\u51fa: 1 3 0 \\n 7 10 0 \\n 10 12 0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5217\u8868\u7406\u89e3<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u7406\u89e3\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u548c\u64cd\u4f5c\u884c\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>\u521b\u5efa\u884c\u5217\u8868<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5217\u8868\u7406\u89e3\u521b\u5efa\u4e00\u4e2a3x3\u7684\u884c\u5217\u8868<\/p>\n<p>matrix = [[i + j for j in range(3)] for i in range(3)]<\/p>\n<p>print(matrix)  # \u8f93\u51fa: [[0, 1, 2], [1, 2, 3], [2, 3, 4]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8f6c\u7f6e\u884c\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u8f6c\u7f6e\u884c\u5217\u8868\u662f\u5c06\u884c\u548c\u5217\u4e92\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f6c\u7f6e\u884c\u5217\u8868<\/p>\n<p>transposed_matrix = [[row[i] for row in matrix] for i in range(len(matrix[0]))]<\/p>\n<p>print(transposed_matrix)  # \u8f93\u51fa: [[0, 1, 2], [1, 2, 3], [2, 3, 4]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5e38\u89c1\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>\u77e9\u9635\u52a0\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u77e9\u9635\u52a0\u6cd5<\/p>\n<p>matrix1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p>matrix2 = [[9, 8, 7], [6, 5, 4], [3, 2, 1]]<\/p>\n<p>result = [[matrix1[i][j] + matrix2[i][j] for j in range(len(matrix1[0]))] for i in range(len(matrix1))]<\/p>\n<p>print(result)  # \u8f93\u51fa: [[10, 10, 10], [10, 10, 10], [10, 10, 10]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u77e9\u9635\u4e58\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u77e9\u9635\u4e58\u6cd5<\/p>\n<p>matrix1 = [[1, 2, 3], [4, 5, 6]]<\/p>\n<p>matrix2 = [[7, 8], [9, 10], [11, 12]]<\/p>\n<p>result = [[sum(a * b for a, b in zip(matrix1_row, matrix2_col)) for matrix2_col in zip(*matrix2)] for matrix1_row in matrix1]<\/p>\n<p>print(result)  # \u8f93\u51fa: [[58, 64], [139, 154]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>\u867d\u7136Python\u5185\u7f6e\u7684\u5217\u8868\u53ef\u4ee5\u8868\u793a\u884c\u5217\u8868\uff0c\u4f46\u5728\u5904\u7406\u5927\u578b\u6216\u590d\u6742\u7684\u77e9\u9635\u8fd0\u7b97\u65f6\uff0cNumPy\u5e93\u63d0\u4f9b\u4e86\u66f4\u9ad8\u6548\u548c\u65b9\u4fbf\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5NumPy<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5NumPy\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u521b\u5efa\u6570\u7ec4<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a3x3\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8bbf\u95ee\u548c\u4fee\u6539\u5143\u7d20<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbf\u95ee\u5143\u7d20<\/p>\n<p>element = matrix[0, 1]<\/p>\n<p>print(element)  # \u8f93\u51fa: 2<\/p>\n<h2><strong>\u4fee\u6539\u5143\u7d20<\/strong><\/h2>\n<p>matrix[2, 2] = 10<\/p>\n<p>print(matrix)  # \u8f93\u51fa: [[ 1  2  3]<\/p>\n<p>              #         [ 4  5  6]<\/p>\n<p>              #         [ 7  8 10]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u77e9\u9635\u8fd0\u7b97<\/h4>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u77e9\u9635\u8fd0\u7b97\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u77e9\u9635\u52a0\u6cd5<\/p>\n<p>matrix1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p>matrix2 = np.array([[9, 8, 7], [6, 5, 4], [3, 2, 1]])<\/p>\n<p>result = matrix1 + matrix2<\/p>\n<p>print(result)  # \u8f93\u51fa: [[10 10 10]<\/p>\n<p>              #         [10 10 10]<\/p>\n<p>              #         [10 10 10]]<\/p>\n<h2><strong>\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>matrix1 = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<p>matrix2 = np.array([[7, 8], [9, 10], [11, 12]])<\/p>\n<p>result = np.dot(matrix1, matrix2)<\/p>\n<p>print(result)  # \u8f93\u51fa: [[ 58  64]<\/p>\n<p>              #         [139 154]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u884c\u5217\u8868\u5728\u8bb8\u591a\u5b9e\u9645\u5e94\u7528\u4e2d\u975e\u5e38\u6709\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>\u6570\u636e\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u884c\u5217\u8868\u5e38\u7528\u4e8e\u5b58\u50a8\u548c\u64cd\u4f5c\u6570\u636e\u96c6\uff0c\u4f8b\u5982\u5728\u6570\u636e\u5206\u6790\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528\u884c\u5217\u8868\u6765\u8868\u793a\u548c\u5904\u7406\u6570\u636e\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u5b58\u50a8\u5b66\u751f\u6210\u7ee9<\/p>\n<p>grades = [<\/p>\n<p>    [&quot;Alice&quot;, 85, 90, 88],<\/p>\n<p>    [&quot;Bob&quot;, 78, 82, 84],<\/p>\n<p>    [&quot;Charlie&quot;, 92, 88, 91]<\/p>\n<p>]<\/p>\n<h2><strong>\u8ba1\u7b97\u6bcf\u4e2a\u5b66\u751f\u7684\u5e73\u5747\u6210\u7ee9<\/strong><\/h2>\n<p>for student in grades:<\/p>\n<p>    name = student[0]<\/p>\n<p>    scores = student[1:]<\/p>\n<p>    average = sum(scores) \/ len(scores)<\/p>\n<p>    print(f&quot;{name}&#39;s average score is {average}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u56fe\u50cf\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u4e2d\uff0c\u56fe\u50cf\u901a\u5e38\u8868\u793a\u4e3a\u884c\u5217\u8868\uff0c\u6bcf\u4e2a\u5143\u7d20\u8868\u793a\u50cf\u7d20\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u7070\u5ea6\u56fe\u50cf<\/p>\n<p>image = [<\/p>\n<p>    [0, 255, 0],<\/p>\n<p>    [255, 0, 255],<\/p>\n<p>    [0, 255, 0]<\/p>\n<p>]<\/p>\n<h2><strong>\u8ba1\u7b97\u56fe\u50cf\u7684\u5e73\u5747\u50cf\u7d20\u503c<\/strong><\/h2>\n<p>total = sum(sum(row) for row in image)<\/p>\n<p>num_pixels = sum(len(row) for row in image)<\/p>\n<p>average_pixel_value = total \/ num_pixels<\/p>\n<p>print(f&quot;Average pixel value: {average_pixel_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u56fe\u7684\u8868\u793a<\/h4>\n<\/p>\n<p><p>\u884c\u5217\u8868\u53ef\u4ee5\u7528\u4e8e\u8868\u793a\u56fe\u7684\u90bb\u63a5\u77e9\u9635\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u8868\u793a\u4e24\u4e2a\u8282\u70b9\u4e4b\u95f4\u7684\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u56fe\u7684\u90bb\u63a5\u77e9\u9635<\/p>\n<p>graph = [<\/p>\n<p>    [0, 1, 0, 0],<\/p>\n<p>    [1, 0, 1, 0],<\/p>\n<p>    [0, 1, 0, 1],<\/p>\n<p>    [0, 0, 1, 0]<\/p>\n<p>]<\/p>\n<h2><strong>\u67e5\u627e\u8282\u70b91\u7684\u90bb\u5c45<\/strong><\/h2>\n<p>neighbors = [index for index, value in enumerate(graph[1]) if value == 1]<\/p>\n<p>print(f&quot;Node 1&#39;s neighbors: {neighbors}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u4f18\u5316\u548c\u6027\u80fd\u8003\u8651<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u578b\u884c\u5217\u8868\u65f6\uff0c\u6027\u80fd\u53ef\u80fd\u6210\u4e3a\u4e00\u4e2a\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316\u548c\u6027\u80fd\u8003\u8651\u7684\u5efa\u8bae\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528NumPy<\/h4>\n<\/p>\n<p><p>\u5982\u524d\u6240\u8ff0\uff0cNumPy\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4f7f\u7528NumPy\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97<\/strong><\/h2>\n<p>matrix1 = np.random.rand(1000, 1000)<\/p>\n<p>matrix2 = np.random.rand(1000, 1000)<\/p>\n<p>result = np.dot(matrix1, matrix2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u590d\u5236<\/h4>\n<\/p>\n<p><p>\u5728\u64cd\u4f5c\u884c\u5217\u8868\u65f6\uff0c\u5c3d\u91cf\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5217\u8868\u590d\u5236\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\uff0c\u4f7f\u7528\u751f\u6210\u5668\u800c\u4e0d\u662f\u5217\u8868\u7406\u89e3\u53ef\u4ee5\u8282\u7701\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u751f\u6210\u5668\u8ba1\u7b97\u77e9\u9635\u7684\u884c\u548c<\/p>\n<p>row_sums = (sum(row) for row in matrix)<\/p>\n<p>for row_sum in row_sums:<\/p>\n<p>    print(row_sum)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<\/h4>\n<\/p>\n<p><p>Python\u5185\u7f6e\u51fd\u6570\u901a\u5e38\u7ecf\u8fc7\u4f18\u5316\uff0c\u6027\u80fd\u4f18\u4e8e\u624b\u5199\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8ba1\u7b97\u77e9\u9635\u7684\u884c\u548c<\/p>\n<p>row_sums = map(sum, matrix)<\/p>\n<p>for row_sum in row_sums:<\/p>\n<p>    print(row_sum)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u884c\u5217\u8868\u662fPython\u4e2d\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u8868\u793a\u548c\u64cd\u4f5c\u4e8c\u7ef4\u6570\u636e\u3002\u901a\u8fc7\u5d4c\u5957\u5217\u8868\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u548c\u64cd\u4f5c\u884c\u5217\u8868\uff0c\u5e76\u4f7f\u7528\u7d22\u5f15\u8bbf\u95ee\u548c\u4fee\u6539\u5143\u7d20\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u65b9\u6cd5\u6dfb\u52a0\u548c\u5220\u9664\u884c\u548c\u5217\uff0c\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u548c\u5217\u8868\u7406\u89e3\u904d\u5386\u548c\u5904\u7406\u884c\u5217\u8868\u3002\u5bf9\u4e8e\u66f4\u9ad8\u6548\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u3002\u884c\u5217\u8868\u5728\u6570\u636e\u5206\u6790\u3001\u56fe\u50cf\u5904\u7406\u548c\u56fe\u7684\u8868\u793a\u7b49\u9886\u57df\u6709\u5e7f\u6cdb\u5e94\u7528\u3002\u5728\u5904\u7406\u5927\u578b\u884c\u5217\u8868\u65f6\uff0c\u4f18\u5316\u548c\u6027\u80fd\u8003\u8651\u4e5f\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u5e0c\u671b\u60a8\u80fd\u591f\u638c\u63e1\u5728Python\u4e2d\u8868\u793a\u548c\u64cd\u4f5c\u884c\u5217\u8868\u7684\u57fa\u672c\u65b9\u6cd5\uff0c\u5e76\u80fd\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7075\u6d3b\u4f7f\u7528\u8fd9\u4e9b\u6280\u5de7\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u884c\u5217\u8868\u5728Python\u4e2d\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u884c\u5217\u8868\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u901a\u5e38\u7528\u4e8e\u5b58\u50a8\u548c\u7ec4\u7ec7\u6570\u636e\u3002\u5b83\u53ef\u4ee5\u88ab\u89c6\u4e3a\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e00\u884c\u4ee3\u8868\u4e00\u4e2a\u6570\u636e\u8bb0\u5f55\uff0c\u800c\u6bcf\u4e00\u5217\u5219\u5bf9\u5e94\u4e8e\u4e00\u4e2a\u5b57\u6bb5\u6216\u5c5e\u6027\u3002\u5728Python\u4e2d\uff0c\u884c\u5217\u8868\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u7684\u5d4c\u5957\u6765\u5b9e\u73b0\uff0c\u4f8b\u5982\uff0c\u4f7f\u7528\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5217\u8868\u7684\u5217\u8868\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u884c\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u5217\u8868\u5d4c\u5957\u6765\u521b\u5efa\u884c\u5217\u8868\u3002\u6bd4\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u6765\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u4e09\u884c\u4e24\u5217\u7684\u884c\u5217\u8868\uff1a  <\/p>\n<pre><code class=\"language-python\">row_list = [[1, &#39;Alice&#39;], [2, &#39;Bob&#39;], [3, &#39;Charlie&#39;]]\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6bcf\u4e2a\u5185\u90e8\u5217\u8868\u4ee3\u8868\u4e00\u884c\uff0c\u5305\u542b\u4e86\u4e00\u4e2a\u6570\u5b57\u548c\u4e00\u4e2a\u540d\u5b57\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bbf\u95ee\u548c\u4fee\u6539\u884c\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u8bbf\u95ee\u548c\u4fee\u6539\u884c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u884c\u5217\u8868\u7684\u7d22\u5f15\u4ece\u96f6\u5f00\u59cb\uff0c\u9996\u5148\u662f\u884c\u7d22\u5f15\uff0c\u63a5\u7740\u662f\u5217\u7d22\u5f15\u3002\u6bd4\u5982\uff0c\u8bbf\u95ee\u7b2c\u4e8c\u884c\u7b2c\u4e00\u5217\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">element = row_list[1][0]  # \u8bbf\u95ee\u7b2c\u4e8c\u884c\u7b2c\u4e00\u5217\uff0c\u8fd4\u56de2\n<\/code><\/pre>\n<p>\u8981\u4fee\u6539\u5143\u7d20\uff0c\u53ef\u4ee5\u76f4\u63a5\u6307\u5b9a\u7d22\u5f15\u8fdb\u884c\u8d4b\u503c\uff1a  <\/p>\n<pre><code class=\"language-python\">row_list[1][1] = &#39;Robert&#39;  # \u5c06\u7b2c\u4e8c\u884c\u7b2c\u4e8c\u5217\u7684\u503c\u4fee\u6539\u4e3a&#39;Robert&#39;\n<\/code><\/pre>\n<p><strong>\u884c\u5217\u8868\u9002\u5408\u7528\u4e8e\u54ea\u4e9b\u573a\u666f\uff1f<\/strong><br \/>\u884c\u5217\u8868\u7279\u522b\u9002\u5408\u7528\u4e8e\u5b58\u50a8\u8868\u683c\u6570\u636e\uff0c\u5982\u7535\u5b50\u8868\u683c\u3001\u6570\u636e\u5e93\u67e5\u8be2\u7ed3\u679c\u6216\u5176\u4ed6\u7ed3\u6784\u5316\u6570\u636e\u3002\u5b83\u4eec\u5728\u5904\u7406\u6570\u636e\u65f6\u63d0\u4f9b\u4e86\u7075\u6d3b\u6027\uff0c\u80fd\u591f\u6709\u6548\u5730\u6267\u884c\u904d\u5386\u3001\u67e5\u627e\u548c\u6570\u636e\u5206\u6790\u7b49\u64cd\u4f5c\u3002\u5bf9\u4e8e\u9700\u8981\u5feb\u901f\u8bbf\u95ee\u548c\u4fee\u6539\u6570\u636e\u7684\u573a\u666f\uff0c\u884c\u5217\u8868\u4e5f\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u884c\u5217\u8868\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5d4c\u5957\u5217\u8868\uff08\u5373\u5217\u8868\u4e2d\u7684\u5217\u8868\uff09\u6765\u8868\u793a\u3002\u884c\u5217\u8868\u662f\u4e00\u79cd\u4e8c\u7ef4\u6570\u636e\u7ed3\u6784\u3001\u6bcf\u4e2a\u5b50\u5217\u8868\u4ee3\u8868 [&hellip;]","protected":false},"author":3,"featured_media":1148040,"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\/1148036"}],"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=1148036"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1148036\/revisions"}],"predecessor-version":[{"id":1148041,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1148036\/revisions\/1148041"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1148040"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1148036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1148036"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1148036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}