{"id":1053062,"date":"2024-12-31T14:30:38","date_gmt":"2024-12-31T06:30:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1053062.html"},"modified":"2024-12-31T14:30:42","modified_gmt":"2024-12-31T06:30:42","slug":"python%e5%a6%82%e4%bd%95%e4%ba%a4%e6%8d%a2%e6%95%b0%e7%bb%84%e4%b8%a4%e8%a1%8c%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1053062.html","title":{"rendered":"Python\u5982\u4f55\u4ea4\u6362\u6570\u7ec4\u4e24\u884c\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/249bba32-3dee-44ec-a4d1-dcd2a906a855.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"Python\u5982\u4f55\u4ea4\u6362\u6570\u7ec4\u4e24\u884c\u503c\" \/><\/p>\n<p><p> <strong>Python\u4ea4\u6362\u6570\u7ec4\u4e24\u884c\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u7d22\u5f15\u3001NumPy\u5e93\u3001\u4ee5\u53caPandas\u5e93\u7b49\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u7d22\u5f15\u6765\u4ea4\u6362\u6570\u7ec4\u7684\u884c\u3001NumPy\u5e93\u7684\u9ad8\u7ea7\u64cd\u4f5c\u3001Pandas\u5e93\u7684DataFrame\u64cd\u4f5c\u3002<\/strong> \u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u5982\u4f55\u4f7f\u7528\u7d22\u5f15\u6765\u4ea4\u6362\u6570\u7ec4\u7684\u884c\u503c\uff1a<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u7d22\u5f15\u4ea4\u6362\u6570\u7ec4\u884c\u503c\uff1a<\/strong> \u901a\u8fc7\u7d22\u5f15\u65b9\u5f0f\uff0c\u76f4\u63a5\u4ea4\u6362\u6570\u7ec4\u4e2d\u4e24\u884c\u7684\u4f4d\u7f6e\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>\u6253\u5370\u4ea4\u6362\u524d\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>print(&quot;\u4ea4\u6362\u524d\u7684\u6570\u7ec4\uff1a&quot;)<\/p>\n<p>print(array)<\/p>\n<h2><strong>\u4ea4\u6362\u7b2c0\u884c\u548c\u7b2c2\u884c<\/strong><\/h2>\n<p>array[[0, 2]] = array[[2, 0]]<\/p>\n<h2><strong>\u6253\u5370\u4ea4\u6362\u540e\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>print(&quot;\u4ea4\u6362\u540e\u7684\u6570\u7ec4\uff1a&quot;)<\/p>\n<p>print(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u7136\u540e\u901a\u8fc7\u7d22\u5f15\u4ea4\u6362\u4e86\u7b2c0\u884c\u548c\u7b2c2\u884c\u7684\u4f4d\u7f6e\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4ed6\u5e38\u89c1\u7684\u65b9\u6cd5\u4ee5\u53ca\u5b83\u4eec\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u7d22\u5f15\u4ea4\u6362\u6570\u7ec4\u884c\u503c<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u7d22\u5f15\u4ea4\u6362\u6570\u7ec4\u884c\u503c\u662f\u4e00\u79cd\u76f4\u63a5\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5c0f\u578b\u6570\u7ec4\u6216\u7b80\u5355\u7684\u4ea4\u6362\u64cd\u4f5c\u3002\u901a\u8fc7\u7d22\u5f15\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u6307\u5b9a\u8981\u4ea4\u6362\u7684\u884c\uff0c\u5e76\u4f7f\u7528Python\u7684\u8d4b\u503c\u64cd\u4f5c\u7b26\u8fdb\u884c\u4ea4\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4<\/p>\n<p>array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<h2><strong>\u6253\u5370\u4ea4\u6362\u524d\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>print(&quot;\u4ea4\u6362\u524d\u7684\u6570\u7ec4\uff1a&quot;)<\/p>\n<p>print(array)<\/p>\n<h2><strong>\u4ea4\u6362\u7b2c1\u884c\u548c\u7b2c2\u884c<\/strong><\/h2>\n<p>array[[1, 2]] = array[[2, 1]]<\/p>\n<h2><strong>\u6253\u5370\u4ea4\u6362\u540e\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>print(&quot;\u4ea4\u6362\u540e\u7684\u6570\u7ec4\uff1a&quot;)<\/p>\n<p>print(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u7528\u4e8e\u5c0f\u578b\u6570\u7ec4\u6216\u9700\u8981\u5feb\u901f\u4ea4\u6362\u884c\u503c\u7684\u573a\u666f\u3002\u5b83\u7684\u4f18\u70b9\u662f\u7b80\u5355\u76f4\u89c2\uff0c\u7f3a\u70b9\u662f\u5bf9\u4e8e\u5927\u578b\u6570\u7ec4\u6216\u590d\u6742\u64cd\u4f5c\uff0c\u53ef\u80fd\u4e0d\u591f\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u9ad8\u7ea7\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u65b9\u6cd5\u3002\u4f7f\u7528NumPy\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u7ec4\u7684\u884c\u503c\u4ea4\u6362\uff0c\u5e76\u4e14\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\u8868\u73b0\u51fa\u8272\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([[10, 20, 30], [40, 50, 60], [70, 80, 90]])<\/p>\n<h2><strong>\u6253\u5370\u4ea4\u6362\u524d\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>print(&quot;\u4ea4\u6362\u524d\u7684\u6570\u7ec4\uff1a&quot;)<\/p>\n<p>print(array)<\/p>\n<h2><strong>\u4ea4\u6362\u7b2c0\u884c\u548c\u7b2c1\u884c<\/strong><\/h2>\n<p>array[[0, 1], :] = array[[1, 0], :]<\/p>\n<h2><strong>\u6253\u5370\u4ea4\u6362\u540e\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>print(&quot;\u4ea4\u6362\u540e\u7684\u6570\u7ec4\uff1a&quot;)<\/p>\n<p>print(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528NumPy\u5e93\u7684\u9ad8\u7ea7\u7d22\u5f15\u529f\u80fd\u6765\u4ea4\u6362\u6570\u7ec4\u7684\u884c\u503c\u3002\u901a\u8fc7\u6307\u5b9a\u884c\u548c\u5217\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u8fdb\u884c\u6570\u7ec4\u64cd\u4f5c\u3002NumPy\u5e93\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u548c\u4e30\u5bcc\u7684\u51fd\u6570\u5e93\uff0c\u9002\u5408\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u548c\u590d\u6742\u7684\u6570\u7ec4\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528Pandas\u5e93\u7684DataFrame\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u63d0\u4f9b\u4e86DataFrame\u6570\u636e\u7ed3\u6784\uff0c\u65b9\u4fbf\u8fdb\u884c\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u3002\u4f7f\u7528Pandas\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u52a0\u76f4\u89c2\u5730\u8fdb\u884c\u6570\u7ec4\u7684\u884c\u503c\u4ea4\u6362\uff0c\u5e76\u4e14\u53ef\u4ee5\u7ed3\u5408\u5176\u4ed6\u6570\u636e\u5206\u6790\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame({<\/p>\n<p>    &#39;A&#39;: [1, 2, 3],<\/p>\n<p>    &#39;B&#39;: [4, 5, 6],<\/p>\n<p>    &#39;C&#39;: [7, 8, 9]<\/p>\n<p>})<\/p>\n<h2><strong>\u6253\u5370\u4ea4\u6362\u524d\u7684DataFrame<\/strong><\/h2>\n<p>print(&quot;\u4ea4\u6362\u524d\u7684DataFrame\uff1a&quot;)<\/p>\n<p>print(df)<\/p>\n<h2><strong>\u4ea4\u6362\u7b2c0\u884c\u548c\u7b2c2\u884c<\/strong><\/h2>\n<p>df.iloc[[0, 2]] = df.iloc[[2, 0]]<\/p>\n<h2><strong>\u6253\u5370\u4ea4\u6362\u540e\u7684DataFrame<\/strong><\/h2>\n<p>print(&quot;\u4ea4\u6362\u540e\u7684DataFrame\uff1a&quot;)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Pandas\u5e93\u521b\u5efa\u4e86\u4e00\u4e2aDataFrame\uff0c\u7136\u540e\u901a\u8fc7iloc\u7d22\u5f15\u5668\u4ea4\u6362\u4e86\u7b2c0\u884c\u548c\u7b2c2\u884c\u7684\u4f4d\u7f6e\u3002Pandas\u5e93\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u5f3a\u5927\u7684\u6570\u636e\u64cd\u4f5c\u80fd\u529b\u548c\u7075\u6d3b\u7684DataFrame\u6570\u636e\u7ed3\u6784\uff0c\u9002\u5408\u8fdb\u884c\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4ea4\u6362\u591a\u884c\u503c\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4ea4\u6362\u6570\u7ec4\u4e2d\u7684\u591a\u884c\u503c\u3002\u53ef\u4ee5\u7ed3\u5408\u524d\u9762\u4ecb\u7ecd\u7684\u65b9\u6cd5\uff0c\u4f7f\u7528\u5faa\u73af\u6216\u5176\u4ed6\u9ad8\u7ea7\u64cd\u4f5c\u6765\u5b9e\u73b0\u8fd9\u4e00\u9700\u6c42\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([[11, 12, 13], [14, 15, 16], [17, 18, 19], [20, 21, 22]])<\/p>\n<h2><strong>\u6253\u5370\u4ea4\u6362\u524d\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>print(&quot;\u4ea4\u6362\u524d\u7684\u6570\u7ec4\uff1a&quot;)<\/p>\n<p>print(array)<\/p>\n<h2><strong>\u5b9a\u4e49\u9700\u8981\u4ea4\u6362\u7684\u884c\u7d22\u5f15<\/strong><\/h2>\n<p>rows_to_swap = [(0, 2), (1, 3)]<\/p>\n<h2><strong>\u4ea4\u6362\u591a\u884c\u503c<\/strong><\/h2>\n<p>for (i, j) in rows_to_swap:<\/p>\n<p>    array[[i, j], :] = array[[j, i], :]<\/p>\n<h2><strong>\u6253\u5370\u4ea4\u6362\u540e\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>print(&quot;\u4ea4\u6362\u540e\u7684\u6570\u7ec4\uff1a&quot;)<\/p>\n<p>print(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u9700\u8981\u4ea4\u6362\u7684\u884c\u7d22\u5f15\u5bf9\uff0c\u7136\u540e\u4f7f\u7528\u5faa\u73af\u7ed3\u5408NumPy\u7684\u9ad8\u7ea7\u7d22\u5f15\u529f\u80fd\uff0c\u4ea4\u6362\u4e86\u6570\u7ec4\u4e2d\u7684\u591a\u884c\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u7528\u4e8e\u9700\u8981\u4ea4\u6362\u591a\u4e2a\u884c\u503c\u7684\u590d\u6742\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u4ea4\u6362\u6570\u7ec4\u884c\u503c<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u590d\u7528\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u4ea4\u6362\u6570\u7ec4\u7684\u884c\u503c\u3002\u8fd9\u6837\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u7ec4\u64cd\u4f5c\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u9879\u76ee\u4e2d\u590d\u7528\u8be5\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def swap_rows(array, row1, row2):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u4ea4\u6362\u6570\u7ec4\u4e2d\u7684\u4e24\u884c\u503c<\/p>\n<p>    \u53c2\u6570\uff1a<\/p>\n<p>    array -- \u9700\u8981\u8fdb\u884c\u64cd\u4f5c\u7684\u6570\u7ec4<\/p>\n<p>    row1, row2 -- \u9700\u8981\u4ea4\u6362\u7684\u884c\u7d22\u5f15<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    array[[row1, row2], :] = array[[row2, row1], :]<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array([[21, 22, 23], [24, 25, 26], [27, 28, 29]])<\/p>\n<h2><strong>\u6253\u5370\u4ea4\u6362\u524d\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>print(&quot;\u4ea4\u6362\u524d\u7684\u6570\u7ec4\uff1a&quot;)<\/p>\n<p>print(array)<\/p>\n<h2><strong>\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u4ea4\u6362\u7b2c1\u884c\u548c\u7b2c2\u884c<\/strong><\/h2>\n<p>swap_rows(array, 1, 2)<\/p>\n<h2><strong>\u6253\u5370\u4ea4\u6362\u540e\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>print(&quot;\u4ea4\u6362\u540e\u7684\u6570\u7ec4\uff1a&quot;)<\/p>\n<p>print(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3aswap_rows\u7684\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u7528\u4e8e\u4ea4\u6362\u6570\u7ec4\u4e2d\u7684\u4e24\u884c\u503c\u3002\u901a\u8fc7\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u7ec4\u64cd\u4f5c\uff0c\u5e76\u4e14\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u590d\u7528\u6027\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3\u4e0e\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u4ea4\u6362\u6570\u7ec4\u4e2d\u7684\u884c\u503c\uff0c\u5e76\u4e14\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u53ca\u5176\u63a8\u8350\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5c0f\u578b\u6570\u7ec4\u6216\u7b80\u5355\u4ea4\u6362\u64cd\u4f5c<\/strong>\uff1a\u4f7f\u7528\u7d22\u5f15\u4ea4\u6362\u884c\u503c\uff0c\u7b80\u5355\u76f4\u89c2\u3002<\/li>\n<li><strong>\u5927\u578b\u6570\u636e\u96c6\u6216\u590d\u6742\u64cd\u4f5c<\/strong>\uff1a\u4f7f\u7528NumPy\u5e93\u7684\u9ad8\u7ea7\u64cd\u4f5c\uff0c\u9ad8\u6548\u4e14\u529f\u80fd\u5f3a\u5927\u3002<\/li>\n<li><strong>\u6570\u636e\u5206\u6790\u548c\u5904\u7406<\/strong>\uff1a\u4f7f\u7528Pandas\u5e93\u7684DataFrame\u64cd\u4f5c\uff0c\u65b9\u4fbf\u7ed3\u5408\u5176\u4ed6\u6570\u636e\u5206\u6790\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u9700\u8981\u4ea4\u6362\u591a\u884c\u503c<\/strong>\uff1a\u7ed3\u5408\u5faa\u73af\u6216\u9ad8\u7ea7\u64cd\u4f5c\uff0c\u7075\u6d3b\u5904\u7406\u590d\u6742\u573a\u666f\u3002<\/li>\n<li><strong>\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\u548c\u590d\u7528\u6027<\/strong>\uff1a\u5b9a\u4e49\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u4fbf\u4e8e\u590d\u7528\u548c\u7ef4\u62a4\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u8fdb\u884c\u6570\u7ec4\u7684\u884c\u503c\u4ea4\u6362\uff0c\u5e76\u4e14\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u5176\u4ed6\u6570\u636e\u64cd\u4f5c\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4ea4\u6362\u6570\u7ec4\u7684\u4e24\u884c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4ea4\u6362\u6570\u7ec4\u7684\u4e24\u884c\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u7d22\u5f15\u5b9e\u73b0\u3002\u5047\u8bbe\u6211\u4eec\u4f7f\u7528NumPy\u5e93\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u6307\u5b9a\u884c\u7684\u7d22\u5f15\u6765\u4ea4\u6362\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>array[[row1, row2]] = array[[row2, row1]]<\/code>\u7684\u8bed\u6cd5\u6765\u5b9e\u73b0\u884c\u7684\u4ea4\u6362\u3002<\/p>\n<p><strong>\u4f7f\u7528\u7eafPython\u5982\u4f55\u4ea4\u6362\u5217\u8868\u7684\u4e24\u884c\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u4e0d\u60f3\u4f7f\u7528NumPy\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u5207\u7247\u6765\u5b9e\u73b0\u3002\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u76f4\u63a5\u8bbf\u95ee\u884c\u3002\u4f8b\u5982\uff0c<code>list_of_lists[row1], list_of_lists[row2] = list_of_lists[row2], list_of_lists[row1]<\/code>\u53ef\u4ee5\u8f7b\u677e\u4ea4\u6362\u6307\u5b9a\u7684\u4e24\u884c\u3002<\/p>\n<p><strong>\u4ea4\u6362\u884c\u540e\u5982\u4f55\u9a8c\u8bc1\u7ed3\u679c\uff1f<\/strong><br \/>\u4ea4\u6362\u884c\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u6253\u5370\u6574\u4e2a\u6570\u7ec4\u6216\u7279\u5b9a\u7684\u884c\u6765\u9a8c\u8bc1\u7ed3\u679c\u3002\u4f7f\u7528<code>print(array)<\/code>\u6216<code>print(array[row_index])<\/code>\u6765\u67e5\u770b\u6570\u7ec4\u7684\u5f53\u524d\u72b6\u6001\uff0c\u786e\u4fdd\u884c\u5df2\u6210\u529f\u4ea4\u6362\u3002\u7ed3\u5408\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5\uff0c\u53ef\u4ee5\u66f4\u6df1\u5165\u5730\u68c0\u67e5\u6570\u7ec4\u7684\u7ed3\u6784\u548c\u5185\u5bb9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4ea4\u6362\u6570\u7ec4\u4e24\u884c\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u7d22\u5f15\u3001NumPy\u5e93\u3001\u4ee5\u53caPandas\u5e93\u7b49\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u7d22 [&hellip;]","protected":false},"author":3,"featured_media":1053069,"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\/1053062"}],"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=1053062"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1053062\/revisions"}],"predecessor-version":[{"id":1053070,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1053062\/revisions\/1053070"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1053069"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1053062"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1053062"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1053062"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}