{"id":1000425,"date":"2024-12-27T09:50:00","date_gmt":"2024-12-27T01:50:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1000425.html"},"modified":"2024-12-27T09:50:03","modified_gmt":"2024-12-27T01:50:03","slug":"python%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e5%a4%9a%e4%b8%aa%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1000425.html","title":{"rendered":"python\u5982\u4f55\u5220\u9664\u591a\u4e2a\u5143\u7d20"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075014\/e38f8bad-f8b9-4cc1-bcff-675258b044fc.webp\" alt=\"python\u5982\u4f55\u5220\u9664\u591a\u4e2a\u5143\u7d20\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5220\u9664\u591a\u4e2a\u5143\u7d20\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u4f7f\u7528filter()\u51fd\u6570\u3001\u4f7f\u7528\u5faa\u73af\u548cremove()\u65b9\u6cd5\u3002\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u6761\u4ef6\u8fc7\u6ee4\u51fa\u4e0d\u9700\u8981\u5220\u9664\u7684\u5143\u7d20\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u5e38\u7528\u7684Python\u7279\u6027\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u7b80\u6d01\u7684\u8bed\u6cd5\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002\u6bd4\u5982\u8bf4\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u5217\u8868<code>my_list<\/code>\uff0c\u5e76\u4e14\u60f3\u8981\u5220\u9664\u5176\u4e2d\u7684\u591a\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5, 6]<\/p>\n<p>elements_to_remove = [2, 4, 6]<\/p>\n<p>my_list = [element for element in my_list if element not in elements_to_remove]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u6d01\u3001\u6613\u8bfb\uff0c\u6548\u7387\u4e5f\u8f83\u9ad8\uff0c\u56e0\u4e3a\u5b83\u53ea\u904d\u5386\u4e00\u6b21\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5728Python\u4e2d\u5220\u9664\u591a\u4e2a\u5143\u7d20\u7684\u4e0d\u540c\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u5220\u9664\u591a\u4e2a\u5143\u7d20\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u8fc7\u6ee4\u51fa\u9700\u8981\u4fdd\u7559\u7684\u5143\u7d20\uff0c\u4ece\u800c\u5b9e\u73b0\u5220\u9664\u4e0d\u9700\u8981\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>\u5217\u8868\u89e3\u6790\u7684\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u7684\u57fa\u672c\u7ed3\u6784\u662f<code>[expression for item in iterable if condition]<\/code>\u3002\u901a\u8fc7\u8fd9\u79cd\u7ed3\u6784\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5224\u65ad\uff0c\u5e76\u6839\u636e\u7ed3\u679c\u51b3\u5b9a\u662f\u5426\u5c06\u5176\u4fdd\u7559\u5728\u65b0\u7684\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [10, 20, 30, 40, 50]<\/p>\n<p>elements_to_remove = [20, 40]<\/p>\n<p>filtered_list = [x for x in original_list if x not in elements_to_remove]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>filtered_list<\/code>\u5c06\u4f1a\u662f<code>[10, 30, 50]<\/code>\uff0c\u56e0\u4e3a<code>20<\/code>\u548c<code>40<\/code>\u88ab\u6392\u9664\u5728\u5916\u3002<\/p>\n<\/p>\n<p><h4>\u5217\u8868\u89e3\u6790\u7684\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u7b80\u6d01\u660e\u4e86<\/strong>\uff1a\u4ee3\u7801\u957f\u5ea6\u77ed\uff0c\u903b\u8f91\u6e05\u6670\u3002<\/li>\n<li><strong>\u6027\u80fd\u4f18\u8d8a<\/strong>\uff1a\u5bf9\u4e8e\u4e2d\u5c0f\u89c4\u6a21\u7684\u6570\u636e\uff0c\u6027\u80fd\u8868\u73b0\u826f\u597d\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u5927\u89c4\u6a21\u6570\u636e\u65f6\u5185\u5b58\u5360\u7528\u8f83\u9ad8<\/strong>\uff1a\u56e0\u4e3a\u5b83\u5728\u5185\u5b58\u4e2d\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u5217\u8868\u3002<\/li>\n<li><strong>\u5bf9\u4e8e\u590d\u6742\u6761\u4ef6\u4e0d\u591f\u76f4\u89c2<\/strong>\uff1a\u5982\u679c\u6761\u4ef6\u8f83\u590d\u6742\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cd\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528filter()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>filter()<\/code>\u51fd\u6570\u4e5f\u662fPython\u63d0\u4f9b\u7684\u4e00\u4e2a\u7528\u4e8e\u8fc7\u6ee4\u5e8f\u5217\u7684\u65b9\u6cd5\u3002\u4e0e\u5217\u8868\u89e3\u6790\u7c7b\u4f3c\uff0c\u5b83\u53ef\u4ee5\u6839\u636e\u7ed9\u5b9a\u7684\u6761\u4ef6\u4fdd\u7559\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>filter()\u7684\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>filter(function, iterable)<\/code>\u7684\u5de5\u4f5c\u539f\u7406\u662f\u5c06<code>iterable<\/code>\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u4f20\u9012\u7ed9<code>function<\/code>\uff0c\u5982\u679c\u8fd4\u56de<code>True<\/code>\uff0c\u5219\u4fdd\u7559\u8be5\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def should_keep(x):<\/p>\n<p>    return x not in [20, 40]<\/p>\n<p>original_list = [10, 20, 30, 40, 50]<\/p>\n<p>filtered_list = list(filter(should_keep, original_list))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837<code>filtered_list<\/code>\u5c06\u4f1a\u662f<code>[10, 30, 50]<\/code>\u3002<\/p>\n<\/p>\n<p><h4>filter()\u7684\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u51fd\u6570\u5316\u7f16\u7a0b\u98ce\u683c<\/strong>\uff1a\u5bf9\u4e8e\u4e60\u60ef\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u66f4\u52a0\u81ea\u7136\u3002<\/li>\n<li><strong>\u60f0\u6027\u8ba1\u7b97<\/strong>\uff1a<code>filter()<\/code>\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u60f0\u6027\u5730\u8fdb\u884c\u8ba1\u7b97\uff0c\u9002\u5408\u5904\u7406\u5927\u6570\u636e\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u4ee3\u7801\u53ef\u8bfb\u6027<\/strong>\uff1a\u5bf9\u4e8e\u4e0d\u719f\u6089\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u5f00\u53d1\u8005\uff0c\u53ef\u80fd\u4e0d\u5982\u5217\u8868\u89e3\u6790\u76f4\u89c2\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5faa\u73af\u548cremove()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>remove()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u4ece\u5217\u8868\u4e2d\u5220\u9664\u67d0\u4e2a\u5143\u7d20\u3002\u901a\u8fc7\u5faa\u73af\uff0c\u6211\u4eec\u53ef\u4ee5\u9010\u4e00\u5220\u9664\u591a\u4e2a\u6307\u5b9a\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>remove()\u7684\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p><code>remove(x)<\/code>\u65b9\u6cd5\u4f1a\u4ece\u5217\u8868\u4e2d\u5220\u9664\u7b2c\u4e00\u4e2a\u503c\u4e3a<code>x<\/code>\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 40, 50]<\/p>\n<p>elements_to_remove = [20, 40]<\/p>\n<p>for element in elements_to_remove:<\/p>\n<p>    if element in my_list:<\/p>\n<p>        my_list.remove(element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6267\u884c\u540e\uff0c<code>my_list<\/code>\u5c06\u53d8\u4e3a<code>[10, 30, 50]<\/code>\u3002<\/p>\n<\/p>\n<p><h4>remove()\u7684\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u9010\u4e00\u5904\u7406<\/strong>\uff1a\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u5904\u7406\u6bcf\u4e2a\u5143\u7d20\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u6548\u7387\u8f83\u4f4e<\/strong>\uff1a\u5bf9\u4e8e\u5927\u7684\u5217\u8868\u6216\u8005\u9700\u8981\u5220\u9664\u7684\u5143\u7d20\u8f83\u591a\u65f6\uff0c\u6548\u7387\u8f83\u4f4e\uff0c\u56e0\u4e3a\u6bcf\u6b21\u5220\u9664\u64cd\u4f5c\u90fd\u9700\u8981\u904d\u5386\u5217\u8868\u3002<\/li>\n<li><strong>\u53ef\u80fd\u5f15\u53d1\u9519\u8bef<\/strong>\uff1a\u5982\u679c\u5143\u7d20\u4e0d\u5728\u5217\u8868\u4e2d\uff0c\u8c03\u7528<code>remove()<\/code>\u4f1a\u5f15\u53d1<code>ValueError<\/code>\uff0c\u9700\u8981\u989d\u5916\u7684\u9519\u8bef\u5904\u7406\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u4f7f\u7528\u96c6\u5408\u8fdb\u884c\u5dee\u96c6\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\uff08<code>set<\/code>\uff09\u662fPython\u4e2d\u7684\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u652f\u6301\u8bb8\u591a\u96c6\u5408\u64cd\u4f5c\uff0c\u5982\u5e76\u96c6\u3001\u4ea4\u96c6\u548c\u5dee\u96c6\u3002\u901a\u8fc7\u96c6\u5408\u7684\u5dee\u96c6\u64cd\u4f5c\uff0c\u6211\u4eec\u53ef\u4ee5\u9ad8\u6548\u5730\u4ece\u5217\u8868\u4e2d\u5220\u9664\u591a\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>\u5dee\u96c6\u64cd\u4f5c\u7684\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u7684\u5dee\u96c6\u8fd0\u7b97\u7b26<code>-<\/code>\u6765\u79fb\u9664\u591a\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 40, 50]<\/p>\n<p>elements_to_remove = {20, 40}<\/p>\n<p>result_list = list(set(my_list) - elements_to_remove)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>result_list<\/code>\u5c06\u662f<code>[10, 30, 50]<\/code>\u3002<\/p>\n<\/p>\n<p><h4>\u5dee\u96c6\u64cd\u4f5c\u7684\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u9ad8\u6548\u5904\u7406<\/strong>\uff1a\u5bf9\u4e8e\u9700\u8981\u5220\u9664\u7684\u5143\u7d20\u5f88\u591a\u65f6\uff0c\u96c6\u5408\u7684\u64cd\u4f5c\u975e\u5e38\u9ad8\u6548\u3002<\/li>\n<li><strong>\u7b80\u6d01<\/strong>\uff1a\u4ee3\u7801\u7b80\u6d01\u660e\u4e86\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u65e0\u5e8f<\/strong>\uff1a\u96c6\u5408\u662f\u65e0\u5e8f\u7684\uff0c\u5982\u679c\u539f\u5217\u8868\u7684\u987a\u5e8f\u5f88\u91cd\u8981\uff0c\u53ef\u80fd\u9700\u8981\u989d\u5916\u7684\u6b65\u9aa4\u6765\u7ef4\u62a4\u987a\u5e8f\u3002<\/li>\n<li><strong>\u6570\u636e\u7c7b\u578b\u9650\u5236<\/strong>\uff1a\u96c6\u5408\u8981\u6c42\u5143\u7d20\u662f\u53ef\u54c8\u5e0c\u7684\uff0c\u56e0\u6b64\u4e0d\u80fd\u76f4\u63a5\u7528\u4e8e\u5217\u8868\u4e2d\u5305\u542b\u4e0d\u53ef\u54c8\u5e0c\u5bf9\u8c61\u7684\u60c5\u51b5\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u5de5\u5177\u7528\u4e8e\u64cd\u4f5c\u5927\u578b\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u3002\u5982\u679c\u4f60\u6b63\u5728\u5904\u7406\u6570\u503c\u6570\u636e\uff0cNumPy\u53ef\u80fd\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h4>NumPy\u7684\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u975e\u5e38\u9ad8\u6548\u7684\u65b9\u6cd5\u7528\u4e8e\u5904\u7406\u6570\u7ec4\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u6765\u8fc7\u6ee4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>my_array = np.array([10, 20, 30, 40, 50])<\/p>\n<p>elements_to_remove = np.array([20, 40])<\/p>\n<p>filtered_array = my_array[~np.isin(my_array, elements_to_remove)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>filtered_array<\/code>\u5c06\u662f<code>array([10, 30, 50])<\/code>\u3002<\/p>\n<\/p>\n<p><h4>NumPy\u7684\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u9ad8\u6548<\/strong>\uff1a\u5bf9\u4e8e\u6570\u503c\u6570\u636e\uff0cNumPy\u63d0\u4f9b\u4e86\u6781\u9ad8\u7684\u6548\u7387\u3002<\/li>\n<li><strong>\u5f3a\u5927\u7684\u529f\u80fd<\/strong>\uff1aNumPy\u63d0\u4f9b\u4e86\u8bb8\u591a\u5f3a\u5927\u7684\u529f\u80fd\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u9002\u7528\u8303\u56f4\u6709\u9650<\/strong>\uff1aNumPy\u4e3b\u8981\u7528\u4e8e\u6570\u503c\u8ba1\u7b97\uff0c\u4e0d\u9002\u7528\u4e8e\u6240\u6709\u7c7b\u578b\u7684\u6570\u636e\u3002<\/li>\n<li><strong>\u5b66\u4e60\u6210\u672c<\/strong>\uff1a\u9700\u8981\u4e86\u89e3NumPy\u7684\u57fa\u672c\u6982\u5ff5\u548c\u7528\u6cd5\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5220\u9664\u591a\u4e2a\u5143\u7d20\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u9002\u5408\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002<strong>\u5217\u8868\u89e3\u6790\u548cfilter()\u51fd\u6570\u9002\u5408\u4e2d\u5c0f\u89c4\u6a21\u7684\u6570\u636e\u5904\u7406\uff0c\u63d0\u4f9b\u4e86\u7b80\u6d01\u7684\u8bed\u6cd5\u548c\u826f\u597d\u7684\u6027\u80fd\u3002<\/strong>\u5bf9\u4e8e\u5927\u578b\u6570\u636e\u96c6\u6216\u9700\u8981\u8fdb\u884c\u590d\u6742\u6570\u636e\u64cd\u4f5c\u7684\u573a\u666f\uff0cNumPy\u548c\u96c6\u5408\u64cd\u4f5c\u63d0\u4f9b\u4e86\u66f4\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002\u5faa\u73af\u548cremove()\u65b9\u6cd5\u867d\u7136\u7b80\u5355\uff0c\u4f46\u5728\u6027\u80fd\u548c\u5b89\u5168\u6027\u4e0a\u6709\u6240\u4e0d\u8db3\uff0c\u9700\u8981\u8c28\u614e\u4f7f\u7528\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u540c\u65f6\u5220\u9664\u591a\u4e2a\u5217\u8868\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u6392\u9664\u6389\u9700\u8981\u5220\u9664\u7684\u5143\u7d20\u3002\u6bd4\u5982\uff0c\u5982\u679c\u4f60\u60f3\u4ece\u5217\u8868\u4e2d\u5220\u9664\u591a\u4e2a\u7279\u5b9a\u7684\u503c\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a  <\/p>\n<pre><code class=\"language-python\">original_list = [1, 2, 3, 4, 5, 6]\nvalues_to_remove = [2, 4]\nnew_list = [item for item in original_list if item not in values_to_remove]\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u4f60\u4f1a\u5f97\u5230\u4e00\u4e2a\u53ea\u5305\u542b\u672a\u5220\u9664\u5143\u7d20\u7684\u65b0\u5217\u8868 <code>[1, 3, 5, 6]<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u5207\u7247\u5220\u9664\u591a\u4e2a\u5143\u7d20\u7684\u65b9\u5f0f\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5207\u7247\u53ef\u4ee5\u7528\u6765\u4ece\u5217\u8868\u4e2d\u5220\u9664\u4e00\u6bb5\u8fde\u7eed\u7684\u5143\u7d20\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u5217\u8868\uff0c\u60f3\u8981\u5220\u9664\u7d22\u5f152\u52304\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u6765\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">original_list = [10, 20, 30, 40, 50, 60]\ndel original_list[2:5]\n<\/code><\/pre>\n<p>\u7ed3\u679c\u662f <code>[10, 20, 60]<\/code>\uff0c\u56e0\u4e3a\u7d22\u5f152\u52304\uff0830\u300140\u300150\uff09\u88ab\u5220\u9664\u4e86\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528<code>filter<\/code>\u51fd\u6570\u5728Python\u4e2d\u5220\u9664\u591a\u4e2a\u5143\u7d20\uff1f<\/strong><br \/><code>filter<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u8fc7\u6ee4\u51fa\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u4ece\u800c\u8fbe\u5230\u5220\u9664\u7684\u6548\u679c\u3002\u5982\u679c\u60f3\u8981\u5220\u9664\u7279\u5b9a\u5143\u7d20\uff0c\u53ef\u4ee5\u7ed3\u5408\u4e00\u4e2a\u51fd\u6570\u6765\u4f7f\u7528\uff1a  <\/p>\n<pre><code class=\"language-python\">original_list = [1, 2, 3, 4, 5, 6]\nvalues_to_remove = {2, 4}\nfiltered_list = list(filter(lambda x: x not in values_to_remove, original_list))\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<code>filtered_list<\/code> \u5c06\u53ea\u5305\u542b <code>[1, 3, 5, 6]<\/code>\uff0c\u5b9e\u73b0\u4e86\u5220\u9664\u7684\u76ee\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5220\u9664\u591a\u4e2a\u5143\u7d20\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u4f7f\u7528filter()\u51fd\u6570\u3001\u4f7f\u7528\u5faa\u73af\u548cremove()\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1000439,"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\/1000425"}],"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=1000425"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1000425\/revisions"}],"predecessor-version":[{"id":1000441,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1000425\/revisions\/1000441"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1000439"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1000425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1000425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1000425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}