{"id":1167766,"date":"2025-01-15T15:49:28","date_gmt":"2025-01-15T07:49:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1167766.html"},"modified":"2025-01-15T15:49:30","modified_gmt":"2025-01-15T07:49:30","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8a%e4%b8%a4%e5%80%bc%e5%af%b9%e8%b0%83","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1167766.html","title":{"rendered":"python\u5982\u4f55\u628a\u4e24\u503c\u5bf9\u8c03"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26065206\/87c21369-b72a-46bb-a98e-d38747ed507f.webp\" alt=\"python\u5982\u4f55\u628a\u4e24\u503c\u5bf9\u8c03\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u5bf9\u8c03\u4e24\u4e2a\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u4e34\u65f6\u53d8\u91cf\u3001Python\u7684\u5143\u7ec4\u89e3\u5305\u548c\u52a0\u51cf\u6cd5\u7b49\u3002<\/strong>\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c<strong>\u5143\u7ec4\u89e3\u5305<\/strong>\u662f\u4e00\u79cd\u975e\u5e38\u7b80\u6d01\u548c\u4f18\u96c5\u7684\u65b9\u5f0f\uff0c\u7279\u522b\u9002\u5408Python\u7f16\u7a0b\u98ce\u683c\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u5143\u7ec4\u89e3\u5305<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5143\u7ec4\u89e3\u5305\u662f\u4e00\u79cd\u975e\u5e38\u7b80\u6d01\u7684\u4ea4\u6362\u53d8\u91cf\u503c\u7684\u65b9\u6cd5\uff0c\u5b83\u5229\u7528\u4e86Python\u7684\u591a\u91cd\u8d4b\u503c\u7279\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 5<\/p>\n<p>b = 10<\/p>\n<p>a, b = b, a<\/p>\n<p>print(&quot;a:&quot;, a, &quot;b:&quot;, b)  # \u8f93\u51fa a: 10 b: 5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u6d01\u660e\u4e86\uff0c\u4e14\u65e0\u9700\u4f7f\u7528\u989d\u5916\u7684\u4e34\u65f6\u53d8\u91cf\u3002\u63a5\u4e0b\u6765\uff0c\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5143\u7ec4\u89e3\u5305\u7684\u5de5\u4f5c\u539f\u7406\u3002<\/p>\n<\/p>\n<p><p>\u5143\u7ec4\u89e3\u5305\u7684\u5de5\u4f5c\u539f\u7406\u662fPython\u5141\u8bb8\u5728\u8d4b\u503c\u64cd\u4f5c\u4e2d\u540c\u65f6\u5bf9\u591a\u4e2a\u53d8\u91cf\u8fdb\u884c\u8d4b\u503c\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u53f3\u4fa7\u7684<code>(b, a)<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u5143\u7d20\u7684\u5143\u7ec4\uff0c\u7136\u540e\u8fd9\u4e2a\u5143\u7ec4\u88ab\u89e3\u5305\u5230\u5de6\u4fa7\u7684<code>a<\/code>\u548c<code>b<\/code>\u4e2d\u3002\u8fd9\u6837\u4e00\u6765\uff0c<code>a<\/code>\u88ab\u8d4b\u503c\u4e3a\u539f\u6765\u7684<code>b<\/code>\u7684\u503c\uff0c<code>b<\/code>\u88ab\u8d4b\u503c\u4e3a\u539f\u6765\u7684<code>a<\/code>\u7684\u503c\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u5bf9\u8c03\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u4e34\u65f6\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u4e34\u65f6\u53d8\u91cf\u662f\u4e00\u79cd\u6bd4\u8f83\u4f20\u7edf\u7684\u65b9\u6cd5\uff0c\u5b83\u7684\u4f18\u70b9\u662f\u7b80\u5355\u660e\u4e86\uff0c\u4e0d\u4f1a\u9020\u6210\u6b67\u4e49\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 5<\/p>\n<p>b = 10<\/p>\n<p>temp = a<\/p>\n<p>a = b<\/p>\n<p>b = temp<\/p>\n<p>print(&quot;a:&quot;, a, &quot;b:&quot;, b)  # \u8f93\u51fa a: 10 b: 5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf<code>temp<\/code>\u4fdd\u5b58\u53d8\u91cf<code>a<\/code>\u7684\u503c\uff0c\u7136\u540e\u5c06<code>b<\/code>\u7684\u503c\u8d4b\u7ed9<code>a<\/code>\uff0c\u6700\u540e\u5c06\u4e34\u65f6\u53d8\u91cf<code>temp<\/code>\u7684\u503c\u8d4b\u7ed9<code>b<\/code>\uff0c\u4ece\u800c\u5b9e\u73b0\u5bf9\u8c03\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u52a0\u51cf\u6cd5<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u52a0\u51cf\u6cd5\u662f\u4e00\u79cd\u4e0d\u9700\u8981\u989d\u5916\u7a7a\u95f4\u7684\u65b9\u6cd5\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u53ea\u6709\u5728\u6570\u503c\u7c7b\u578b\u7684\u53d8\u91cf\u4e2d\u624d\u80fd\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 5<\/p>\n<p>b = 10<\/p>\n<p>a = a + b<\/p>\n<p>b = a - b<\/p>\n<p>a = a - b<\/p>\n<p>print(&quot;a:&quot;, a, &quot;b:&quot;, b)  # \u8f93\u51fa a: 10 b: 5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06<code>a<\/code>\u548c<code>b<\/code>\u7684\u548c\u8d4b\u503c\u7ed9<code>a<\/code>\uff0c\u7136\u540e\u901a\u8fc7<code>a-b<\/code>\u8d4b\u503c\u7ed9<code>b<\/code>\uff0c\u6700\u540e\u901a\u8fc7<code>a-b<\/code>\u8d4b\u503c\u7ed9<code>a<\/code>\uff0c\u4ece\u800c\u5b9e\u73b0\u5bf9\u8c03\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u662f\u4e00\u79cd\u975e\u5e38\u5de7\u5999\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u4efb\u4f55\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u4e0d\u5e38\u89c1\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 5<\/p>\n<p>b = 10<\/p>\n<p>a = a ^ b<\/p>\n<p>b = a ^ b<\/p>\n<p>a = a ^ b<\/p>\n<p>print(&quot;a:&quot;, a, &quot;b:&quot;, b)  # \u8f93\u51fa a: 10 b: 5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u5229\u7528\u4e86\u5f02\u6216\u8fd0\u7b97\u7684\u6027\u8d28\uff1a\u5982\u679c<code>a ^ b = c<\/code>\uff0c\u90a3\u4e48<code>c ^ b = a<\/code>\uff0c<code>c ^ a = b<\/code>\uff0c\u4ece\u800c\u5b9e\u73b0\u5bf9\u8c03\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Python\u5185\u7f6e\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u867d\u7136Python\u6ca1\u6709\u76f4\u63a5\u63d0\u4f9b\u5bf9\u8c03\u53d8\u91cf\u503c\u7684\u5185\u7f6e\u51fd\u6570\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/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<p>a, b = swap(a, b)<\/p>\n<p>print(&quot;a:&quot;, a, &quot;b:&quot;, b)  # \u8f93\u51fa a: 10 b: 5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u6d01\u4e14\u6613\u4e8e\u7406\u89e3\uff0c\u540c\u65f6\u4e5f\u4f53\u73b0\u4e86Python\u51fd\u6570\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5728\u5217\u8868\u4e2d\u5bf9\u8c03\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5bf9\u8c03\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lst = [1, 2, 3, 4, 5]<\/p>\n<p>i, j = 1, 3  # \u5bf9\u8c03\u5217\u8868\u4e2d\u7d22\u5f15\u4e3a1\u548c3\u7684\u5143\u7d20<\/p>\n<p>lst[i], lst[j] = lst[j], lst[i]<\/p>\n<p>print(lst)  # \u8f93\u51fa [1, 4, 3, 2, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u5229\u7528\u4e86Python\u7684\u5143\u7ec4\u89e3\u5305\u7279\u6027\uff0c\u5bf9\u5217\u8868\u4e2d\u7684\u4e24\u4e2a\u5143\u7d20\u8fdb\u884c\u5bf9\u8c03\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5bf9\u8c03\u5b57\u5178\u4e2d\u7684\u503c<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5bf9\u8c03\u5b57\u5178\u4e2d\u7684\u503c\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">d = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>d[&#39;a&#39;], d[&#39;b&#39;] = d[&#39;b&#39;], d[&#39;a&#39;]<\/p>\n<p>print(d)  # \u8f93\u51fa {&#39;a&#39;: 2, &#39;b&#39;: 1}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u540c\u6837\u5229\u7528\u4e86Python\u7684\u5143\u7ec4\u89e3\u5305\u7279\u6027\uff0c\u5bf9\u5b57\u5178\u4e2d\u7684\u4e24\u4e2a\u503c\u8fdb\u884c\u5bf9\u8c03\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5728\u7c7b\u5bf9\u8c61\u4e2d\u5bf9\u8c03\u5c5e\u6027\u503c<\/h3>\n<\/p>\n<p><p>\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5bf9\u8c03\u7c7b\u5bf9\u8c61\u7684\u5c5e\u6027\u503c\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self, a, b):<\/p>\n<p>        self.a = a<\/p>\n<p>        self.b = b<\/p>\n<p>obj = MyClass(5, 10)<\/p>\n<p>obj.a, obj.b = obj.b, obj.a<\/p>\n<p>print(&quot;a:&quot;, obj.a, &quot;b:&quot;, obj.b)  # \u8f93\u51fa a: 10 b: 5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u540c\u6837\u5229\u7528\u4e86Python\u7684\u5143\u7ec4\u89e3\u5305\u7279\u6027\uff0c\u5bf9\u7c7b\u5bf9\u8c61\u7684\u4e24\u4e2a\u5c5e\u6027\u503c\u8fdb\u884c\u5bf9\u8c03\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5bf9\u8c03\u591a\u7ec4\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5bf9\u8c03\u591a\u7ec4\u53d8\u91cf\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a, b, c, d = 1, 2, 3, 4<\/p>\n<p>a, b, c, d = d, c, b, a<\/p>\n<p>print(&quot;a:&quot;, a, &quot;b:&quot;, b, &quot;c:&quot;, c, &quot;d:&quot;, d)  # \u8f93\u51fa a: 4 b: 3 c: 2 d: 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u5229\u7528\u4e86Python\u7684\u591a\u91cd\u8d4b\u503c\u7279\u6027\uff0c\u5bf9\u591a\u7ec4\u53d8\u91cf\u8fdb\u884c\u5bf9\u8c03\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5728\u6392\u5e8f\u7b97\u6cd5\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u6392\u5e8f\u7b97\u6cd5\u4e2d\uff0c\u5bf9\u8c03\u53d8\u91cf\u503c\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def bubble_sort(lst):<\/p>\n<p>    n = len(lst)<\/p>\n<p>    for i in range(n):<\/p>\n<p>        for j in range(0, n-i-1):<\/p>\n<p>            if lst[j] &gt; lst[j+1]:<\/p>\n<p>                lst[j], lst[j+1] = lst[j+1], lst[j]<\/p>\n<p>    return lst<\/p>\n<p>lst = [64, 34, 25, 12, 22, 11, 90]<\/p>\n<p>sorted_lst = bubble_sort(lst)<\/p>\n<p>print(sorted_lst)  # \u8f93\u51fa [11, 12, 22, 25, 34, 64, 90]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u5229\u7528\u4e86Python\u7684\u5143\u7ec4\u89e3\u5305\u7279\u6027\uff0c\u5bf9\u5217\u8868\u4e2d\u7684\u4e24\u4e2a\u5143\u7d20\u8fdb\u884c\u5bf9\u8c03\uff0c\u4ece\u800c\u5b9e\u73b0\u5192\u6ce1\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u5728\u56fe\u7b97\u6cd5\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u56fe\u7b97\u6cd5\u4e2d\uff0c\u5bf9\u8c03\u53d8\u91cf\u503c\u540c\u6837\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def dijkstra(graph, start):<\/p>\n<p>    # \u521d\u59cb\u5316\u8ddd\u79bb\u8868<\/p>\n<p>    distances = {vertex: float(&#39;infinity&#39;) for vertex in graph}<\/p>\n<p>    distances[start] = 0<\/p>\n<p>    # \u521d\u59cb\u5316\u8bbf\u95ee\u96c6\u5408<\/p>\n<p>    visited = set()<\/p>\n<p>    while visited != set(graph.keys()):<\/p>\n<p>        # \u9009\u62e9\u672a\u8bbf\u95ee\u4e2d\u8ddd\u79bb\u6700\u5c0f\u7684\u9876\u70b9<\/p>\n<p>        min_vertex = min((vertex for vertex in distances if vertex not in visited), key=distances.get)<\/p>\n<p>        visited.add(min_vertex)<\/p>\n<p>        for neighbor, weight in graph[min_vertex].items():<\/p>\n<p>            if neighbor not in visited:<\/p>\n<p>                new_distance = distances[min_vertex] + weight<\/p>\n<p>                if new_distance &lt; distances[neighbor]:<\/p>\n<p>                    distances[neighbor] = new_distance<\/p>\n<p>    return distances<\/p>\n<p>graph = {<\/p>\n<p>    &#39;A&#39;: {&#39;B&#39;: 1, &#39;C&#39;: 4},<\/p>\n<p>    &#39;B&#39;: {&#39;A&#39;: 1, &#39;C&#39;: 2, &#39;D&#39;: 5},<\/p>\n<p>    &#39;C&#39;: {&#39;A&#39;: 4, &#39;B&#39;: 2, &#39;D&#39;: 1},<\/p>\n<p>    &#39;D&#39;: {&#39;B&#39;: 5, &#39;C&#39;: 1},<\/p>\n<p>}<\/p>\n<p>distances = dijkstra(graph, &#39;A&#39;)<\/p>\n<p>print(distances)  # \u8f93\u51fa {&#39;A&#39;: 0, &#39;B&#39;: 1, &#39;C&#39;: 3, &#39;D&#39;: 4}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u5229\u7528\u4e86Python\u7684\u5b57\u5178\u548c\u96c6\u5408\u7279\u6027\uff0c\u5bf9\u56fe\u7b97\u6cd5\u4e2d\u7684\u9876\u70b9\u8fdb\u884c\u5904\u7406\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86Dijkstra\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u5728\u77e9\u9635\u64cd\u4f5c\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u77e9\u9635\u64cd\u4f5c\u4e2d\uff0c\u5bf9\u8c03\u5143\u7d20\u503c\u4e5f\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def transpose(matrix):<\/p>\n<p>    rows = len(matrix)<\/p>\n<p>    cols = len(matrix[0])<\/p>\n<p>    transposed = [[0]*rows for _ in range(cols)]<\/p>\n<p>    for i in range(rows):<\/p>\n<p>        for j in range(cols):<\/p>\n<p>            transposed[j][i] = matrix[i][j]<\/p>\n<p>    return transposed<\/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>transposed_matrix = transpose(matrix)<\/p>\n<p>print(transposed_matrix)  # \u8f93\u51fa [[1, 4, 7], [2, 5, 8], [3, 6, 9]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5bf9\u8c03\u77e9\u9635\u4e2d\u7684\u5143\u7d20\uff0c\u5b9e\u73b0\u4e86\u77e9\u9635\u7684\u8f6c\u7f6e\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u5728\u5b57\u7b26\u4e32\u64cd\u4f5c\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b57\u7b26\u4e32\u64cd\u4f5c\u4e2d\uff0c\u5bf9\u8c03\u5b57\u7b26\u4f4d\u7f6e\u4e5f\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def reverse_string(s):<\/p>\n<p>    return s[::-1]<\/p>\n<p>s = &quot;hello&quot;<\/p>\n<p>reversed_s = reverse_string(s)<\/p>\n<p>print(reversed_s)  # \u8f93\u51fa &quot;olleh&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u5229\u7528\u4e86Python\u7684\u5207\u7247\u7279\u6027\uff0c\u5bf9\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u4f4d\u7f6e\u8fdb\u884c\u5bf9\u8c03\uff0c\u4ece\u800c\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u53cd\u8f6c\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u5728\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u590d\u6742\u6570\u636e\u7ed3\u6784\u4e2d\uff0c\u5bf9\u8c03\u5143\u7d20\u503c\u4e5f\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Node:<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.value = value<\/p>\n<p>        self.next = None<\/p>\n<p>def swap_nodes(head, x, y):<\/p>\n<p>    if x == y:<\/p>\n<p>        return head<\/p>\n<p>    prevX, currX = None, head<\/p>\n<p>    while currX and currX.value != x:<\/p>\n<p>        prevX, currX = currX, currX.next<\/p>\n<p>    prevY, currY = None, head<\/p>\n<p>    while currY and currY.value != y:<\/p>\n<p>        prevY, currY = currY, currY.next<\/p>\n<p>    if not currX or not currY:<\/p>\n<p>        return head<\/p>\n<p>    if prevX:<\/p>\n<p>        prevX.next = currY<\/p>\n<p>    else:<\/p>\n<p>        head = currY<\/p>\n<p>    if prevY:<\/p>\n<p>        prevY.next = currX<\/p>\n<p>    else:<\/p>\n<p>        head = currX<\/p>\n<p>    currX.next, currY.next = currY.next, currX.next<\/p>\n<p>    return head<\/p>\n<p>def print_list(head):<\/p>\n<p>    while head:<\/p>\n<p>        print(head.value, end=&quot; -&gt; &quot;)<\/p>\n<p>        head = head.next<\/p>\n<p>    print(&quot;None&quot;)<\/p>\n<p>head = Node(10)<\/p>\n<p>head.next = Node(15)<\/p>\n<p>head.next.next = Node(12)<\/p>\n<p>head.next.next.next = Node(13)<\/p>\n<p>head.next.next.next.next = Node(20)<\/p>\n<p>head.next.next.next.next.next = Node(14)<\/p>\n<p>print(&quot;Original list:&quot;)<\/p>\n<p>print_list(head)<\/p>\n<p>head = swap_nodes(head, 12, 20)<\/p>\n<p>print(&quot;List after swapping 12 and 20:&quot;)<\/p>\n<p>print_list(head)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u5355\u94fe\u8868\u4e2d\u8282\u70b9\u7684\u5bf9\u8c03\u64cd\u4f5c\uff0c\u4ece\u800c\u7075\u6d3b\u5904\u7406\u94fe\u8868\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u5143\u7d20\u5bf9\u8c03\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u5728\u6587\u4ef6\u64cd\u4f5c\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u64cd\u4f5c\u4e2d\uff0c\u5bf9\u8c03\u6587\u4ef6\u5185\u5bb9\u4e5f\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def swap_file_content(file1, file2):<\/p>\n<p>    with open(file1, &#39;r&#39;) as f1, open(file2, &#39;r&#39;) as f2:<\/p>\n<p>        content1, content2 = f1.read(), f2.read()<\/p>\n<p>    with open(file1, &#39;w&#39;) as f1, open(file2, &#39;w&#39;) as f2:<\/p>\n<p>        f1.write(content2)<\/p>\n<p>        f2.write(content1)<\/p>\n<p>file1 = &#39;file1.txt&#39;<\/p>\n<p>file2 = &#39;file2.txt&#39;<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6587\u4ef6<\/strong><\/h2>\n<p>with open(file1, &#39;w&#39;) as f:<\/p>\n<p>    f.write(&quot;Content of file1&quot;)<\/p>\n<p>with open(file2, &#39;w&#39;) as f:<\/p>\n<p>    f.write(&quot;Content of file2&quot;)<\/p>\n<p>print(&quot;Original contents:&quot;)<\/p>\n<p>with open(file1, &#39;r&#39;) as f:<\/p>\n<p>    print(f&quot;File1: {f.read()}&quot;)<\/p>\n<p>with open(file2, &#39;r&#39;) as f:<\/p>\n<p>    print(f&quot;File2: {f.read()}&quot;)<\/p>\n<p>swap_file_content(file1, file2)<\/p>\n<p>print(&quot;Contents after swapping:&quot;)<\/p>\n<p>with open(file1, &#39;r&#39;) as f:<\/p>\n<p>    print(f&quot;File1: {f.read()}&quot;)<\/p>\n<p>with open(file2, &#39;r&#39;) as f:<\/p>\n<p>    print(f&quot;File2: {f.read()}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u5185\u5bb9\uff0c\u5b9e\u73b0\u4e86\u4e24\u4e2a\u6587\u4ef6\u5185\u5bb9\u7684\u5bf9\u8c03\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u5728\u5e76\u53d1\u7f16\u7a0b\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5e76\u53d1\u7f16\u7a0b\u4e2d\uff0c\u5bf9\u8c03\u53d8\u91cf\u503c\u4e5f\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u64cd\u4f5c\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def swap_shared_variables(lock, shared_dict, key1, key2):<\/p>\n<p>    with lock:<\/p>\n<p>        shared_dict[key1], shared_dict[key2] = shared_dict[key2], shared_dict[key1]<\/p>\n<p>lock = threading.Lock()<\/p>\n<p>shared_dict = {&#39;x&#39;: 1, &#39;y&#39;: 2}<\/p>\n<p>print(&quot;Original values:&quot;, shared_dict)<\/p>\n<p>t1 = threading.Thread(target=swap_shared_variables, args=(lock, shared_dict, &#39;x&#39;, &#39;y&#39;))<\/p>\n<p>t2 = threading.Thread(target=swap_shared_variables, args=(lock, shared_dict, &#39;x&#39;, &#39;y&#39;))<\/p>\n<p>t1.start()<\/p>\n<p>t2.start()<\/p>\n<p>t1.join()<\/p>\n<p>t2.join()<\/p>\n<p>print(&quot;Values after swapping:&quot;, shared_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u5229\u7528\u7ebf\u7a0b\u9501\u4fdd\u8bc1\u4e86\u5e76\u53d1\u7f16\u7a0b\u4e2d\u7684\u6570\u636e\u4e00\u81f4\u6027\uff0c\u5b9e\u73b0\u4e86\u5171\u4eab\u53d8\u91cf\u7684\u5bf9\u8c03\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u516d\u3001\u5728\u6570\u636e\u5e93\u64cd\u4f5c\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5e93\u64cd\u4f5c\u4e2d\uff0c\u5bf9\u8c03\u8868\u8bb0\u5f55\u4e5f\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>def swap_database_records(conn, table, id1, id2):<\/p>\n<p>    cur = conn.cursor()<\/p>\n<p>    cur.execute(f&quot;SELECT * FROM {table} WHERE id = ?&quot;, (id1,))<\/p>\n<p>    record1 = cur.fetchone()<\/p>\n<p>    cur.execute(f&quot;SELECT * FROM {table} WHERE id = ?&quot;, (id2,))<\/p>\n<p>    record2 = cur.fetchone()<\/p>\n<p>    if record1 and record2:<\/p>\n<p>        cur.execute(f&quot;UPDATE {table} SET value = ? WHERE id = ?&quot;, (record2[1], id1))<\/p>\n<p>        cur.execute(f&quot;UPDATE {table} SET value = ? WHERE id = ?&quot;, (record1[1], id2))<\/p>\n<p>        conn.commit()<\/p>\n<p>conn = sqlite3.connect(&#39;:memory:&#39;)<\/p>\n<p>conn.execute(&quot;CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)&quot;)<\/p>\n<p>conn.execute(&quot;INSERT INTO test (value) VALUES (&#39;Record1&#39;)&quot;)<\/p>\n<p>conn.execute(&quot;INSERT INTO test (value) VALUES (&#39;Record2&#39;)&quot;)<\/p>\n<p>conn.commit()<\/p>\n<p>print(&quot;Original records:&quot;)<\/p>\n<p>for row in conn.execute(&quot;SELECT * FROM test&quot;):<\/p>\n<p>    print(row)<\/p>\n<p>swap_database_records(conn, &#39;test&#39;, 1, 2)<\/p>\n<p>print(&quot;Records after swapping:&quot;)<\/p>\n<p>for row in conn.execute(&quot;SELECT * FROM test&quot;):<\/p>\n<p>    print(row)<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7SQL\u67e5\u8be2\u548c\u66f4\u65b0\u64cd\u4f5c\uff0c\u5b9e\u73b0\u4e86\u6570\u636e\u5e93\u8868\u8bb0\u5f55\u7684\u5bf9\u8c03\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u603b\u7684\u6765\u8bf4\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u5b9e\u73b0\u5bf9\u8c03\u53d8\u91cf\u503c\u7684\u65b9\u6cd5\uff0c\u5305\u62ec<strong>\u4f7f\u7528\u4e34\u65f6\u53d8\u91cf\u3001\u5143\u7ec4\u89e3\u5305\u3001\u52a0\u51cf\u6cd5\u3001\u5f02\u6216\u8fd0\u7b97\u3001Python\u5185\u7f6e\u51fd\u6570\u3001\u5217\u8868\u5143\u7d20\u5bf9\u8c03\u3001\u5b57\u5178\u503c\u5bf9\u8c03\u3001\u7c7b\u5bf9\u8c61\u5c5e\u6027\u5bf9\u8c03\u3001\u591a\u7ec4\u53d8\u91cf\u5bf9\u8c03\u3001\u6392\u5e8f\u7b97\u6cd5\u3001\u56fe\u7b97\u6cd5\u3001\u77e9\u9635\u64cd\u4f5c\u3001\u5b57\u7b26\u4e32\u64cd\u4f5c\u3001\u6570\u636e\u7ed3\u6784\u64cd\u4f5c\u3001\u6587\u4ef6\u64cd\u4f5c\u3001\u5e76\u53d1\u7f16\u7a0b\u3001\u6570\u636e\u5e93\u64cd\u4f5c<\/strong>\u7b49\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u548c\u53ef\u8bfb\u6027\uff0c\u540c\u65f6\u4e5f\u80fd\u6ee1\u8db3\u4e0d\u540c\u5e94\u7528\u573a\u666f\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5143\u7ec4\u62c6\u5305\uff0c\u4f8b\u5982\uff1a<code>a, b = b, a<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u65e2\u7b80\u6d01\u53c8\u9ad8\u6548\uff0c\u80fd\u591f\u540c\u65f6\u8fdb\u884c\u8d4b\u503c\u548c\u4ea4\u6362\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u4e34\u65f6\u53d8\u91cf\uff0c\u5148\u5c06\u4e00\u4e2a\u53d8\u91cf\u7684\u503c\u5b58\u50a8\u5728\u4e34\u65f6\u53d8\u91cf\u4e2d\uff0c\u7136\u540e\u8fdb\u884c\u8d4b\u503c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u662f\u5426\u6709\u5185\u7f6e\u51fd\u6570\u53ef\u4ee5\u4ea4\u6362\u503c\uff1f<\/strong><br \/>Python\u5e76\u6ca1\u6709\u4e13\u95e8\u7684\u5185\u7f6e\u51fd\u6570\u6765\u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\uff0c\u4f46\u901a\u8fc7\u4f7f\u7528\u5143\u7ec4\u62c6\u5305\u7684\u65b9\u5f0f\uff0c\u5b9e\u9645\u4e0a\u53ef\u4ee5\u770b\u4f5c\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u201c\u5185\u7f6e\u201d\u4ea4\u6362\u673a\u5236\u3002\u7531\u4e8ePython\u7684\u8bbe\u8ba1\u54f2\u5b66\u5f3a\u8c03\u7b80\u6d01\u6027\u548c\u53ef\u8bfb\u6027\uff0c\u5143\u7ec4\u62c6\u5305\u6210\u4e3a\u4e86\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u5728\u4ea4\u6362\u503c\u65f6\uff0c\u5982\u4f55\u5904\u7406\u591a\u4e2a\u53d8\u91cf\u7684\u4ea4\u6362\uff1f<\/strong><br \/>\u5bf9\u4e8e\u591a\u4e2a\u53d8\u91cf\u7684\u4ea4\u6362\uff0cPython\u540c\u6837\u652f\u6301\u5143\u7ec4\u62c6\u5305\uff0c\u4f8b\u5982\uff1a<code>a, b, c = c, a, b<\/code>\u3002\u8fd9\u79cd\u5199\u6cd5\u53ef\u4ee5\u540c\u65f6\u4ea4\u6362\u591a\u4e2a\u53d8\u91cf\u7684\u503c\uff0c\u4fdd\u6301\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u548c\u53ef\u8bfb\u6027\u3002\u786e\u4fdd\u53d8\u91cf\u7684\u6570\u91cf\u4e00\u81f4\uff0c\u53ef\u4ee5\u6709\u6548\u907f\u514d\u7a0b\u5e8f\u8fd0\u884c\u65f6\u7684\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u5bf9\u8c03\u4e24\u4e2a\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u4e34\u65f6\u53d8\u91cf\u3001Python\u7684\u5143\u7ec4\u89e3\u5305\u548c\u52a0\u51cf\u6cd5\u7b49\u3002\u5728\u8fd9\u4e9b\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":1167768,"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\/1167766"}],"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=1167766"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1167766\/revisions"}],"predecessor-version":[{"id":1167770,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1167766\/revisions\/1167770"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1167768"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1167766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1167766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1167766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}