{"id":985059,"date":"2024-12-27T07:33:30","date_gmt":"2024-12-26T23:33:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/985059.html"},"modified":"2024-12-27T07:33:33","modified_gmt":"2024-12-26T23:33:33","slug":"python%e5%a6%82%e4%bd%95%e4%b8%ba%e5%a4%8d%e5%88%b6%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/985059.html","title":{"rendered":"python\u5982\u4f55\u4e3a\u590d\u5236\u5217\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212405\/376e62cf-d1ad-45ea-80dd-eee53cfa3fd1.webp\" alt=\"python\u5982\u4f55\u4e3a\u590d\u5236\u5217\u8868\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u4e3a\u590d\u5236\u5217\u8868\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u5207\u7247\u64cd\u4f5c\u3001\u4f7f\u7528list()\u51fd\u6570\u3001\u4f7f\u7528copy\u6a21\u5757\u4e2d\u7684copy()\u65b9\u6cd5\u3001\u4ee5\u53ca\u5229\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u5207\u7247\u64cd\u4f5c\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u76f4\u89c2\u4e14\u6267\u884c\u6548\u7387\u9ad8\u3002\u5207\u7247\u64cd\u4f5c\u901a\u8fc7<code>new_list = old_list[:]<\/code>\u7684\u65b9\u5f0f\u6765\u590d\u5236\u5217\u8868\uff0c\u786e\u4fdd\u65b0\u5217\u8868\u4e0e\u539f\u5217\u8868\u7684\u5185\u5bb9\u76f8\u540c\u4f46\u4e3a\u4e0d\u540c\u7684\u5bf9\u8c61\u3002\u56e0\u6b64\uff0c\u4fee\u6539\u65b0\u5217\u8868\u4e0d\u4f1a\u5f71\u54cd\u5230\u539f\u5217\u8868\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\u7684\u5177\u4f53\u4f7f\u7528\u548c\u9002\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5207\u7247\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u5207\u7247\u64cd\u4f5c\u662fPython\u4e2d\u590d\u5236\u5217\u8868\u7684\u6700\u5e38\u7528\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7\u4f7f\u7528\u5192\u53f7<code>[:]<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u5305\u542b\u4e86\u539f\u5217\u8868\u7684\u6240\u6709\u5143\u7d20\u3002\u5207\u7247\u64cd\u4f5c\u7684\u4f18\u70b9\u5728\u4e8e\u5176\u7b80\u6d01\u6027\u548c\u9ad8\u6548\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 3, 4, 5]<\/p>\n<p>copied_list = original_list[:]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5207\u7247\u64cd\u4f5c\u9002\u7528\u4e8e\u9700\u8981\u6d45\u590d\u5236\u5217\u8868\u7684\u573a\u666f\u3002\u6d45\u590d\u5236\u6307\u7684\u662f\u590d\u5236\u5217\u8868\u672c\u8eab\uff0c\u800c\u4e0d\u662f\u5217\u8868\u4e2d\u5bf9\u8c61\u7684\u6df1\u5c42\u590d\u5236\u3002\u56e0\u6b64\uff0c\u5982\u679c\u5217\u8868\u4e2d\u5305\u542b\u53ef\u53d8\u5bf9\u8c61\uff08\u5982\u53e6\u4e00\u4e2a\u5217\u8868\uff09\uff0c\u5219\u8fd9\u4e9b\u53ef\u53d8\u5bf9\u8c61\u4ecd\u7136\u5171\u4eab\u76f8\u540c\u7684\u5f15\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528list()\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>list()<\/code>\u51fd\u6570\u662f\u53e6\u4e00\u79cd\u590d\u5236\u5217\u8868\u7684\u7b80\u5355\u65b9\u6cd5\u3002\u901a\u8fc7\u4f20\u5165\u4e00\u4e2a\u5217\u8868\u5bf9\u8c61\uff0c<code>list()<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u5bf9\u8c61\uff0c\u5305\u542b\u539f\u5217\u8868\u7684\u6240\u6709\u5143\u7d20\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u6548\u679c\u4e0e\u5207\u7247\u64cd\u4f5c\u76f8\u540c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 3, 4, 5]<\/p>\n<p>copied_list = list(original_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u540c\u6837\u9002\u7528\u4e8e\u6d45\u590d\u5236\u7684\u573a\u666f\uff0c\u4e0e\u5207\u7247\u64cd\u4f5c\u6ca1\u6709\u672c\u8d28\u533a\u522b\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001copy\u6a21\u5757\u4e2d\u7684copy()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>Python\u7684<code>copy<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e24\u79cd\u590d\u5236\u65b9\u6cd5\uff1a<code>copy()<\/code>\u548c<code>deepcopy()<\/code>\u3002<code>copy()<\/code>\u65b9\u6cd5\u7528\u4e8e\u6d45\u590d\u5236\uff0c\u800c<code>deepcopy()<\/code>\u7528\u4e8e\u6df1\u590d\u5236\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u5217\u8868\u590d\u5236\uff0c<code>copy()<\/code>\u65b9\u6cd5\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import copy<\/p>\n<p>original_list = [1, 2, 3, 4, 5]<\/p>\n<p>copied_list = copy.copy(original_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>copy()<\/code>\u65b9\u6cd5\u4e0e\u5207\u7247\u64cd\u4f5c\u548c<code>list()<\/code>\u51fd\u6570\u4e00\u6837\uff0c\u9002\u7528\u4e8e\u6d45\u590d\u5236\u3002\u7136\u800c\uff0c\u5b83\u7684\u4f18\u70b9\u5728\u4e8e\u53ef\u4ee5\u66f4\u6e05\u6670\u5730\u8868\u8fbe\u590d\u5236\u64cd\u4f5c\uff0c\u5c24\u5176\u662f\u5728\u4e0e<code>deepcopy()<\/code>\u7ed3\u5408\u4f7f\u7528\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7075\u6d3b\u4e14\u5f3a\u5927\u7684\u5217\u8868\u521b\u5efa\u65b9\u5f0f\u3002\u867d\u7136\u5b83\u901a\u5e38\u7528\u4e8e\u751f\u6210\u65b0\u5217\u8868\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u590d\u5236\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, 2, 3, 4, 5]<\/p>\n<p>copied_list = [item for item in original_list]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u590d\u5236\u5217\u8868\u867d\u7136\u4e0d\u5982\u5207\u7247\u64cd\u4f5c\u548c<code>list()<\/code>\u51fd\u6570\u76f4\u89c2\uff0c\u4f46\u5728\u9700\u8981\u5bf9\u5217\u8868\u8fdb\u884c\u4e00\u5b9a\u53d8\u6362\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u6df1\u590d\u5236\u4e0e\u6d45\u590d\u5236\u7684\u533a\u522b<\/p>\n<\/p>\n<p><p>\u5728\u8ba8\u8bba\u590d\u5236\u5217\u8868\u65f6\uff0c\u4e86\u89e3\u6df1\u590d\u5236\u4e0e\u6d45\u590d\u5236\u7684\u533a\u522b\u975e\u5e38\u91cd\u8981\u3002\u6d45\u590d\u5236\u53ea\u590d\u5236\u5217\u8868\u7684\u7b2c\u4e00\u5c42\uff0c\u5bf9\u4e8e\u5217\u8868\u4e2d\u7684\u53ef\u53d8\u5bf9\u8c61\uff0c\u590d\u5236\u540e\u7684\u5217\u8868\u4ecd\u7136\u4e0e\u539f\u5217\u8868\u5171\u4eab\u76f8\u540c\u7684\u5f15\u7528\u3002\u800c\u6df1\u590d\u5236\u5219\u4f1a\u9012\u5f52\u5730\u590d\u5236\u6240\u6709\u5c42\u6b21\u7684\u5bf9\u8c61\uff0c\u786e\u4fdd\u65b0\u5217\u8868\u4e2d\u7684\u6240\u6709\u5bf9\u8c61\u90fd\u662f\u72ec\u7acb\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import copy<\/p>\n<p>original_list = [[1, 2], [3, 4]]<\/p>\n<p>shallow_copied_list = copy.copy(original_list)<\/p>\n<p>deep_copied_list = copy.deepcopy(original_list)<\/p>\n<p>original_list[0][0] = 9<\/p>\n<p>print(shallow_copied_list)  # \u8f93\u51fa: [[9, 2], [3, 4]]<\/p>\n<p>print(deep_copied_list)     # \u8f93\u51fa: [[1, 2], [3, 4]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4fee\u6539\u539f\u5217\u8868\u4e2d\u7684\u5143\u7d20\u4f1a\u5f71\u54cd\u5230\u6d45\u590d\u5236\u7684\u5217\u8868\uff0c\u4f46\u4e0d\u4f1a\u5f71\u54cd\u6df1\u590d\u5236\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u9009\u62e9\u5408\u9002\u7684\u590d\u5236\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u5217\u8868\u590d\u5236\u65b9\u6cd5\u9700\u8981\u6839\u636e\u5177\u4f53\u9700\u6c42\u6765\u5b9a\u3002\u5982\u679c\u53ea\u662f\u590d\u5236\u5217\u8868\u7684\u7b2c\u4e00\u5c42\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u3001<code>list()<\/code>\u51fd\u6570\u6216<code>copy.copy()<\/code>\u65b9\u6cd5\u3002\u5982\u679c\u9700\u8981\u5b8c\u5168\u72ec\u7acb\u7684\u526f\u672c\uff0c\u7279\u522b\u662f\u5217\u8868\u4e2d\u5305\u542b\u5d4c\u5957\u5217\u8868\u6216\u5176\u4ed6\u53ef\u53d8\u5bf9\u8c61\u65f6\uff0c\u5219\u5e94\u4f7f\u7528<code>copy.deepcopy()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u5728\u6027\u80fd\u4e0a\uff0c\u5207\u7247\u64cd\u4f5c\u548c<code>list()<\/code>\u51fd\u6570\u901a\u5e38\u6bd4\u4f7f\u7528<code>copy<\/code>\u6a21\u5757\u66f4\u5feb\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u5185\u7f6e\u7684\u64cd\u4f5c\u4e14\u4e0d\u6d89\u53ca\u6a21\u5757\u5bfc\u5165\u3002\u7136\u800c\uff0c\u5728\u9700\u8981\u660e\u786e\u8868\u8fbe\u590d\u5236\u610f\u56fe\u65f6\uff0c<code>copy<\/code>\u6a21\u5757\u7684\u4f7f\u7528\u663e\u5f97\u66f4\u52a0\u6e05\u6670\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u590d\u5236\u5217\u8868\u7684\u9700\u6c42\u975e\u5e38\u5e38\u89c1\u3002\u4f8b\u5982\uff0c\u5728\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3001\u8fc7\u6ee4\u6216\u5176\u4ed6\u53d8\u6362\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u4fdd\u7559\u539f\u59cb\u5217\u8868\u7684\u526f\u672c\u3002\u540c\u65f6\uff0c\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u4e3a\u907f\u514d\u5171\u4eab\u6570\u636e\u7684\u7ade\u4e89\u6761\u4ef6\uff0c\u4e5f\u53ef\u80fd\u9700\u8981\u590d\u5236\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e86\u89e3\u4e0d\u540c\u7684\u5217\u8868\u590d\u5236\u65b9\u6cd5\u53ca\u5176\u7279\u70b9\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u6b63\u786e\u6027\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\u6765\u8bf4\uff0cPython\u4e3a\u590d\u5236\u5217\u8868\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u573a\u666f\u548c\u7279\u70b9\u3002\u5207\u7247\u64cd\u4f5c\u548c<code>list()<\/code>\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u6d45\u590d\u5236\u65b9\u6cd5\uff0c\u800c<code>copy.deepcopy()<\/code>\u5219\u7528\u4e8e\u6df1\u590d\u5236\u3002\u4e86\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u7684\u533a\u522b\u548c\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u66f4\u597d\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u5217\u8868\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u9ad8\u6548\u5730\u590d\u5236\u4e00\u4e2a\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u9ad8\u6548\u5730\u590d\u5236\u4e00\u4e2a\u5217\u8868\u3002\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\uff0c\u4f8b\u5982 <code>new_list = original_list[:]<\/code>\u3002\u6b64\u5916\uff0c\u4f7f\u7528 <code>list()<\/code> \u51fd\u6570\u4e5f\u662f\u4e00\u4e2a\u6709\u6548\u7684\u65b9\u6cd5\uff1a<code>new_list = list(original_list)<\/code>\u3002\u5982\u679c\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u76f8\u540c\u5143\u7d20\u7684\u65b0\u5217\u8868\uff0c<code>copy()<\/code> \u65b9\u6cd5\u4e5f\u53ef\u4ee5\u8fbe\u5230\u76f8\u540c\u7684\u6548\u679c\uff1a<code>new_list = original_list.copy()<\/code>\u3002<\/p>\n<p><strong>\u5728\u590d\u5236\u5217\u8868\u65f6\uff0c\u5982\u4f55\u5904\u7406\u5d4c\u5957\u5217\u8868\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u5f53\u590d\u5236\u4e00\u4e2a\u5305\u542b\u5d4c\u5957\u5217\u8868\u7684\u5217\u8868\u65f6\uff0c\u76f4\u63a5\u590d\u5236\u4f1a\u5bfc\u81f4\u539f\u5217\u8868\u548c\u65b0\u5217\u8868\u5171\u4eab\u5185\u5c42\u5217\u8868\u7684\u5f15\u7528\u3002\u8fd9\u610f\u5473\u7740\u5bf9\u5185\u5c42\u5217\u8868\u7684\u4fee\u6539\u4f1a\u5f71\u54cd\u5230\u4e24\u4e2a\u5217\u8868\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>copy.deepcopy()<\/code> \u65b9\u6cd5\uff0c\u5b83\u4f1a\u9012\u5f52\u5730\u590d\u5236\u6240\u6709\u5c42\u7ea7\u7684\u5217\u8868\uff0c\u786e\u4fdd\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u72ec\u7acb\u7684\u3002\u4f8b\u5982\uff0c<code>import copy; new_list = copy.deepcopy(original_list)<\/code>\u3002<\/p>\n<p><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u590d\u5236\u5217\u8868\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\uff08list comprehension\uff09\u6765\u590d\u5236\u5217\u8868\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u53ef\u4ee5\u5728\u590d\u5236\u7684\u540c\u65f6\u5bf9\u5143\u7d20\u8fdb\u884c\u5904\u7406\u3002\u4f8b\u5982\uff0c<code>new_list = [x for x in original_list]<\/code> \u53ef\u4ee5\u5b9e\u73b0\u590d\u5236\uff0c\u540c\u65f6\u5728\u6b64\u57fa\u7840\u4e0a\u8fdb\u884c\u5143\u7d20\u7684\u8f6c\u6362\u6216\u7b5b\u9009\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u5bf9\u5217\u8868\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\u65f6\u975e\u5e38\u5b9e\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u4e3a\u590d\u5236\u5217\u8868\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u5207\u7247\u64cd\u4f5c\u3001\u4f7f\u7528list()\u51fd\u6570\u3001\u4f7f\u7528copy\u6a21\u5757\u4e2d\u7684copy()\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":985066,"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\/985059"}],"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=985059"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/985059\/revisions"}],"predecessor-version":[{"id":985067,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/985059\/revisions\/985067"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/985066"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=985059"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=985059"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=985059"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}