{"id":1115151,"date":"2025-01-08T18:04:04","date_gmt":"2025-01-08T10:04:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1115151.html"},"modified":"2025-01-08T18:04:06","modified_gmt":"2025-01-08T10:04:06","slug":"python%e5%a6%82%e4%bd%95%e4%ba%a4%e6%8d%a2%e4%b8%a4%e4%b8%aa%e5%8f%98%e9%87%8f%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1115151.html","title":{"rendered":"python\u5982\u4f55\u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080029\/ea046b70-be47-48e3-bcf1-31ce3d4a590d.webp\" alt=\"python\u5982\u4f55\u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\u503c\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u4e34\u65f6\u53d8\u91cf\u3001Python\u7684\u591a\u5143\u8d4b\u503c\u8bed\u6cd5\u3001\u52a0\u51cf\u6cd5\u6216\u5f02\u6216\u8fd0\u7b97\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528Python\u7684\u591a\u5143\u8d4b\u503c\u8bed\u6cd5\u662f\u6700\u7b80\u6d01\u548c\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u4e34\u65f6\u53d8\u91cf<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u4e34\u65f6\u53d8\u91cf\u662f\u6700\u76f4\u89c2\u548c\u57fa\u7840\u7684\u65b9\u6cd5\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 5<\/p>\n<p>b = 10<\/p>\n<h2><strong>\u4f7f\u7528\u4e34\u65f6\u53d8\u91cf<\/strong><\/h2>\n<p>temp = a<\/p>\n<p>a = b<\/p>\n<p>b = temp<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u9996\u5148\u4f7f\u7528\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf<code>temp<\/code>\u6765\u5b58\u50a8\u53d8\u91cf<code>a<\/code>\u7684\u503c\uff0c\u7136\u540e\u5c06\u53d8\u91cf<code>b<\/code>\u7684\u503c\u8d4b\u7ed9\u53d8\u91cf<code>a<\/code>\uff0c\u6700\u540e\u5c06\u4e34\u65f6\u53d8\u91cf<code>temp<\/code>\u7684\u503c\u8d4b\u7ed9\u53d8\u91cf<code>b<\/code>\u3002\u8fd9\u6837\u5c31\u5b8c\u6210\u4e86\u53d8\u91cf<code>a<\/code>\u548c<code>b<\/code>\u7684\u503c\u7684\u4ea4\u6362\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528Python\u591a\u5143\u8d4b\u503c\u8bed\u6cd5<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u6d01\u7684\u8bed\u6cd5\u6765\u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\uff0c\u4e0d\u9700\u8981\u4f7f\u7528\u4e34\u65f6\u53d8\u91cf\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u6267\u884c\u6548\u7387\u8f83\u9ad8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 5<\/p>\n<p>b = 10<\/p>\n<h2><strong>\u4f7f\u7528\u591a\u5143\u8d4b\u503c\u8bed\u6cd5<\/strong><\/h2>\n<p>a, b = b, a<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u5229\u7528\u4e86Python\u7684\u591a\u5143\u8d4b\u503c\u8bed\u6cd5\uff0c\u76f4\u63a5\u5c06\u53d8\u91cf<code>a<\/code>\u548c<code>b<\/code>\u7684\u503c\u8fdb\u884c\u4ea4\u6362\u3002\u8fd9\u79cd\u65b9\u6cd5\u662fPython\u7279\u6709\u7684\uff0c\u975e\u5e38\u7b80\u6d01\u9ad8\u6548\uff0c\u63a8\u8350\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u52a0\u51cf\u6cd5<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u52a0\u51cf\u6cd5\u6765\u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\u662f\u4e00\u79cd\u5de7\u5999\u7684\u65b9\u6cd5\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u6574\u6570\u6ea2\u51fa\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 5<\/p>\n<p>b = 10<\/p>\n<h2><strong>\u4f7f\u7528\u52a0\u51cf\u6cd5<\/strong><\/h2>\n<p>a = a + b<\/p>\n<p>b = a - b<\/p>\n<p>a = a - b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u9996\u5148\u5c06<code>a<\/code>\u548c<code>b<\/code>\u7684\u548c\u8d4b\u7ed9\u53d8\u91cf<code>a<\/code>\uff0c\u7136\u540e\u901a\u8fc7\u51cf\u53bb\u53d8\u91cf<code>b<\/code>\u5f97\u5230\u539f\u672c\u53d8\u91cf<code>a<\/code>\u7684\u503c\uff0c\u6700\u540e\u518d\u901a\u8fc7\u51cf\u53bb\u53d8\u91cf<code>b<\/code>\u5f97\u5230\u539f\u672c\u53d8\u91cf<code>b<\/code>\u7684\u503c\u3002\u8fd9\u6837\u5c31\u5b8c\u6210\u4e86\u53d8\u91cf<code>a<\/code>\u548c<code>b<\/code>\u7684\u503c\u7684\u4ea4\u6362\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u4f4d\u64cd\u4f5c\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u6574\u6570\u7c7b\u578b\u7684\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 5<\/p>\n<p>b = 10<\/p>\n<h2><strong>\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97<\/strong><\/h2>\n<p>a = a ^ b<\/p>\n<p>b = a ^ b<\/p>\n<p>a = a ^ b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u9996\u5148\u5c06<code>a<\/code>\u548c<code>b<\/code>\u8fdb\u884c\u5f02\u6216\u8fd0\u7b97\uff0c\u5e76\u5c06\u7ed3\u679c\u8d4b\u7ed9\u53d8\u91cf<code>a<\/code>\uff0c\u7136\u540e\u901a\u8fc7\u5f02\u6216\u8fd0\u7b97\u5f97\u5230\u539f\u672c\u53d8\u91cf<code>a<\/code>\u7684\u503c\uff0c\u6700\u540e\u518d\u901a\u8fc7\u5f02\u6216\u8fd0\u7b97\u5f97\u5230\u539f\u672c\u53d8\u91cf<code>b<\/code>\u7684\u503c\u3002\u8fd9\u6837\u5c31\u5b8c\u6210\u4e86\u53d8\u91cf<code>a<\/code>\u548c<code>b<\/code>\u7684\u503c\u7684\u4ea4\u6362\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528Python\u5185\u7f6e\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570\u6765\u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sorted<\/code>\u51fd\u6570\u6765\u5bf9\u4e24\u4e2a\u6570\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u91cd\u65b0\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 10<\/p>\n<p>b = 5<\/p>\n<h2><strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<\/strong><\/h2>\n<p>a, b = sorted([a, b])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u9996\u5148\u5c06\u4e24\u4e2a\u53d8\u91cf\u653e\u5165\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u7136\u540e\u4f7f\u7528<code>sorted<\/code>\u51fd\u6570\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff0c\u6700\u540e\u5c06\u6392\u5e8f\u540e\u7684\u5217\u8868\u91cd\u65b0\u8d4b\u503c\u7ed9\u53d8\u91cf<code>a<\/code>\u548c<code>b<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u6392\u5e8f\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u7279\u5b9a\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u6765\u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u5e93\u6765\u4ea4\u6362\u4e24\u4e2a\u6570\u7ec4\u5143\u7d20\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>a = np.array([1, 2, 3])<\/p>\n<p>b = np.array([4, 5, 6])<\/p>\n<h2><strong>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/strong><\/h2>\n<p>a, b = b, a<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528<code>numpy<\/code>\u5e93\u6765\u521b\u5efa\u4e24\u4e2a\u6570\u7ec4\uff0c\u7136\u540e\u76f4\u63a5\u4f7f\u7528\u591a\u5143\u8d4b\u503c\u8bed\u6cd5\u6765\u4ea4\u6362\u6570\u7ec4\u7684\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5904\u7406\u6570\u7ec4\u6216\u77e9\u9635\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u4f7f\u7528\u51fd\u6570\u4ea4\u6362\u53d8\u91cf\u503c<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u5c06\u4ea4\u6362\u53d8\u91cf\u503c\u7684\u64cd\u4f5c\u5c01\u88c5\u5230\u4e00\u4e2a\u51fd\u6570\u4e2d\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u590d\u7528\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def swap(a, b):<\/p>\n<p>    return b, a<\/p>\n<p>a = 5<\/p>\n<p>b = 10<\/p>\n<h2><strong>\u4f7f\u7528\u51fd\u6570<\/strong><\/h2>\n<p>a, b = swap(a, b)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a<code>swap<\/code>\u51fd\u6570\uff0c\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\u5e76\u8fd4\u56de\u4ea4\u6362\u540e\u7684\u503c\u3002\u7136\u540e\u901a\u8fc7\u8c03\u7528\u51fd\u6570\u6765\u4ea4\u6362\u53d8\u91cf\u7684\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u591a\u6b21\u4ea4\u6362\u53d8\u91cf\u503c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\u503c\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u3002<strong>\u4f7f\u7528\u4e34\u65f6\u53d8\u91cf\u3001Python\u591a\u5143\u8d4b\u503c\u8bed\u6cd5\u3001\u52a0\u51cf\u6cd5\u548c\u5f02\u6216\u8fd0\u7b97\u7b49\u65b9\u6cd5\u662f\u6700\u5e38\u89c1\u7684<\/strong>\uff0c\u5176\u4e2dPython\u591a\u5143\u8d4b\u503c\u8bed\u6cd5\u662f\u6700\u7b80\u6d01\u548c\u9ad8\u6548\u7684\u3002\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u5143\u7ec4\u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u5229\u7528\u5143\u7ec4\u7684\u7279\u6027\u6765\u8f7b\u677e\u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\u3002\u5177\u4f53\u65b9\u6cd5\u662f\u5c06\u4e24\u4e2a\u53d8\u91cf\u653e\u5165\u4e00\u4e2a\u5143\u7ec4\u4e2d\uff0c\u5e76\u76f4\u63a5\u8d4b\u503c\u7ed9\u4e24\u4e2a\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e24\u4e2a\u53d8\u91cfa\u548cb\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u8fdb\u884c\u4ea4\u6362\uff1a<code>a, b = b, a<\/code>\u3002\u8fd9\u6837\uff0ca\u7684\u503c\u5c06\u53d8\u4e3a\u539f\u6765b\u7684\u503c\uff0c\u800cb\u7684\u503c\u5219\u53d8\u4e3a\u539f\u6765a\u7684\u503c\uff0c\u8fc7\u7a0b\u7b80\u5355\u4e14\u76f4\u89c2\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u8fd8\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u4ea4\u6362\u53d8\u91cf\u7684\u503c\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528\u5143\u7ec4\uff0c\u8fd8\u6709\u5176\u4ed6\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4ea4\u6362\u53d8\u91cf\u7684\u503c\u3002\u53ef\u4ee5\u4f7f\u7528\u4e34\u65f6\u53d8\u91cf\u7684\u65b9\u5f0f\uff0c\u4f8b\u5982\uff1a<code>temp = a; a = b; b = temp<\/code>\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u7b97\u672f\u8fd0\u7b97\uff0c\u4f8b\u5982\uff1a<code>a = a + b; b = a - b; a = a - b<\/code>\u3002\u4e0d\u8fc7\uff0c\u4f7f\u7528\u4e34\u65f6\u53d8\u91cf\u548c\u7b97\u672f\u8fd0\u7b97\u7684\u65b9\u5f0f\u5728\u53ef\u8bfb\u6027\u548c\u5b89\u5168\u6027\u4e0a\u4e0d\u5982\u5143\u7ec4\u4ea4\u6362\u65b9\u5f0f\u76f4\u89c2\u3002<\/p>\n<p><strong>\u4ea4\u6362\u53d8\u91cf\u503c\u65f6\uff0c\u662f\u5426\u4f1a\u5f71\u54cd\u5230\u53d8\u91cf\u7684\u7c7b\u578b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4ea4\u6362\u53d8\u91cf\u7684\u503c\u4e0d\u4f1a\u6539\u53d8\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u53d8\u91cf\u7684\u7c7b\u578b\u662f\u52a8\u6001\u7684\uff0c\u610f\u5473\u7740\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u65f6\u95f4\u6301\u6709\u4e0d\u540c\u7c7b\u578b\u7684\u503c\u3002\u65e0\u8bba\u662f\u4f7f\u7528\u5143\u7ec4\u4ea4\u6362\u8fd8\u662f\u5176\u4ed6\u65b9\u6cd5\uff0c\u53d8\u91cf\u5728\u4ea4\u6362\u540e\u4ecd\u7136\u4fdd\u6301\u5176\u539f\u672c\u7684\u7c7b\u578b\uff0c\u53ea\u662f\u503c\u53d1\u751f\u4e86\u53d8\u5316\u3002\u56e0\u6b64\uff0c\u4ea4\u6362\u64cd\u4f5c\u672c\u8eab\u5e76\u4e0d\u4f1a\u5bf9\u7c7b\u578b\u9020\u6210\u4efb\u4f55\u5f71\u54cd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u4e34\u65f6\u53d8\u91cf\u3001Python\u7684\u591a\u5143\u8d4b\u503c\u8bed\u6cd5\u3001\u52a0\u51cf\u6cd5\u6216\u5f02\u6216 [&hellip;]","protected":false},"author":3,"featured_media":1115155,"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\/1115151"}],"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=1115151"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1115151\/revisions"}],"predecessor-version":[{"id":1115160,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1115151\/revisions\/1115160"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1115155"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1115151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1115151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1115151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}