{"id":1105378,"date":"2025-01-08T16:30:24","date_gmt":"2025-01-08T08:30:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1105378.html"},"modified":"2025-01-08T16:30:27","modified_gmt":"2025-01-08T08:30:27","slug":"python%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e4%b8%a4%e4%b8%aa%e5%88%97%e8%a1%a8%e4%b8%ad%e7%9a%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1105378.html","title":{"rendered":"python\u5982\u4f55\u5220\u9664\u4e24\u4e2a\u5217\u8868\u4e2d\u7684"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070313\/4ddd8f79-baf8-4e13-bb0c-1d2873f32adc.webp\" alt=\"python\u5982\u4f55\u5220\u9664\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d: <strong>\u5728Python\u4e2d\u5220\u9664\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u4f8b\u5982\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u5faa\u73af\u904d\u5386\u7684\u65b9\u6cd5<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u96c6\u5408\u7684\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u5355\u6613\u61c2\uff0c\u800c\u4e14\u6027\u80fd\u76f8\u5bf9\u8f83\u9ad8\u3002\u901a\u8fc7\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5220\u9664\u91cd\u590d\u5143\u7d20\uff0c\u56e0\u4e3a\u96c6\u5408\u662f\u65e0\u5e8f\u4e14\u4e0d\u5141\u8bb8\u91cd\u590d\u7684\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528\u96c6\u5408\u7684\u65b9\u6cd5\u6765\u5220\u9664\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\uff0c\u5e76\u7ed9\u51fa\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u5220\u9664\u91cd\u590d\u5143\u7d20<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u5220\u9664\u91cd\u590d\u5143\u7d20\u662f\u4e00\u79cd\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002\u96c6\u5408\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u4e0d\u5141\u8bb8\u91cd\u590d\u5143\u7d20\uff0c\u56e0\u6b64\u53ef\u4ee5\u7528\u6765\u81ea\u52a8\u53bb\u91cd\u3002\u5c06\u4e24\u4e2a\u5217\u8868\u5408\u5e76\u4e3a\u4e00\u4e2a\u96c6\u5408\uff0c\u7136\u540e\u5c06\u96c6\u5408\u8f6c\u6362\u56de\u5217\u8868\uff0c\u5c31\u80fd\u5f97\u5230\u4e00\u4e2a\u65e0\u91cd\u590d\u5143\u7d20\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>list1 = [1, 2, 3, 4, 5]<\/p>\n<p>list2 = [3, 4, 5, 6, 7]<\/p>\n<h2><strong>\u5c06\u4e24\u4e2a\u5217\u8868\u5408\u5e76\u4e3a\u4e00\u4e2a\u96c6\u5408<\/strong><\/h2>\n<p>combined_set = set(list1 + list2)<\/p>\n<h2><strong>\u5c06\u96c6\u5408\u8f6c\u6362\u56de\u5217\u8868<\/strong><\/h2>\n<p>result_list = list(combined_set)<\/p>\n<p>print(result_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06 <code>list1<\/code> \u548c <code>list2<\/code> \u5408\u5e76\u4e3a\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u7136\u540e\u5c06\u8fd9\u4e2a\u65b0\u7684\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408 <code>combined_set<\/code>\u3002\u7531\u4e8e\u96c6\u5408\u4e0d\u5141\u8bb8\u91cd\u590d\u5143\u7d20\uff0c\u56e0\u6b64\u91cd\u590d\u7684\u5143\u7d20\u4f1a\u88ab\u81ea\u52a8\u53bb\u9664\u3002\u6700\u540e\uff0c\u6211\u4eec\u5c06\u96c6\u5408 <code>combined_set<\/code> \u8f6c\u6362\u56de\u5217\u8868 <code>result_list<\/code>\uff0c\u5f97\u5230\u4e00\u4e2a\u65e0\u91cd\u590d\u5143\u7d20\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5220\u9664\u91cd\u590d\u5143\u7d20<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u751f\u6210\u5217\u8868\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408 <code>not in<\/code> \u5173\u952e\u5b57\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5220\u9664\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>list1 = [1, 2, 3, 4, 5]<\/p>\n<p>list2 = [3, 4, 5, 6, 7]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5220\u9664\u91cd\u590d\u5143\u7d20<\/strong><\/h2>\n<p>result_list = [item for item in list1 if item not in list2] + list2<\/p>\n<p>print(result_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868 <code>result_list<\/code>\uff0c\u5176\u4e2d\u5305\u542b <code>list1<\/code> \u4e2d\u4e0d\u5728 <code>list2<\/code> \u4e2d\u7684\u5143\u7d20\u4ee5\u53ca <code>list2<\/code> \u4e2d\u7684\u6240\u6709\u5143\u7d20\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd <code>result_list<\/code> \u4e2d\u6ca1\u6709\u91cd\u590d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5220\u9664\u91cd\u590d\u5143\u7d20<\/p>\n<\/p>\n<p><p>\u5faa\u73af\u904d\u5386\u662f\u4e00\u79cd\u4f20\u7edf\u7684\u5220\u9664\u91cd\u590d\u5143\u7d20\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u904d\u5386\u4e24\u4e2a\u5217\u8868\uff0c\u9010\u4e2a\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u91cd\u590d\uff0c\u5982\u679c\u91cd\u590d\u5219\u5c06\u5176\u5220\u9664\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>list1 = [1, 2, 3, 4, 5]<\/p>\n<p>list2 = [3, 4, 5, 6, 7]<\/p>\n<h2><strong>\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5220\u9664\u91cd\u590d\u5143\u7d20<\/strong><\/h2>\n<p>result_list = list1[:]<\/p>\n<p>for item in list2:<\/p>\n<p>    if item not in result_list:<\/p>\n<p>        result_list.append(item)<\/p>\n<p>print(result_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868 <code>result_list<\/code>\uff0c\u5176\u521d\u59cb\u503c\u4e3a <code>list1<\/code> \u7684\u526f\u672c\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u904d\u5386 <code>list2<\/code> \u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u68c0\u67e5\u8be5\u5143\u7d20\u662f\u5426\u5df2\u7ecf\u5728 <code>result_list<\/code> \u4e2d\u3002\u5982\u679c\u4e0d\u5728\uff0c\u5219\u5c06\u5176\u6dfb\u52a0\u5230 <code>result_list<\/code> \u4e2d\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd <code>result_list<\/code> \u4e2d\u6ca1\u6709\u91cd\u590d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u5b57\u5178\u5220\u9664\u91cd\u590d\u5143\u7d20<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u4e5f\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u6765\u5220\u9664\u91cd\u590d\u5143\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u4f5c\u4e3a\u5b57\u5178\u7684\u952e\uff0c\u8fd9\u6837\u53ef\u4ee5\u81ea\u52a8\u53bb\u91cd\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>list1 = [1, 2, 3, 4, 5]<\/p>\n<p>list2 = [3, 4, 5, 6, 7]<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178\u5220\u9664\u91cd\u590d\u5143\u7d20<\/strong><\/h2>\n<p>combined_dict = {item: None for item in list1 + list2}<\/p>\n<h2><strong>\u5c06\u5b57\u5178\u7684\u952e\u8f6c\u6362\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>result_list = list(combined_dict.keys())<\/p>\n<p>print(result_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u5c06 <code>list1<\/code> \u548c <code>list2<\/code> \u4e2d\u7684\u5143\u7d20\u4f5c\u4e3a\u5b57\u5178 <code>combined_dict<\/code> \u7684\u952e\uff0c\u7531\u4e8e\u5b57\u5178\u7684\u952e\u4e0d\u5141\u8bb8\u91cd\u590d\uff0c\u56e0\u6b64\u53ef\u4ee5\u81ea\u52a8\u53bb\u91cd\u3002\u6700\u540e\uff0c\u6211\u4eec\u5c06\u5b57\u5178 <code>combined_dict<\/code> \u7684\u952e\u8f6c\u6362\u4e3a\u5217\u8868 <code>result_list<\/code>\uff0c\u5f97\u5230\u4e00\u4e2a\u65e0\u91cd\u590d\u5143\u7d20\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u5220\u9664\u91cd\u590d\u5143\u7d20<\/p>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5220\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>set<\/code> \u51fd\u6570\u548c <code>list<\/code> \u51fd\u6570\u7ec4\u5408\u6765\u5220\u9664\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>list1 = [1, 2, 3, 4, 5]<\/p>\n<p>list2 = [3, 4, 5, 6, 7]<\/p>\n<h2><strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u5220\u9664\u91cd\u590d\u5143\u7d20<\/strong><\/h2>\n<p>result_list = list(set(list1 + list2))<\/p>\n<p>print(result_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06 <code>list1<\/code> \u548c <code>list2<\/code> \u5408\u5e76\u4e3a\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528 <code>set<\/code> \u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u81ea\u52a8\u53bb\u91cd\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528 <code>list<\/code> \u51fd\u6570\u5c06\u96c6\u5408\u8f6c\u6362\u56de\u5217\u8868\uff0c\u5f97\u5230\u4e00\u4e2a\u65e0\u91cd\u590d\u5143\u7d20\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5220\u9664\u91cd\u590d\u5143\u7d20<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u5185\u7f6e\u51fd\u6570\u548c\u6570\u636e\u7ed3\u6784\uff0cPython \u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5220\u9664\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>pandas<\/code> \u5e93\u4e2d\u7684 <code>drop_duplicates<\/code> \u51fd\u6570\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>import pandas as pd<\/p>\n<p>list1 = [1, 2, 3, 4, 5]<\/p>\n<p>list2 = [3, 4, 5, 6, 7]<\/p>\n<h2><strong>\u4f7f\u7528 pandas \u5e93\u5220\u9664\u91cd\u590d\u5143\u7d20<\/strong><\/h2>\n<p>df = pd.DataFrame({&#39;A&#39;: list1 + list2})<\/p>\n<p>result_list = df[&#39;A&#39;].drop_duplicates().tolist()<\/p>\n<p>print(result_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06 <code>list1<\/code> \u548c <code>list2<\/code> \u5408\u5e76\u4e3a\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a <code>pandas<\/code> \u6570\u636e\u6846 <code>df<\/code>\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528 <code>drop_duplicates<\/code> \u51fd\u6570\u5220\u9664\u91cd\u590d\u5143\u7d20\uff0c\u5e76\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u5217\u8868 <code>result_list<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u591a\u79cd\u5220\u9664\u4e24\u4e2a\u5217\u8868\u4e2d\u91cd\u590d\u5143\u7d20\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u5faa\u73af\u904d\u5386\u3001\u5b57\u5178\u3001\u5185\u7f6e\u51fd\u6570\u548c\u7b2c\u4e09\u65b9\u5e93 <code>pandas<\/code>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u7684\u65b9\u6cd5\u6700\u4e3a\u7b80\u5355\u548c\u9ad8\u6548<\/strong>\uff0c\u9002\u5408\u5927\u591a\u6570\u573a\u666f\u3002\u5982\u679c\u9700\u8981\u66f4\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0c\u5220\u9664\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u662fPython\u4e2d\u5e38\u89c1\u7684\u64cd\u4f5c\u4e4b\u4e00\uff0c\u638c\u63e1\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u4e0d\u540c\u573a\u666f\u4e0b\u7075\u6d3b\u5e94\u5bf9\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u540c\u65f6\u5220\u9664\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u76f8\u540c\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u6765\u9ad8\u6548\u5730\u5220\u9664\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u76f8\u540c\u5143\u7d20\u3002\u9996\u5148\uff0c\u5c06\u4e24\u4e2a\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u7136\u540e\u4f7f\u7528\u96c6\u5408\u7684\u4ea4\u96c6\u64cd\u4f5c\u627e\u5230\u5171\u540c\u7684\u5143\u7d20\uff0c\u6700\u540e\u4ece\u539f\u59cb\u5217\u8868\u4e2d\u53bb\u9664\u8fd9\u4e9b\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">list1 = [1, 2, 3, 4, 5]\nlist2 = [4, 5, 6, 7]\ncommon_elements = set(list1).intersection(set(list2))\n\nlist1 = [item for item in list1 if item not in common_elements]\nlist2 = [item for item in list2 if item not in common_elements]\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<code>list1<\/code>\u548c<code>list2<\/code>\u4e2d\u90fd\u5c06\u5220\u9664\u76f8\u540c\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5220\u9664\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u5e76\u4fdd\u7559\u53e6\u4e00\u4e2a\u5217\u8868\u4e0d\u53d8\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5220\u9664\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\uff0c\u800c\u4e0d\u5f71\u54cd\u53e6\u4e00\u4e2a\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5e0c\u671b\u4ece<code>list1<\/code>\u4e2d\u5220\u9664\u5143\u7d20<code>3<\/code>\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<pre><code class=\"language-python\">list1 = [1, 2, 3, 4, 5]\nlist2 = [4, 5, 6, 7]\nlist1 = [item for item in list1 if item != 3]\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u4fdd\u7559<code>list2<\/code>\u4e0d\u53d8\uff0c\u53ea\u5728<code>list1<\/code>\u4e2d\u5220\u9664<code>3<\/code>\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u7b80\u5355\u7684\u65b9\u6cd5\u5728Python\u4e2d\u5220\u9664\u4e24\u4e2a\u5217\u8868\u7684\u6240\u6709\u5143\u7d20\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>clear()<\/code>\u65b9\u6cd5\u8f7b\u677e\u5730\u6e05\u7a7a\u5217\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">list1 = [1, 2, 3]\nlist2 = [4, 5, 6]\nlist1.clear()\nlist2.clear()\n<\/code><\/pre>\n<p>\u6b64\u65b9\u6cd5\u5c06\u540c\u65f6\u6e05\u7a7a\u4e24\u4e2a\u5217\u8868\uff0c\u4f7f\u5b83\u4eec\u90fd\u53d8\u4e3a\u7a7a\u5217\u8868\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d: \u5728Python\u4e2d\u5220\u9664\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u91cd\u590d\u5143\u7d20\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u4f8b\u5982\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u5faa\u73af\u904d\u5386\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1105393,"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\/1105378"}],"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=1105378"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1105378\/revisions"}],"predecessor-version":[{"id":1105394,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1105378\/revisions\/1105394"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1105393"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1105378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1105378"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1105378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}