{"id":959426,"date":"2024-12-27T03:40:32","date_gmt":"2024-12-26T19:40:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/959426.html"},"modified":"2024-12-27T03:40:34","modified_gmt":"2024-12-26T19:40:34","slug":"python-%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e5%8f%8c%e5%be%aa%e7%8e%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/959426.html","title":{"rendered":"python \u5982\u4f55\u5b9e\u73b0\u53cc\u5faa\u73af"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102230\/410f8b64-bf8f-40ab-8488-639143569aee.webp\" alt=\"python \u5982\u4f55\u5b9e\u73b0\u53cc\u5faa\u73af\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u5b9e\u73b0\u53cc\u5faa\u73af\u7684\u5e38\u89c1\u65b9\u6cd5\u662f\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\uff0c\u8fd9\u901a\u5e38\u7528\u4e8e\u5904\u7406\u4e8c\u7ef4\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5217\u8868\u5217\u8868\u3001\u77e9\u9635\u7b49\u3002<strong>Python\u5b9e\u73b0\u53cc\u5faa\u73af\u7684\u5e38\u89c1\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5d4c\u5957for\u5faa\u73af\u3001while\u5faa\u73af\u7ed3\u5408for\u5faa\u73af\u3001\u4ee5\u53ca\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u8fdb\u884c\u53cc\u5c42\u5faa\u73af\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u5d4c\u5957for\u5faa\u73af\uff0c\u8fd9\u662f\u7531\u4e8e\u5176\u7b80\u5355\u76f4\u63a5\u4e14\u6613\u4e8e\u7406\u89e3\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u5d4c\u5957for\u5faa\u73af\u6765\u904d\u5386\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u5d4c\u5957for\u5faa\u73af\u662f\u6700\u76f4\u89c2\u7684\u53cc\u5faa\u73af\u5b9e\u73b0\u65b9\u5f0f\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\uff08\u5217\u8868\u7684\u5217\u8868\uff09\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5916\u5c42\u5faa\u73af\u904d\u5386\u5217\u8868\u7684\u6bcf\u4e00\u4e2a\u5b50\u5217\u8868\uff0c\u7136\u540e\u901a\u8fc7\u5185\u5c42\u5faa\u73af\u904d\u5386\u5b50\u5217\u8868\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002\u8fd9\u6837\u7684\u5d4c\u5957for\u5faa\u73af\u7ed3\u6784\u4f7f\u5f97\u6211\u4eec\u80fd\u591f\u8f7b\u677e\u5730\u8bbf\u95ee\u548c\u64cd\u4f5c\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u8003\u8651\u4e00\u4e2a\u7b80\u5355\u7684\u4e8c\u7ef4\u5217\u8868<code>matrix<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\u53cc\u5faa\u73af\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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>for row in matrix:<\/p>\n<p>    for element in row:<\/p>\n<p>        print(element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5916\u5c42\u5faa\u73af<code>for row in matrix<\/code>\u904d\u5386\u6bcf\u4e00\u4e2a\u5b50\u5217\u8868\uff0c\u5185\u5c42\u5faa\u73af<code>for element in row<\/code>\u904d\u5386\u5b50\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u5c06\u5176\u6253\u5370\u51fa\u6765\u3002\u8fd9\u6837\u7684\u53cc\u5faa\u73af\u7ed3\u6784\u5728\u6570\u636e\u5904\u7406\u3001\u77e9\u9635\u8fd0\u7b97\u3001\u56fe\u50cf\u5904\u7406\u7b49\u573a\u666f\u4e2d\u5341\u5206\u5e38\u89c1\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5d4c\u5957FOR\u5faa\u73af<\/p>\n<\/p>\n<p><p>\u5d4c\u5957for\u5faa\u73af\u662fPython\u4e2d\u5b9e\u73b0\u53cc\u5faa\u73af\u7684\u57fa\u7840\u65b9\u5f0f\uff0c\u901a\u8fc7\u5916\u5c42\u548c\u5185\u5c42\u7684\u5faa\u73af\u7ed3\u6784\uff0c\u53ef\u4ee5\u904d\u5386\u548c\u5904\u7406\u591a\u7ef4\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u57fa\u672c\u7ed3\u6784<\/strong><\/li>\n<\/ol>\n<p><p>\u5d4c\u5957for\u5faa\u73af\u7684\u57fa\u672c\u7ed3\u6784\u662f\uff0c\u5728\u5916\u5c42\u5faa\u73af\u4e2d\u904d\u5386\u4e3b\u5217\u8868\u6216\u4e3b\u96c6\u5408\uff0c\u7136\u540e\u5728\u5185\u5c42\u5faa\u73af\u4e2d\u904d\u5386\u6bcf\u4e00\u4e2a\u5b50\u5217\u8868\u6216\u5b50\u96c6\u5408\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8bbf\u95ee\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [[10, 20], [30, 40], [50, 60]]<\/p>\n<p>for sublist in data:<\/p>\n<p>    for item in sublist:<\/p>\n<p>        print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5916\u5c42\u5faa\u73af\u904d\u5386\u4e86<code>data<\/code>\u7684\u6bcf\u4e2a\u5b50\u5217\u8868\uff0c\u800c\u5185\u5c42\u5faa\u73af\u5219\u8bbf\u95ee\u4e86\u6bcf\u4e2a\u5b50\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u6700\u7ec8\u7684\u8f93\u51fa\u662f<code>10, 20, 30, 40, 50, 60<\/code>\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4e8c\u7ef4\u5217\u8868\u7684\u904d\u5386<\/strong><\/li>\n<\/ol>\n<p><p>\u5f53\u5904\u7406\u4e8c\u7ef4\u5217\u8868\uff08\u5982\u77e9\u9635\uff09\u65f6\uff0c\u5d4c\u5957for\u5faa\u73af\u7279\u522b\u6709\u7528\u3002\u5728\u77e9\u9635\u8fd0\u7b97\u3001\u56fe\u50cf\u5904\u7406\u7b49\u573a\u666f\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u904d\u5386\u6bcf\u4e00\u884c\u548c\u6bcf\u4e00\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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>for i in range(len(matrix)):<\/p>\n<p>    for j in range(len(matrix[i])):<\/p>\n<p>        print(f&quot;Element at ({i},{j}) is {matrix[i][j]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>i<\/code>\u548c<code>j<\/code>\u5206\u522b\u8868\u793a\u884c\u548c\u5217\u7684\u7d22\u5f15\uff0c\u901a\u8fc7<code>matrix[i][j]<\/code>\u53ef\u4ee5\u8bbf\u95ee\u5230\u7279\u5b9a\u4f4d\u7f6e\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001WHILE\u5faa\u73af\u7ed3\u5408FOR\u5faa\u73af<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u7ed3\u5408<code>for<\/code>\u5faa\u73af\u53ef\u4ee5\u63d0\u4f9b\u66f4\u5927\u7684\u7075\u6d3b\u6027\uff0c\u6bd4\u5982\u9700\u8981\u52a8\u6001\u63a7\u5236\u5faa\u73af\u7684\u6761\u4ef6\u6216\u8005\u5728\u67d0\u4e9b\u7279\u5b9a\u7684\u60c5\u51b5\u4e0b\u9000\u51fa\u5faa\u73af\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u57fa\u672c\u7ed3\u6784<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7<code>while<\/code>\u5faa\u73af\u53ef\u4ee5\u63a7\u5236\u5916\u5c42\u5faa\u73af\u7684\u6267\u884c\u6761\u4ef6\uff0c\u800c<code>for<\/code>\u5faa\u73af\u5219\u53ef\u4ee5\u7528\u4e8e\u904d\u5386\u5185\u90e8\u7684\u5217\u8868\u6216\u96c6\u5408\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p>i = 0<\/p>\n<p>while i &lt; len(data):<\/p>\n<p>    for item in data[i]:<\/p>\n<p>        print(item)<\/p>\n<p>    i += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7ed3\u6784\u4e2d\uff0c<code>while<\/code>\u5faa\u73af\u63a7\u5236\u5916\u5c42\u7684\u904d\u5386\uff0c\u800c\u5185\u5c42\u7684<code>for<\/code>\u5faa\u73af\u5219\u904d\u5386\u6bcf\u4e2a\u5b50\u5217\u8868\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u52a8\u6001\u63a7\u5236\u5faa\u73af<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528<code>while<\/code>\u7ed3\u5408<code>for<\/code>\u53ef\u4ee5\u5728\u5904\u7406\u9700\u8981\u52a8\u6001\u6539\u53d8\u6761\u4ef6\u7684\u573a\u666f\u4e2d\u4f7f\u7528\uff0c\u6bd4\u5982\u5728\u6570\u636e\u5904\u7406\u8fc7\u7a0b\u4e2d\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u63d0\u524d\u9000\u51fa\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p>i = 0<\/p>\n<p>found = False<\/p>\n<p>while i &lt; len(data) and not found:<\/p>\n<p>    for item in data[i]:<\/p>\n<p>        if item == 5:<\/p>\n<p>            print(&quot;Found 5&quot;)<\/p>\n<p>            found = True<\/p>\n<p>            break<\/p>\n<p>    i += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f53\u627e\u5230\u5143\u7d20<code>5<\/code>\u65f6\uff0c<code>found<\/code>\u6807\u8bb0\u4e3a<code>True<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u63d0\u524d\u9000\u51fa\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8fdb\u884c\u53cc\u5c42\u5faa\u73af<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u548cPythonic\u7684\u65b9\u5f0f\u6765\u5904\u7406\u5faa\u73af\u548c\u751f\u6210\u65b0\u7684\u5217\u8868\u3002\u5728\u5904\u7406\u9700\u8981\u53cc\u5faa\u73af\u7684\u573a\u666f\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u63d0\u4f9b\u66f4\u4e3a\u7b80\u6d01\u7684\u4ee3\u7801\u7ed3\u6784\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u57fa\u672c\u7ed3\u6784<\/strong><\/li>\n<\/ol>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u5141\u8bb8\u5728\u4e00\u884c\u5185\u5b9e\u73b0\u5d4c\u5957\u5faa\u73af\uff0c\u5e76\u751f\u6210\u65b0\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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>flattened = [item for sublist in matrix for item in sublist]<\/p>\n<p>print(flattened)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>flattened<\/code>\u662f\u4e00\u4e2a\u4e00\u7ef4\u5217\u8868\uff0c\u5305\u542b\u4e86<code>matrix<\/code>\u4e2d\u6240\u6709\u7684\u5143\u7d20\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u4e8c\u7ef4\u5217\u8868\u8f6c\u6362\u4e3a\u4e00\u7ef4\u5217\u8868\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6761\u4ef6\u7b5b\u9009<\/strong><\/li>\n<\/ol>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u5faa\u73af\uff0c\u8fd8\u53ef\u4ee5\u7ed3\u5408\u6761\u4ef6\u7b5b\u9009\u51fa\u7b26\u5408\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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>even_numbers = [item for sublist in matrix for item in sublist if item % 2 == 0]<\/p>\n<p>print(even_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>even_numbers<\/code>\u5217\u8868\u53ea\u5305\u542b\u4e86<code>matrix<\/code>\u4e2d\u6240\u6709\u7684\u5076\u6570\u5143\u7d20\u3002\u901a\u8fc7\u6dfb\u52a0\u6761\u4ef6\u5224\u65ad\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u7b5b\u9009\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5e94\u7528\u573a\u666f\u4e0e\u9ad8\u7ea7\u6280\u5de7<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53cc\u5faa\u73af\u7684\u4f7f\u7528\u573a\u666f\u975e\u5e38\u5e7f\u6cdb\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\u6570\u636e\u5904\u7406\u3001\u56fe\u50cf\u5904\u7406\u3001\u77e9\u9635\u8fd0\u7b97\u7b49\u3002\u5728\u8fd9\u4e9b\u573a\u666f\u4e2d\uff0c\u638c\u63e1\u53cc\u5faa\u73af\u7684\u4f7f\u7528\u6280\u5de7\u662f\u975e\u5e38\u6709\u5e2e\u52a9\u7684\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6570\u636e\u5904\u7406<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4e2d\uff0c\u53cc\u5faa\u73af\u5e38\u7528\u4e8e\u904d\u5386\u548c\u5904\u7406\u591a\u7ef4\u6570\u636e\u96c6\u3002\u6bd4\u5982\u5728\u5904\u7406CSV\u6587\u4ef6\u7684\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u53cc\u5faa\u73af\u904d\u5386\u6bcf\u4e00\u884c\u548c\u6bcf\u4e00\u5217\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>with open(&#39;data.csv&#39;, newline=&#39;&#39;) as csvfile:<\/p>\n<p>    datareader = csv.reader(csvfile)<\/p>\n<p>    for row in datareader:<\/p>\n<p>        for value in row:<\/p>\n<p>            print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u56fe\u50cf\u5904\u7406<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u9886\u57df\uff0c\u56fe\u50cf\u901a\u5e38\u88ab\u8868\u793a\u4e3a\u4e8c\u7ef4\u77e9\u9635\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u4ee3\u8868\u4e00\u4e2a\u50cf\u7d20\u7684\u503c\u3002\u53cc\u5faa\u73af\u53ef\u4ee5\u7528\u4e8e\u904d\u5386\u548c\u5904\u7406\u6bcf\u4e00\u4e2a\u50cf\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>image = Image.open(&#39;example.png&#39;)<\/p>\n<p>pixels = image.load()<\/p>\n<p>for i in range(image.size[0]):  # Width<\/p>\n<p>    for j in range(image.size[1]):  # Height<\/p>\n<p>        pixel = pixels[i, j]<\/p>\n<p>        # \u5904\u7406\u50cf\u7d20\u503c\uff0c\u4f8b\u5982\u8fdb\u884c\u7070\u5ea6\u8f6c\u6362<\/p>\n<p>        gray = int(sum(pixel) \/ 3)<\/p>\n<p>        pixels[i, j] = (gray, gray, gray)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u53cc\u5faa\u73af\u904d\u5386\u56fe\u50cf\u7684\u6bcf\u4e00\u4e2a\u50cf\u7d20\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u7070\u5ea6\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u77e9\u9635\u8fd0\u7b97<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u77e9\u9635\u8fd0\u7b97\u4e2d\uff0c\u53cc\u5faa\u73af\u662f\u5b9e\u73b0\u77e9\u9635\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u7b49\u8fd0\u7b97\u7684\u57fa\u7840\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix1 = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6]<\/p>\n<p>]<\/p>\n<p>matrix2 = [<\/p>\n<p>    [7, 8, 9],<\/p>\n<p>    [10, 11, 12]<\/p>\n<p>]<\/p>\n<p>result = [[0, 0, 0], [0, 0, 0]]<\/p>\n<p>for i in range(len(matrix1)):<\/p>\n<p>    for j in range(len(matrix1[0])):<\/p>\n<p>        result[i][j] = matrix1[i][j] + matrix2[i][j]<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u53cc\u5faa\u73af\u8fdb\u884c\u77e9\u9635\u7684\u52a0\u6cd5\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u6027\u80fd\u4f18\u5316\u4e0e\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u53cc\u5faa\u73af\u8fdb\u884c\u590d\u6742\u8fd0\u7b97\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6027\u80fd\u74f6\u9888\uff0c\u56e0\u6b64\u5728\u7f16\u5199\u4ee3\u7801\u65f6\u9700\u8981\u6ce8\u610f\u4e00\u4e9b\u4f18\u5316\u6280\u5de7\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5d4c\u5957<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u7f16\u5199\u53cc\u5faa\u73af\u65f6\uff0c\u5c3d\u91cf\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5d4c\u5957\uff0c\u4ee5\u51cf\u5c11\u5faa\u73af\u7684\u590d\u6742\u5ea6\u3002\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\uff0c\u51cf\u5c11\u5faa\u73af\u7684\u5c42\u6b21\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5229\u7528NumPy\u7b49\u5e93<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u5904\u7406\u77e9\u9635\u548c\u6570\u7ec4\u65f6\uff0cPython\u7684NumPy\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u8fd0\u7b97\u529f\u80fd\uff0c\u53ef\u4ee5\u66ff\u4ee3\u53cc\u5faa\u73af\u8fdb\u884c\u64cd\u4f5c\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>matrix1 = np.array([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6]<\/p>\n<p>])<\/p>\n<p>matrix2 = np.array([<\/p>\n<p>    [7, 8, 9],<\/p>\n<p>    [10, 11, 12]<\/p>\n<p>])<\/p>\n<p>result = matrix1 + matrix2<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u4f7f\u7528\u751f\u6210\u5668<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u751f\u6210\u5668\u66ff\u4ee3\u5217\u8868\u53ef\u4ee5\u8282\u7701\u5185\u5b58\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def flatten(matrix):<\/p>\n<p>    for sublist in matrix:<\/p>\n<p>        for item in sublist:<\/p>\n<p>            yield item<\/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>for value in flatten(matrix):<\/p>\n<p>    print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3\u6765\u8bf4\uff0cPython\u4e2d\u5b9e\u73b0\u53cc\u5faa\u73af\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u6700\u5e38\u89c1\u7684\u662f\u5d4c\u5957for\u5faa\u73af\u3002\u6b64\u5916\uff0c\u901a\u8fc7\u7ed3\u5408while\u5faa\u73af\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u4ee5\u53ca\u5229\u7528NumPy\u7b49\u5e93\uff0c\u53ef\u4ee5\u5728\u4e0d\u540c\u573a\u666f\u4e2d\u5b9e\u73b0\u9ad8\u6548\u7684\u53cc\u5faa\u73af\u64cd\u4f5c\u3002\u7406\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\u5bf9\u4e8e\u63d0\u5347Python\u7f16\u7a0b\u80fd\u529b\u548c\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u53cc\u5faa\u73af\u904d\u5386\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53cc\u5faa\u73af\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u5faa\u73af\u7684\u65b9\u5f0f\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\uff08\u5217\u8868\u7684\u5217\u8868\uff09\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e24\u4e2afor\u5faa\u73af\u6765\u904d\u5386\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\nfor row in matrix:\n    for element in row:\n        print(element)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u5c06\u9010\u884c\u6253\u5370\u51fa\u4e8c\u7ef4\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<p><strong>\u53cc\u5faa\u73af\u5982\u4f55\u5728Python\u4e2d\u7528\u4e8e\u751f\u6210\u7ec4\u5408\uff1f<\/strong><br \/>\u53cc\u5faa\u73af\u53ef\u4ee5\u7528\u6765\u751f\u6210\u591a\u4e2a\u5217\u8868\u7684\u7ec4\u5408\u3002\u5047\u8bbe\u4f60\u6709\u4e24\u4e2a\u5217\u8868\uff0c\u60f3\u8981\u83b7\u53d6\u6240\u6709\u53ef\u80fd\u7684\u914d\u5bf9\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">list1 = [&#39;A&#39;, &#39;B&#39;]\nlist2 = [1, 2]\nfor item1 in list1:\n    for item2 in list2:\n        print((item1, item2))\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u5c06\u8f93\u51fa\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\uff0c\u5982(&#39;A&#39;, 1)\u3001(&#39;A&#39;, 2)\u3001(&#39;B&#39;, 1)\u548c(&#39;B&#39;, 2)\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528\u53cc\u5faa\u73af\u65f6\uff0c\u5982\u4f55\u63d0\u9ad8\u6548\u7387\uff1f<\/strong><br \/>\u53cc\u5faa\u73af\u5728\u5904\u7406\u5927\u6570\u636e\u65f6\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u3002\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u6765\u4f18\u5316\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u5c06\u53cc\u5faa\u73af\u7684\u7ed3\u679c\u7b80\u5316\u4e3a\u4e00\u884c\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">combinations = [(item1, item2) for item1 in list1 for item2 in list2]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u6267\u884c\u901f\u5ea6\u66f4\u5feb\uff0c\u9002\u5408\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5b9e\u73b0\u53cc\u5faa\u73af\u7684\u5e38\u89c1\u65b9\u6cd5\u662f\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\uff0c\u8fd9\u901a\u5e38\u7528\u4e8e\u5904\u7406\u4e8c\u7ef4\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5217\u8868\u5217\u8868\u3001\u77e9\u9635\u7b49\u3002Pyth [&hellip;]","protected":false},"author":3,"featured_media":959432,"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\/959426"}],"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=959426"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/959426\/revisions"}],"predecessor-version":[{"id":959434,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/959426\/revisions\/959434"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/959432"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=959426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=959426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=959426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}