{"id":1146773,"date":"2025-01-08T23:19:43","date_gmt":"2025-01-08T15:19:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1146773.html"},"modified":"2025-01-08T23:19:45","modified_gmt":"2025-01-08T15:19:45","slug":"python%e5%a6%82%e4%bd%95%e5%8f%96%e4%b8%80%e4%b8%aa%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9f%a9%e9%98%b5%e4%b8%ad%e7%9a%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1146773.html","title":{"rendered":"python\u5982\u4f55\u53d6\u4e00\u4e2a\u5b57\u7b26\u4e32\u77e9\u9635\u4e2d\u7684"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182659\/e7db0b51-07ab-44ce-aea2-2de059fd8386.webp\" alt=\"python\u5982\u4f55\u53d6\u4e00\u4e2a\u5b57\u7b26\u4e32\u77e9\u9635\u4e2d\u7684\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u53d6\u4e00\u4e2a\u5b57\u7b26\u4e32\u77e9\u9635\u4e2d\u7684\u5143\u7d20<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53d6\u4e00\u4e2a\u5b57\u7b26\u4e32\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u3002<strong>\u4f7f\u7528\u5217\u8868\u7d22\u5f15\u3001\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/strong>\u90fd\u662f\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u83b7\u53d6\u5b57\u7b26\u4e32\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5217\u8868\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u7d22\u5f15\u662fPython\u4e2d\u6700\u57fa\u7840\u7684\u64cd\u4f5c\u4e4b\u4e00\u3002\u5bf9\u4e8e\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\uff08\u5373\u5b57\u7b26\u4e32\u77e9\u9635\uff09\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u7d22\u5f15\u6765\u83b7\u53d6\u5176\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>\u5217\u8868\u7d22\u5f15\u7684\u57fa\u672c\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5217\u8868\u7d22\u5f15\u662f\u901a\u8fc7\u65b9\u62ec\u53f7<code>[]<\/code>\u6765\u5b9e\u73b0\u7684\u3002\u5bf9\u4e8e\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\uff08\u77e9\u9635\uff09\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e24\u4e2a\u7d22\u5f15\uff0c\u7b2c\u4e00\u4e2a\u7d22\u5f15\u8868\u793a\u884c\uff0c\u7b2c\u4e8c\u4e2a\u7d22\u5f15\u8868\u793a\u5217\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u5b57\u7b26\u4e32\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = [<\/p>\n<p>    [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;],<\/p>\n<p>    [&quot;d&quot;, &quot;e&quot;, &quot;f&quot;],<\/p>\n<p>    [&quot;g&quot;, &quot;h&quot;, &quot;i&quot;]<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8981\u83b7\u53d6\u77e9\u9635\u4e2d\u67d0\u4e2a\u5143\u7d20\uff0c\u4f8b\u5982\u7b2c\u4e8c\u884c\u7b2c\u4e09\u5217\u7684\u5143\u7d20\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">element = matrix[1][2]<\/p>\n<p>print(element)  # \u8f93\u51fa: f<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5217\u8868\u7d22\u5f15\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u5d4c\u5957\u7d22\u5f15\uff0cPython\u8fd8\u652f\u6301\u8d1f\u7d22\u5f15\u548c\u5207\u7247\u64cd\u4f5c\u3002\u8d1f\u7d22\u5f15\u7528\u4e8e\u4ece\u5217\u8868\u7684\u672b\u5c3e\u5f00\u59cb\u8ba1\u6570\uff0c\u800c\u5207\u7247\u64cd\u4f5c\u7528\u4e8e\u83b7\u53d6\u5217\u8868\u7684\u5b50\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u83b7\u53d6\u6700\u540e\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">element = matrix[-1][0]<\/p>\n<p>print(element)  # \u8f93\u51fa: g<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u5207\u7247\u83b7\u53d6\u7b2c\u4e00\u884c\u7684\u524d\u4e24\u4e2a\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">elements = matrix[0][:2]<\/p>\n<p>print(elements)  # \u8f93\u51fa: [&#39;a&#39;, &#39;b&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u548c\u4e30\u5bcc\u7684\u6570\u503c\u8fd0\u7b97\u51fd\u6570\u3002\u4f7f\u7528NumPy\u5904\u7406\u5b57\u7b26\u4e32\u77e9\u9635\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u8fdb\u884c\u7d22\u5f15\u548c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>NumPy\u6570\u7ec4\u7684\u57fa\u672c\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5NumPy\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u4e00\u4e2aNumPy\u6570\u7ec4\u5e76\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>matrix = np.array([<\/p>\n<p>    [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;],<\/p>\n<p>    [&quot;d&quot;, &quot;e&quot;, &quot;f&quot;],<\/p>\n<p>    [&quot;g&quot;, &quot;h&quot;, &quot;i&quot;]<\/p>\n<p>])<\/p>\n<h2><strong>\u83b7\u53d6\u7b2c\u4e8c\u884c\u7b2c\u4e09\u5217\u7684\u5143\u7d20<\/strong><\/h2>\n<p>element = matrix[1, 2]<\/p>\n<p>print(element)  # \u8f93\u51fa: f<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>NumPy\u6570\u7ec4\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>NumPy\u6570\u7ec4\u652f\u6301\u591a\u79cd\u9ad8\u7ea7\u64cd\u4f5c\uff0c\u4f8b\u5982\u5e03\u5c14\u7d22\u5f15\u548c\u82b1\u5f0f\u7d22\u5f15\u3002\u5e03\u5c14\u7d22\u5f15\u7528\u4e8e\u6839\u636e\u6761\u4ef6\u7b5b\u9009\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u82b1\u5f0f\u7d22\u5f15\u7528\u4e8e\u901a\u8fc7\u591a\u4e2a\u7d22\u5f15\u6570\u7ec4\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u83b7\u53d6\u77e9\u9635\u4e2d\u6240\u6709\u7b49\u4e8e&quot;f&quot;\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">elements = matrix[matrix == &quot;f&quot;]<\/p>\n<p>print(elements)  # \u8f93\u51fa: [&#39;f&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u82b1\u5f0f\u7d22\u5f15\u83b7\u53d6\u7b2c\u4e00\u884c\u548c\u7b2c\u4e09\u884c\u7684\u6240\u6709\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rows = np.array([0, 2])<\/p>\n<p>elements = matrix[rows, :]<\/p>\n<p>print(elements)<\/p>\n<h2><strong>\u8f93\u51fa:<\/strong><\/h2>\n<h2><strong>[[&#39;a&#39; &#39;b&#39; &#39;c&#39;]<\/strong><\/h2>\n<h2><strong> [&#39;g&#39; &#39;h&#39; &#39;i&#39;]]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\uff08List Comprehension\uff09\u662fPython\u4e2d\u4e00\u79cd\u7b80\u6d01\u9ad8\u6548\u7684\u521b\u5efa\u5217\u8868\u7684\u65b9\u6cd5\u3002\u4f7f\u7528\u5217\u8868\u89e3\u6790\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u4ece\u5b57\u7b26\u4e32\u77e9\u9635\u4e2d\u63d0\u53d6\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>\u5217\u8868\u89e3\u6790\u7684\u57fa\u672c\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">[expression for item in iterable]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u89e3\u6790\u6765\u63d0\u53d6\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u83b7\u53d6\u77e9\u9635\u4e2d\u6240\u6709\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = [<\/p>\n<p>    [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;],<\/p>\n<p>    [&quot;d&quot;, &quot;e&quot;, &quot;f&quot;],<\/p>\n<p>    [&quot;g&quot;, &quot;h&quot;, &quot;i&quot;]<\/p>\n<p>]<\/p>\n<p>elements = [element for row in matrix for element in row]<\/p>\n<p>print(elements)<\/p>\n<h2><strong>\u8f93\u51fa: [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;, &#39;h&#39;, &#39;i&#39;]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5217\u8868\u89e3\u6790\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u8fd8\u652f\u6301\u6761\u4ef6\u8868\u8fbe\u5f0f\u548c\u5d4c\u5957\u89e3\u6790\u3002\u4f8b\u5982\uff0c\u83b7\u53d6\u77e9\u9635\u4e2d\u6240\u6709\u7b49\u4e8e&quot;e&quot;\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">elements = [element for row in matrix for element in row if element == &quot;e&quot;]<\/p>\n<p>print(elements)  # \u8f93\u51fa: [&#39;e&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u89e3\u6790\u83b7\u53d6\u77e9\u9635\u4e2d\u6bcf\u884c\u7684\u524d\u4e24\u4e2a\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">elements = [[element for element in row[:2]] for row in matrix]<\/p>\n<p>print(elements)<\/p>\n<h2><strong>\u8f93\u51fa: [[&#39;a&#39;, &#39;b&#39;], [&#39;d&#39;, &#39;e&#39;], [&#39;g&#39;, &#39;h&#39;]]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5b57\u7b26\u4e32\u77e9\u9635\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u77e9\u9635\u5728\u6570\u636e\u5206\u6790\u3001<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u548c\u81ea\u7136\u8bed\u8a00\u5904\u7406\u7b49\u9886\u57df\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u4e0b\u9762\u4ecb\u7ecd\u51e0\u4e2a\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>\u6570\u636e\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u5b57\u7b26\u4e32\u77e9\u9635\u53ef\u4ee5\u7528\u4e8e\u8868\u793a\u548c\u5904\u7406\u6587\u672c\u6570\u636e\u3002\u4f8b\u5982\uff0c\u5904\u7406\u4e00\u4e2a\u5305\u542b\u6587\u672c\u6570\u636e\u7684CSV\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u5c06\u5176\u52a0\u8f7d\u4e3aDataFrame\uff0c\u7136\u540e\u8fdb\u884c\u5404\u79cd\u5206\u6790\u548c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u52a0\u8f7dCSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&quot;data.csv&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u6307\u5b9a\u5217\u7684\u6240\u6709\u503c<\/strong><\/h2>\n<p>column_values = data[&quot;column_name&quot;].values<\/p>\n<h2><strong>\u5c06\u5217\u503c\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u77e9\u9635<\/strong><\/h2>\n<p>string_matrix = [list(value) for value in column_values]<\/p>\n<h2><strong>\u6253\u5370\u5b57\u7b26\u4e32\u77e9\u9635<\/strong><\/h2>\n<p>print(string_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u673a\u5668\u5b66\u4e60<\/h4>\n<\/p>\n<p><p>\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u5b57\u7b26\u4e32\u77e9\u9635\u53ef\u4ee5\u7528\u4e8e\u8868\u793a\u548c\u5904\u7406\u7279\u5f81\u77e9\u9635\u3002\u4f8b\u5982\uff0c\u5904\u7406\u4e00\u4e2a\u5305\u542b\u6587\u672c\u7279\u5f81\u7684\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u4f7f\u7528sklearn\u5e93\u5c06\u5176\u8f6c\u6362\u4e3a\u7279\u5f81\u77e9\u9635\uff0c\u7136\u540e\u8fdb\u884c\u8bad\u7ec3\u548c\u9884\u6d4b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.feature_extraction.text import CountVectorizer<\/p>\n<h2><strong>\u6587\u672c\u6570\u636e<\/strong><\/h2>\n<p>documents = [&quot;This is the first document.&quot;, &quot;This document is the second document.&quot;, &quot;And this is the third one.&quot;]<\/p>\n<h2><strong>\u521b\u5efaCountVectorizer\u5bf9\u8c61<\/strong><\/h2>\n<p>vectorizer = CountVectorizer()<\/p>\n<h2><strong>\u5c06\u6587\u672c\u6570\u636e\u8f6c\u6362\u4e3a\u7279\u5f81\u77e9\u9635<\/strong><\/h2>\n<p>X = vectorizer.fit_transform(documents)<\/p>\n<h2><strong>\u6253\u5370\u7279\u5f81\u77e9\u9635<\/strong><\/h2>\n<p>print(X.toarray())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u81ea\u7136\u8bed\u8a00\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08NLP\uff09\u4e2d\uff0c\u5b57\u7b26\u4e32\u77e9\u9635\u53ef\u4ee5\u7528\u4e8e\u8868\u793a\u548c\u5904\u7406\u8bcd\u5411\u91cf\u3002\u4f8b\u5982\uff0c\u4f7f\u7528gensim\u5e93\u5c06\u6587\u672c\u6570\u636e\u8f6c\u6362\u4e3a\u8bcd\u5411\u91cf\uff0c\u7136\u540e\u8fdb\u884c\u5404\u79cdNLP\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from gensim.models import Word2Vec<\/p>\n<h2><strong>\u53e5\u5b50\u6570\u636e<\/strong><\/h2>\n<p>sentences = [[&quot;This&quot;, &quot;is&quot;, &quot;the&quot;, &quot;first&quot;, &quot;sentence&quot;], [&quot;This&quot;, &quot;sentence&quot;, &quot;is&quot;, &quot;the&quot;, &quot;second&quot;, &quot;sentence&quot;], [&quot;And&quot;, &quot;this&quot;, &quot;is&quot;, &quot;the&quot;, &quot;third&quot;, &quot;one&quot;]]<\/p>\n<h2><strong>\u521b\u5efaWord2Vec\u6a21\u578b<\/strong><\/h2>\n<p>model = Word2Vec(sentences, vector_size=100, window=5, min_count=1, workers=4)<\/p>\n<h2><strong>\u83b7\u53d6\u8bcd\u5411\u91cf<\/strong><\/h2>\n<p>word_vector = model.wv[&quot;sentence&quot;]<\/p>\n<h2><strong>\u6253\u5370\u8bcd\u5411\u91cf<\/strong><\/h2>\n<p>print(word_vector)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5982\u4f55\u5728Python\u4e2d\u53d6\u4e00\u4e2a\u5b57\u7b26\u4e32\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u3002<strong>\u4f7f\u7528\u5217\u8868\u7d22\u5f15\u3001\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/strong>\u90fd\u662f\u5e38\u89c1\u4e14\u6709\u6548\u7684\u65b9\u6cd5\u3002\u6b64\u5916\uff0c\u5b57\u7b26\u4e32\u77e9\u9635\u5728\u6570\u636e\u5206\u6790\u3001\u673a\u5668\u5b66\u4e60\u548c\u81ea\u7136\u8bed\u8a00\u5904\u7406\u7b49\u9886\u57df\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u548c\u5e94\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u5904\u7406\u548c\u5206\u6790\u6587\u672c\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u521b\u5efa\u5b57\u7b26\u4e32\u77e9\u9635\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u6765\u521b\u5efa\u5b57\u7b26\u4e32\u77e9\u9635\u3002\u6bcf\u4e2a\u5185\u5c42\u5217\u8868\u4ee3\u8868\u77e9\u9635\u7684\u4e00\u884c\uff0c\u884c\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u77e9\u9635\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">matrix = [[&quot;hello&quot;, &quot;world&quot;], [&quot;python&quot;, &quot;rocks&quot;]]\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<code>matrix[0][0]<\/code>\u5c06\u8fd4\u56de&quot;hello&quot;\uff0c<code>matrix[1][1]<\/code>\u5c06\u8fd4\u56de&quot;rocks&quot;\u3002<\/p>\n<p><strong>\u5728\u5b57\u7b26\u4e32\u77e9\u9635\u4e2d\u5982\u4f55\u63d0\u53d6\u7279\u5b9a\u7684\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u8981\u63d0\u53d6\u7279\u5b9a\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u884c\u548c\u5217\u7684\u7d22\u5f15\u3002\u4f8b\u5982\uff0c\u5982\u679c\u9700\u8981\u63d0\u53d6\u7b2c\u4e00\u884c\u7b2c\u4e8c\u5217\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a  <\/p>\n<pre><code class=\"language-python\">specific_string = matrix[0][1]  # \u7ed3\u679c\u4e3a&quot;world&quot;\n<\/code><\/pre>\n<p>\u786e\u4fdd\u7d22\u5f15\u5728\u77e9\u9635\u7684\u8303\u56f4\u5185\uff0c\u4ee5\u907f\u514dIndexError\u3002<\/p>\n<p><strong>\u5982\u4f55\u904d\u5386\u6574\u4e2a\u5b57\u7b26\u4e32\u77e9\u9635\u5e76\u6253\u5370\u6bcf\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u6765\u904d\u5386\u5b57\u7b26\u4e32\u77e9\u9635\u3002\u5916\u5c42\u5faa\u73af\u904d\u5386\u884c\uff0c\u5185\u5c42\u5faa\u73af\u904d\u5386\u5217\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">for row in matrix:\n    for element in row:\n        print(element)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u9010\u4e2a\u6253\u5370\u51fa\u77e9\u9635\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u53d6\u4e00\u4e2a\u5b57\u7b26\u4e32\u77e9\u9635\u4e2d\u7684\u5143\u7d20 \u5728Python\u4e2d\uff0c\u53d6\u4e00\u4e2a\u5b57\u7b26\u4e32\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u3002\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1146778,"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\/1146773"}],"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=1146773"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1146773\/revisions"}],"predecessor-version":[{"id":1146783,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1146773\/revisions\/1146783"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1146778"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1146773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1146773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1146773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}