{"id":980476,"date":"2024-12-27T06:54:48","date_gmt":"2024-12-26T22:54:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/980476.html"},"modified":"2024-12-27T06:54:50","modified_gmt":"2024-12-26T22:54:50","slug":"python%e5%a6%82%e4%bd%95%e5%be%aa%e7%8e%af%e8%b5%8b%e5%80%bc%e8%ae%a1%e7%ae%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/980476.html","title":{"rendered":"python\u5982\u4f55\u5faa\u73af\u8d4b\u503c\u8ba1\u7b97"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205832\/899fd995-ea45-4db8-8454-323aa7972e5e.webp\" alt=\"python\u5982\u4f55\u5faa\u73af\u8d4b\u503c\u8ba1\u7b97\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8fdb\u884c\u5faa\u73af\u8d4b\u503c\u8ba1\u7b97\u53ef\u4ee5\u901a\u8fc7for\u5faa\u73af\u3001while\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001enumerate\u51fd\u6570\u7b49\u65b9\u5f0f\u5b9e\u73b0\u3001\u6bcf\u79cd\u65b9\u5f0f\u90fd\u6709\u5176\u7279\u5b9a\u7684\u5e94\u7528\u573a\u666f\u548c\u4f18\u52bf\u3002<\/strong> \u5176\u4e2d\uff0cfor\u5faa\u73af\u662f\u6700\u5e38\u7528\u7684\u5faa\u73af\u7ed3\u6784\uff0c\u7528\u4e8e\u904d\u5386\u5e8f\u5217\u5e76\u8fdb\u884c\u8d4b\u503c\u548c\u8ba1\u7b97\uff0cwhile\u5faa\u73af\u9002\u7528\u4e8e\u9700\u8981\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u7ee7\u7eed\u6267\u884c\u7684\u573a\u666f\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u5b9e\u73b0\u7b80\u6d01\u7684\u5faa\u73af\u8d4b\u503c\u548c\u8ba1\u7b97\uff0c\u800cenumerate\u51fd\u6570\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u540c\u65f6\u83b7\u53d6\u5143\u7d20\u53ca\u5176\u7d22\u5f15\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5728\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e0bfor\u5faa\u73af\u5728\u5faa\u73af\u8d4b\u503c\u8ba1\u7b97\u4e2d\u7684\u5e94\u7528\u3002for\u5faa\u73af\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u5faa\u73af\u7ed3\u6784\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u904d\u5386\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u7b49\u3002\u5728for\u5faa\u73af\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6216\u76f4\u63a5\u5f15\u7528\u5143\u7d20\u8fdb\u884c\u8d4b\u503c\u548c\u8ba1\u7b97\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528for\u5faa\u73af\u5bf9\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\u8ba1\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>squared_numbers = []<\/p>\n<p>for number in numbers:<\/p>\n<p>    squared = number  2<\/p>\n<p>    squared_numbers.append(squared)<\/p>\n<p>print(squared_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u904d\u5386\u4e86\u4e00\u4e2a\u5217\u8868<code>numbers<\/code>\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\u8ba1\u7b97\uff0c\u7136\u540e\u5c06\u7ed3\u679c\u5b58\u50a8\u5728<code>squared_numbers<\/code>\u5217\u8868\u4e2d\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u5408\u5927\u591a\u6570\u9700\u8981\u5bf9\u5e8f\u5217\u8fdb\u884c\u9010\u4e00\u5904\u7406\u7684\u573a\u666f\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8Python\u4e2d\u5faa\u73af\u8d4b\u503c\u8ba1\u7b97\u7684\u5404\u79cd\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001FOR\u5faa\u73af<\/p>\n<\/p>\n<p><p>for\u5faa\u73af\u662fPython\u4e2d\u6700\u5e38\u89c1\u7684\u5faa\u73af\u7ed3\u6784\u4e4b\u4e00\uff0c\u9002\u7528\u4e8e\u904d\u5386\u53ef\u8fed\u4ee3\u5bf9\u8c61\u5e76\u5bf9\u5176\u8fdb\u884c\u64cd\u4f5c\u3002for\u5faa\u73af\u53ef\u4ee5\u7528\u4e8e\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u3001\u5b57\u5178\u7b49\u6570\u636e\u7ed3\u6784\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecdfor\u5faa\u73af\u5728\u4e0d\u540c\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5217\u8868\u4e2d\u7684\u5e94\u7528<\/li>\n<\/ol>\n<p><p>\u5728\u5217\u8868\u4e2d\uff0cfor\u5faa\u73af\u53ef\u4ee5\u7528\u4e8e\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\uff0c\u4f8b\u5982\u8ba1\u7b97\u3001\u4fee\u6539\u3001\u7b5b\u9009\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528for\u5faa\u73af\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u52a0\u500d\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>doubled_numbers = []<\/p>\n<p>for number in numbers:<\/p>\n<p>    doubled_numbers.append(number * 2)<\/p>\n<p>print(doubled_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cfor\u5faa\u73af\u904d\u5386<code>numbers<\/code>\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5c06\u5176\u52a0\u500d\u540e\u6dfb\u52a0\u5230<code>doubled_numbers<\/code>\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u5b57\u5178\u4e2d\u7684\u5e94\u7528<\/li>\n<\/ol>\n<p><p>\u5728\u5b57\u5178\u4e2d\uff0cfor\u5faa\u73af\u53ef\u4ee5\u7528\u4e8e\u904d\u5386\u952e\u3001\u503c\u6216\u952e\u503c\u5bf9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\uff0c\u5e76\u5bf9\u503c\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">student_scores = {&#39;Alice&#39;: 85, &#39;Bob&#39;: 92, &#39;Charlie&#39;: 78}<\/p>\n<p>adjusted_scores = {}<\/p>\n<p>for student, score in student_scores.items():<\/p>\n<p>    adjusted_scores[student] = score + 5<\/p>\n<p>print(adjusted_scores)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>items()<\/code>\u65b9\u6cd5\u904d\u5386<code>student_scores<\/code>\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u5b66\u751f\u7684\u5206\u6570\u8fdb\u884c\u52a05\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001WHILE\u5faa\u73af<\/p>\n<\/p>\n<p><p>while\u5faa\u73af\u662fPython\u4e2d\u7684\u53e6\u4e00\u79cd\u5faa\u73af\u7ed3\u6784\uff0c\u9002\u7528\u4e8e\u9700\u8981\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u7ee7\u7eed\u6267\u884c\u7684\u573a\u666f\u3002\u5728while\u5faa\u73af\u4e2d\uff0c\u5faa\u73af\u4f1a\u4e00\u76f4\u6267\u884c\uff0c\u76f4\u5230\u6761\u4ef6\u4e0d\u518d\u6ee1\u8db3\u3002<\/p>\n<\/p>\n<ol>\n<li>\u57fa\u672c\u7528\u6cd5<\/li>\n<\/ol>\n<p><p>while\u5faa\u73af\u901a\u5e38\u7528\u4e8e\u5904\u7406\u9700\u8981\u52a8\u6001\u5224\u65ad\u7684\u573a\u666f\uff0c\u4f8b\u5982\u7528\u6237\u8f93\u5165\u3001\u6570\u636e\u6d41\u5904\u7406\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528while\u5faa\u73af\u8ba1\u7b97\u76f4\u5230\u67d0\u4e2a\u6761\u4ef6\u6ee1\u8db3\u7684\u7d2f\u52a0\u548c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">total = 0<\/p>\n<p>number = 1<\/p>\n<p>while number &lt;= 10:<\/p>\n<p>    total += number<\/p>\n<p>    number += 1<\/p>\n<p>print(total)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cwhile\u5faa\u73af\u6301\u7eed\u6267\u884c\uff0c\u76f4\u5230<code>number<\/code>\u5927\u4e8e10\u3002\u5728\u5faa\u73af\u4e2d\uff0c\u6211\u4eec\u4e0d\u65ad\u7d2f\u52a0<code>number<\/code>\u7684\u503c\u5230<code>total<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u65e0\u9650\u5faa\u73af<\/li>\n<\/ol>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff0c\u5e76\u5728\u5185\u90e8\u901a\u8fc7\u6761\u4ef6\u63a7\u5236\u9000\u51fa\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528while\u5faa\u73af\u521b\u5efa\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff0c\u5e76\u901a\u8fc7break\u8bed\u53e5\u9000\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    user_input = input(&quot;Enter a number (or &#39;exit&#39; to quit): &quot;)<\/p>\n<p>    if user_input.lower() == &#39;exit&#39;:<\/p>\n<p>        break<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;You entered: {user_input}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cwhile\u5faa\u73af\u521b\u5efa\u4e86\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u8f93\u5165&#39;exit&#39;\u6765\u9000\u51fa\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u7528\u4e8e\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u751f\u6210\u5217\u8868\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u7b80\u5355\u7684\u5faa\u73af\u8d4b\u503c\u548c\u8ba1\u7b97\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li>\u57fa\u672c\u7528\u6cd5<\/li>\n<\/ol>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u7528\u4e8e\u5bf9\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\uff0c\u5e76\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\u8ba1\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>squared_numbers = [number  2 for number in numbers]<\/p>\n<p>print(squared_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u5b9e\u73b0\u4e86\u5bf9\u6bcf\u4e2a\u5143\u7d20\u5e73\u65b9\u8ba1\u7b97\u7684\u529f\u80fd\uff0c\u4e0efor\u5faa\u73af\u7684\u5b9e\u73b0\u65b9\u5f0f\u76f8\u6bd4\u66f4\u52a0\u7b80\u6d01\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u5e26\u6761\u4ef6\u7684\u5217\u8868\u63a8\u5bfc\u5f0f<\/li>\n<\/ol>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u8fd8\u53ef\u4ee5\u7ed3\u5408\u6761\u4ef6\u8bed\u53e5\uff0c\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5bf9\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>even_numbers = [number for number in numbers if number % 2 == 0]<\/p>\n<p>print(even_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u7b5b\u9009\u51fa<code>numbers<\/code>\u5217\u8868\u4e2d\u6240\u6709\u7684\u5076\u6570\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001ENUMERATE\u51fd\u6570<\/p>\n<\/p>\n<p><p>enumerate\u51fd\u6570\u7528\u4e8e\u5728\u5faa\u73af\u4e2d\u540c\u65f6\u83b7\u53d6\u5143\u7d20\u53ca\u5176\u7d22\u5f15\uff0c\u8fd9\u5728\u9700\u8981\u540c\u65f6\u5904\u7406\u5143\u7d20\u548c\u7d22\u5f15\u7684\u573a\u666f\u4e2d\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\u57fa\u672c\u7528\u6cd5<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528enumerate\u51fd\u6570\u5728\u5faa\u73af\u4e2d\u83b7\u53d6\u5143\u7d20\u53ca\u5176\u7d22\u5f15\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>for index, fruit in enumerate(fruits):<\/p>\n<p>    print(f&quot;Index {index}: {fruit}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cenumerate\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u7d22\u5f15\u548c\u503c\u7684\u5143\u7ec4\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u540c\u65f6\u89e3\u5305\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u6307\u5b9a\u8d77\u59cb\u7d22\u5f15<\/li>\n<\/ol>\n<p><p>enumerate\u51fd\u6570\u8fd8\u5141\u8bb8\u6211\u4eec\u6307\u5b9a\u8d77\u59cb\u7d22\u5f15\uff0c\u8fd9\u5728\u9700\u8981\u81ea\u5b9a\u4e49\u7d22\u5f15\u8d77\u59cb\u503c\u7684\u573a\u666f\u4e2d\u975e\u5e38\u6709\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>for index, fruit in enumerate(fruits, start=1):<\/p>\n<p>    print(f&quot;Index {index}: {fruit}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u8d77\u59cb\u7d22\u5f15\u6307\u5b9a\u4e3a1\uff0c\u800c\u4e0d\u662f\u9ed8\u8ba4\u76840\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5faa\u73af\u5d4c\u5957<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u573a\u666f\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u5faa\u73af\u5d4c\u5957\uff0c\u5373\u5728\u4e00\u4e2a\u5faa\u73af\u5185\u90e8\u5d4c\u5957\u53e6\u4e00\u4e2a\u5faa\u73af\u3002\u8fd9\u901a\u5e38\u7528\u4e8e\u5904\u7406\u591a\u7ef4\u6570\u636e\u7ed3\u6784\u6216\u590d\u6742\u7684\u8ba1\u7b97\u903b\u8f91\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5217\u8868\u5d4c\u5957\u5faa\u73af<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u904d\u5386\u4e8c\u7ef4\u5217\u8868\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\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>flattened = []<\/p>\n<p>for row in matrix:<\/p>\n<p>    for item in row:<\/p>\n<p>        flattened.append(item)<\/p>\n<p>print(flattened)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5916\u5c42\u5faa\u73af\u904d\u5386\u6bcf\u4e00\u884c\uff0c\u5185\u5c42\u5faa\u73af\u904d\u5386\u6bcf\u884c\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u5b57\u5178\u5d4c\u5957\u5faa\u73af<\/li>\n<\/ol>\n<p><p>\u5728\u5904\u7406\u5d4c\u5957\u5b57\u5178\u65f6\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u8fdb\u884c\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_dict = {<\/p>\n<p>    &#39;group1&#39;: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25},<\/p>\n<p>    &#39;group2&#39;: {&#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 30}<\/p>\n<p>}<\/p>\n<p>for group, info in nested_dict.items():<\/p>\n<p>    for key, value in info.items():<\/p>\n<p>        print(f&quot;{group} - {key}: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u904d\u5386\u5d4c\u5957\u5b57\u5178\u4e2d\u7684\u6bcf\u4e2a\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5faa\u73af\u4f18\u5316<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u5faa\u73af\u8d4b\u503c\u8ba1\u7b97\u65f6\uff0c\u4f18\u5316\u5faa\u73af\u7ed3\u6784\u548c\u903b\u8f91\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u4f18\u5316\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li>\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97<\/li>\n<\/ol>\n<p><p>\u5728\u5faa\u73af\u4e2d\uff0c\u5c3d\u91cf\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u3002\u53ef\u4ee5\u5c06\u4e0d\u53d8\u7684\u8ba1\u7b97\u79fb\u51fa\u5faa\u73af\uff0c\u51cf\u5c11\u5faa\u73af\u4f53\u5185\u7684\u5f00\u9500\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u4f18\u5316\u7684\u4ee3\u7801<\/p>\n<p>for i in range(100):<\/p>\n<p>    result = i * 2 + 5<\/p>\n<h2><strong>\u4f18\u5316\u540e\u7684\u4ee3\u7801<\/strong><\/h2>\n<p>offset = 5<\/p>\n<p>for i in range(100):<\/p>\n<p>    result = i * 2 + offset<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784<\/li>\n<\/ol>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u63d0\u9ad8\u5faa\u73af\u7684\u6548\u7387\u3002\u4f8b\u5982\uff0c\u5728\u9700\u8981\u9891\u7e41\u67e5\u627e\u7684\u573a\u666f\u4e2d\uff0c\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u800c\u975e\u5217\u8868\uff08list\uff09\u53ef\u4ee5\u83b7\u5f97\u66f4\u9ad8\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u5c3d\u91cf\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/li>\n<\/ol>\n<p><p>\u5728\u80fd\u591f\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u573a\u666f\u4e2d\uff0c\u5c3d\u91cf\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u66ff\u4ee3\u4f20\u7edf\u7684for\u5faa\u73af\uff0c\u56e0\u4e3a\u5217\u8868\u63a8\u5bfc\u5f0f\u901a\u5e38\u66f4\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u638c\u63e1\u4ee5\u4e0a\u5404\u79cd\u5faa\u73af\u8d4b\u503c\u8ba1\u7b97\u7684\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728Python\u7f16\u7a0b\u4e2d\u7f16\u5199\u51fa\u66f4\u9ad8\u6548\u3001\u53ef\u8bfb\u6027\u66f4\u5f3a\u7684\u4ee3\u7801\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u5faa\u73af\u7ed3\u6784\u548c\u65b9\u6cd5\uff0c\u662f\u7f16\u7a0b\u80fd\u529b\u63d0\u5347\u7684\u91cd\u8981\u4e00\u6b65\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u5faa\u73af\u8fdb\u884c\u8d4b\u503c\u64cd\u4f5c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5faa\u73af\u7ed3\u6784\uff08\u5982for\u5faa\u73af\u548cwhile\u5faa\u73af\uff09\u6765\u5b9e\u73b0\u8d4b\u503c\u64cd\u4f5c\u3002\u901a\u8fc7\u5faa\u73af\uff0c\u53ef\u4ee5\u6839\u636e\u4e00\u5b9a\u7684\u6761\u4ef6\u6216\u8303\u56f4\u9010\u6b65\u4e3a\u53d8\u91cf\u8d4b\u503c\u3002\u4f8b\u5982\uff0c\u5728for\u5faa\u73af\u4e2d\uff0c\u53ef\u4ee5\u904d\u5386\u4e00\u4e2a\u5217\u8868\u6216\u8303\u56f4\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5904\u7406\u548c\u8d4b\u503c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">values = []\nfor i in range(5):\n    values.append(i * 2)  # \u8fd9\u91cc\u5c06i\u7684\u503c\u4e58\u4ee52\u540e\u6dfb\u52a0\u5230values\u5217\u8868\u4e2d\nprint(values)  # \u8f93\u51fa: [0, 2, 4, 6, 8]\n<\/code><\/pre>\n<p><strong>\u5728\u5faa\u73af\u4e2d\u5982\u4f55\u8fdb\u884c\u590d\u6742\u7684\u8ba1\u7b97\u548c\u8d4b\u503c\uff1f<\/strong><br \/>\u9664\u4e86\u7b80\u5355\u7684\u8d4b\u503c\u5916\uff0c\u5faa\u73af\u8fd8\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u8ba1\u7b97\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\u548c\u591a\u91cd\u8d4b\u503c\u3002\u501f\u52a9if\u8bed\u53e5\uff0c\u53ef\u4ee5\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u9009\u62e9\u4e0d\u540c\u7684\u8d4b\u503c\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">results = []\nfor i in range(10):\n    if i % 2 == 0:\n        results.append(i ** 2)  # \u5076\u6570\u7684\u5e73\u65b9\n    else:\n        results.append(i ** 3)  # \u5947\u6570\u7684\u7acb\u65b9\nprint(results)  # \u8f93\u51fa: [0, 1, 4, 27, 16, 125, 36, 343, 64, 729]\n<\/code><\/pre>\n<p><strong>\u5728Python\u5faa\u73af\u4e2d\u5982\u4f55\u907f\u514d\u91cd\u590d\u8ba1\u7b97\uff1f<\/strong><br \/>\u4e3a\u4e86\u63d0\u9ad8\u6548\u7387\uff0c\u907f\u514d\u5728\u5faa\u73af\u4e2d\u8fdb\u884c\u91cd\u590d\u8ba1\u7b97\u662f\u5341\u5206\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u901a\u8fc7\u5c06\u8ba1\u7b97\u7ed3\u679c\u5b58\u50a8\u5728\u5b57\u5178\u6216\u5217\u8868\u4e2d\u6765\u5b9e\u73b0\u7f13\u5b58\uff0c\u4ece\u800c\u5728\u540e\u7eed\u5faa\u73af\u4e2d\u76f4\u63a5\u4f7f\u7528\u5df2\u6709\u7ed3\u679c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u5b57\u5178\u8fdb\u884c\u7f13\u5b58\uff1a  <\/p>\n<pre><code class=\"language-python\">cache = {}\nfor i in range(10):\n    if i not in cache:\n        cache[i] = i * 10  # \u8ba1\u7b97\u5e76\u7f13\u5b58\u7ed3\u679c\n    print(cache[i])  # \u76f4\u63a5\u4f7f\u7528\u7f13\u5b58\u4e2d\u7684\u7ed3\u679c\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8fdb\u884c\u5faa\u73af\u8d4b\u503c\u8ba1\u7b97\u53ef\u4ee5\u901a\u8fc7for\u5faa\u73af\u3001while\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001enumerate\u51fd\u6570\u7b49\u65b9\u5f0f\u5b9e [&hellip;]","protected":false},"author":3,"featured_media":980481,"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\/980476"}],"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=980476"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/980476\/revisions"}],"predecessor-version":[{"id":980484,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/980476\/revisions\/980484"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/980481"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=980476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=980476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=980476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}