{"id":1038,"date":"2021-06-10T17:48:12","date_gmt":"2021-06-10T09:48:12","guid":{"rendered":"http:\/\/www.mustenaka.cn\/?p=1038"},"modified":"2021-06-10T18:27:29","modified_gmt":"2021-06-10T10:27:29","slug":"python-ab-ba","status":"publish","type":"post","link":"https:\/\/www.mustenaka.cn\/index.php\/2021\/06\/10\/python-ab-ba\/","title":{"rendered":"Python a,b = b,a \u7684\u5b9e\u73b0"},"content":{"rendered":"<p>Python\u6709\u4e00\u4e2a\u5f88\u795e\u5947\u7684\u53d6\u5de7\u6280\u5de7<\/p>\n<p>a,b = b,a<\/p>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u5b8c\u6210\u6570\u636e\u7684\u4ea4\u6362\uff0c\u800c\u4e0d\u9700\u8981\u4f7f\u7528\u51fd\u6570swap\uff0c\u6216\u8005\u5176\u4ed6\u7684\u4ea4\u6362\u65b9\u5f0f\uff0c\u90a3\u4e48\u8fd9\u4e2a\u64cd\u4f5c\u7a76\u7adf\u600e\u4e48\u5b9e\u73b0\uff0c\u800c\u601d\u8003\u53c8\u662f\u4ec0\u4e48\u5462\uff1f<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\" python=\"\">'''\r\n\u521b\u5efaa,b\u4e24\u4e2a\u53d8\u91cf\r\n'''\r\na = 1\r\nb = 3\r\nprint(a,b)<\/code><\/pre>\n<p>Output: 1 3<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\" python=\"\">'''\r\n\u4ea4\u6362a,b=b,a\uff0c\u53d1\u73b0\u4ed6\u4eec\u7684\u6570\u636e\u8fdb\u884c\u4e86\u5bf9\u8c03\r\n\u672c\u80fd\u7684\u4ee5\u4e3a\uff0c\u662f\u4e0d\u662f\u8fdb\u884c\u4e86\u5730\u5740\u4ea4\u6362\r\n\u4f46\u662f\u9700\u8981\u601d\u7d22\u4e00\u4e0b\uff0cC\u8bed\u8a00\u7684swap\u662f\u4e0d\u662f\r\nswap(&amp;a, &amp;b){\r\n    temp = a;\r\n    a = b;\r\n    b = temp;\r\n}\r\n\u8fd9\u6837\u7684\u65b9\u5f0f\uff0c\u800cPython\uff0c\u662f\u6ca1\u6709temp\u7684\uff0c\u6240\u4ee5\u9700\u8981\u7ee7\u7eed\u601d\u8003\u4e0b\u53bb\r\n'''\r\na,b = b,a\r\nprint(a,b)<\/code><\/pre>\n<p>Output: 3 1<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\" python=\"\">'''\r\n\u6211\u4eec\u521b\u5efa\u4e00\u4e2alist = [1,2,3,4,5]\r\n\u540c\u65f6\u521b\u5efa\u4e00\u4e2ai\u7528\u6765\u7d22\u5f15\u8fd9\u4e2alist\r\nli[i] = \u7b2c\u4e00\u4e2a\u6570\u5b57\uff0c1\r\n'''\r\nli = [1,2,3,4,5]\r\ni = 0\r\nprint(li,i)<\/code><\/pre>\n<p>Output: [1, 2, 3, 4, 5] 0<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\" python=\"\">'''\r\neg1.\r\n\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u7b2c\u4e00\u4e2a\u5143\u7d20\uff1a1\u548c\u7b2cli[1]\u4e2a\u5143\u7d20\uff08\u4e5f\u5c31\u662f\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff09\uff0c2\u8fdb\u884c\u4ea4\u6362\r\n\u7406\u8bba\u4e0a\u5e94\u8be5\u5f97\u5230\u4e00\u6570\u636e\r\n[2,1,3,4,5]\r\n\u7136\u800c\u4e8b\u5b9e\u4e0a\u786e\u5b9e[2,2,1,4,5]\uff0c\u4e0e\u6982\u5ff5\u53d1\u751f\u4e86\u504f\u5dee\r\n'''\r\nli[i], li[li[i]] = li[li[i]], li[i]\r\nprint(li,i)<\/code><\/pre>\n<p>Output: [2, 2, 1, 4, 5] 0<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\" python=\"\">'''\r\neg2.\r\n\u5bf9\u521a\u521a\u7684\u6570\u636e\u5728\u505a\u4e00\u4e2a\u540c\u7c7b\u578b\u7684\u5b9e\u9a8c\r\n'''\r\nli2 = [1,2,3,4,5]  # \u521b\u5efa\u7684\u662fli2\r\ni = 0\r\nli2[li2[i]], li2[i] = li2[i], li2[li2[i]]\r\nprint(li2,i)<\/code><\/pre>\n<p>Output: [2, 1, 3, 4, 5] 0<\/p>\n<h3 id=\"\u73b0\u8c61\">\u73b0\u8c61<a class=\"anchor-link\" href=\"http:\/\/localhost:8888\/notebooks\/Downloads\/jupyter\/%E8%A7%A3%E7%AD%94a%2Cb%3Db%2Ca.ipynb#%E7%8E%B0%E8%B1%A1\"><\/a><\/h3>\n<p>\u53d1\u73b0\u4ed6\u4eec\u7684\u6570\u636e\u4ec5\u4ec5\u662f\u4e00\u4e2a\u987a\u5e8f\u4e0d\u540c\u800c\u4e0d\u4e00\u6837\u4e86\uff0c\u731c\u6d4b\u7b2c\u4e00\u4e2a\u5b9e\u9a8ceg1.\u5e94\u8be5\u662f\u5bf9li[i]\u7387\u5148\u8fdb\u884c\u4e86\u8d4b\u503c\uff0c\u5bfc\u81f4li[i]\u63d0\u524d\u7f16\u7a0b\u4e862\uff0c\u800cli[li[i]] \u7f16\u7a0b\u4e86li[2]\u5219\u53d8\u6210\u4e86\u7b2c\u4e09\u4e2a\u5143\u7d20\uff0c\u6545li[i] = 2 \u800cli[li[i]] = \u6700\u5f00\u59cb\u76841<\/p>\n<h3 id=\"\u90a3\u4e48\u95ee\u9898\u6765\u4e86\">\u90a3\u4e48\u95ee\u9898\u6765\u4e86<a class=\"anchor-link\" href=\"http:\/\/localhost:8888\/notebooks\/Downloads\/jupyter\/%E8%A7%A3%E7%AD%94a%2Cb%3Db%2Ca.ipynb#%E9%82%A3%E4%B9%88%E9%97%AE%E9%A2%98%E6%9D%A5%E4%BA%86\"><\/a><\/h3>\n<p>\u5982\u679c\u4ec5\u4ec5\u662f\u901a\u8fc7\u6307\u9488\u8fdb\u884c\u4e86\u5185\u5b58\u5730\u5740\u7684\u6539\u53d8\uff0c\u4e0d\u5e94\u8be5\u5728li[i] \u8d4b\u503c\u4e862\u4e4b\u540e\uff0cli[li[i]] \u4e5f\u5e94\u8be5\u53d8\u4e3a2\u4e48\uff1f\u800c\u5374\u4e8b\u5b9e\u4e0a\u53d1\u751f\u4e86\u4ea4\u6362\u53d8\u6210\u4e86li[i]\u6700\u5f00\u59cb\u7684\u72b6\u6001\u5373\u4e3a1<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\" python=\"\"># \u7ffb\u9605\u5b98\u65b9\u7684\u8bf4\u7c73\u6587\u6863\uff1a https:\/\/docs.python.org\/3\/library\/dis.html\r\n'''\r\n\u56de\u5230\u4e00\u5f00\u59cb\u7684a,b = b,a\r\npython\u5728\u591a\u53c2\u6570\u8d4b\u503c\u7684\u65f6\u5019\uff0c\u4f1a\u5c06\u6570\u636e\u6253\u5305\u6210\u4e00\u4e2a\u5143\u7ec4\uff08tuple\uff09\uff0c\u5143\u7ec4\u662f\u4e0d\u53ef\u4ee5\u4fee\u6539\u7684\u5177\u6709\u4fdd\u62a4\u7279\u6027\r\n\u5373 a,b = (b, a)\r\n\u518d\u8fdb\u884c\u5143\u7ec4\u62c6\u5305\u5de5\u4f5c\r\na = b_old \r\nb = a_old\r\n\r\n\u800c\u5143\u7ec4\u62c6\u5305\u7684\u601d\u60f3\uff0c\u5176\u5b9e\u5c31\u662f\u628a\u65b0\u53d8\u91cf\u653e\u8fdb\u4e00\u4e2a\u6808\u4e2d\uff0c\u65e7\u7684\u5143\u7ec4\u653e\u8fdb\u4e00\u4e2a\u6808\u4e2d\r\n\u4f9d\u6b21\u8f93\u51fa\u6808\u9876\u5143\u7d20\u8fdb\u884c\u4e00\u4e00\u5339\u914d\r\n\r\n\u6ce8\u610f\u4e0d\u540c\u7ed3\u6784\u89c1\u4e5f\u53ef\u4ee5\u64cd\u4f5c\uff0c\u7531\u4e8epython\u7684\u7b49\u4e8e\u53f7\u975e\u5e38\u7684\u7075\u6d3b\r\n'''\r\n\r\n# \u521b\u5efa\u4e86\u4e00\u4e2adata\u7684list\u6570\u636e\uff0c\u5176\u4e2d\u5305\u62ec\u4e86\uff0c\u5b57\u7b26\u4e32\uff0c\u6570\u5b57\uff0c\u5143\u7ec4\r\ndata = ['abcd',11,2,(1,2,3)]\r\nx1,x2,x3,x4 = data\r\nprint(x1)\r\nprint(x2)\r\nprint(x3)\r\nprint(x4)<\/code><\/pre>\n<p>output:<\/p>\n<p>abcd<br \/>\n11<br \/>\n2<br \/>\n(1, 2, 3)<\/p>\n<p>\u5bf9\u5176\u4e2d\u7684\u5185\u5bb9\u518d\u505a\u62c6\u5206\u4e5f\u662f\u53ef\u4ee5\u7684<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"\" python=\"\"># \u5176\u4e2dx4\u53c8\u53ef\u4ee5\u62c6\u5206\r\ny1,y2,y3 = x4\r\nprint(y1)\r\nprint(y2)\r\nprint(y3)<\/code><\/pre>\n<p>Output:<\/p>\n<p>1<br \/>\n2<br \/>\n3<\/p>\n<h3 id=\"\u603b\u7ed3\">\u603b\u7ed3<a class=\"anchor-link\" href=\"http:\/\/localhost:8888\/notebooks\/Downloads\/jupyter\/%E8%A7%A3%E7%AD%94a%2Cb%3Db%2Ca.ipynb#%E6%80%BB%E7%BB%93\"><\/a><\/h3>\n<p>\u5176\u5b9e\u8fd9\u4e2a\u7279\u6027\u8981\u6ce8\u610f\uff0c\u5e76\u4e14\u5728\u4f7f\u7528\u4e2d\u8981\u7559\u610f\u6570\u636e\u7684\u7c7b\u578b\u5bf9\u9f50\uff0c\u5426\u5219\u53ef\u80fd\u51fa\u73b0\u6f5c\u5728BUG\uff0c\u7531\u4e8ePython\u7684\u51fd\u6570\u8fd4\u56de\u503c\u662f\u9ed8\u8ba4\u591a\u8fd4\u56de\u503c\u7684\uff0c\u6240\u4ee5\u4e5f\u56e0\u4e3a\u8fd9\u4e2a\u7279\u6027\u80fd\u591f\u8282\u7701\u5f88\u591a\u51fd\u6570\u8fd4\u56de\u503c\u7684\u800c\u5916\u8bbe\u8ba1\uff0c\u6bd4\u5982C\u8bed\u8a00\u4e2d\uff0c\u8981\u60f3\u5b9e\u73b0\u4e00\u4e2a\u51fd\u6570\u540c\u65f6\u8fd4\u56de\u591a\u4e2a\u503c<\/p>\n<p>1.\u8981\u4e48\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u7ed3\u6784\uff0c\u628a\u591a\u51fd\u6570\u8fd4\u56de\u503c\u6846\u6b7b 2.\u8981\u4e48\u4f7f\u7528\u6570\u636e\u7ed3\u6784\u5b57\u5178\uff0c\u5c06\u591a\u53c2\u6570\u4f5c\u4e3a\u5b57\u5178\u6620\u5c04\u7684\u65b9\u5f0f\u4f20\u9012\u51fa\u6765<\/p>\n<p>\u800cPython\u53ea\u9700\u8981 a,b = func(x) \u8fd9\u6837<\/p>\n<p>&nbsp;<\/p>\n<h4 id=\"\u6ce8\">\u6ce8<a class=\"anchor-link\" href=\"http:\/\/localhost:8888\/notebooks\/Downloads\/jupyter\/%E8%A7%A3%E7%AD%94a%2Cb%3Db%2Ca.ipynb#%E6%B3%A8\"><\/a><\/h4>\n<p>Python\u4e5f\u53ef\u4ee5\u81ea\u5efa\u6570\u636e\u7ed3\u6784\uff0c\u5982\u679c\u4f60\u7528C,JAVA,C++,C#\u8fd9\u7c7b\u8bed\u8a00\uff0c\u505a\u81ea\u5efa\u6570\u636e\u7ed3\u6784\u5e94\u8be5\u633a\u9891\u7e41\u7684\uff0c\u800cPython\u5c31\u57fa\u672c\u7528\u9ed8\u8ba4\u7684\u90a3\u4e9b\uff0c\u8fd9\u4e5f\u5f97\u76ca\u4e8ePython\u7075\u6d3b\u7684\u7279\u6027\uff0c\u4e0d\u9700\u8981\u5efa\u7acb\u90a3\u4e48\u591a\u6570\u636e\u7ed3\u6784\uff0c\u53cd\u800c\u8fd8\u4e0d\u5982\u5efa\u7acb\u57fa\u7c7b<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u6709\u4e00\u4e2a\u5f88\u795e\u5947\u7684\u53d6\u5de7\u6280\u5de7 a,b = b,a \u8fd9\u6837\u5c31\u53ef\u4ee5\u5b8c\u6210\u6570\u636e\u7684\u4ea4\u6362\uff0c\u800c\u4e0d\u9700\u8981\u4f7f\u7528\u51fd\u6570s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[31],"class_list":["post-1038","post","type-post","status-publish","format-standard","hentry","category-python","tag-python"],"views":1995,"_links":{"self":[{"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/posts\/1038","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1038"}],"version-history":[{"count":1,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/posts\/1038\/revisions"}],"predecessor-version":[{"id":1039,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/posts\/1038\/revisions\/1039"}],"wp:attachment":[{"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mustenaka.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}