{"id":1073335,"date":"2025-01-08T11:24:36","date_gmt":"2025-01-08T03:24:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1073335.html"},"modified":"2025-01-08T11:24:38","modified_gmt":"2025-01-08T03:24:38","slug":"python-%e5%88%97%e8%a1%a8%e9%87%8c%e5%a6%82%e4%bd%95%e5%8e%bb%e9%99%a4%e5%ad%97%e7%ac%a6%e4%b8%b2-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1073335.html","title":{"rendered":"python \u5217\u8868\u91cc\u5982\u4f55\u53bb\u9664\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103253\/c58d42b9-2bb6-46d6-b90e-43dab43e947d.webp\" alt=\"python \u5217\u8868\u91cc\u5982\u4f55\u53bb\u9664\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> \u8981\u4ecePython\u5217\u8868\u4e2d\u53bb\u9664\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u4f8b\u5982\u5217\u8868\u63a8\u5bfc\u5f0f\u3001filter\u51fd\u6570\u3001for\u5faa\u73af\u7b49\u3002<strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528filter\u51fd\u6570\u3001\u4f7f\u7528for\u5faa\u73af\u3001\u4f7f\u7528\u5185\u5efa\u51fd\u6570isinstance<\/strong>\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u5f0f\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u975e\u5e38\u7b80\u6d01\u548c\u9ad8\u6548\u7684\u65b9\u5f0f\u3002\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u8fc7\u6ee4\u6389\u5217\u8868\u4e2d\u7684\u5b57\u7b26\u4e32\u5143\u7d20\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u975e\u5e38\u7b80\u6d01\u548c\u9ad8\u6548\u7684\u65b9\u5f0f\u6765\u8fc7\u6ee4\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, &#39;apple&#39;, 2, &#39;banana&#39;, 3, &#39;cherry&#39;]<\/p>\n<p>filtered_list = [x for x in original_list if not isinstance(x, str)]<\/p>\n<p>print(filtered_list)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868 <code>filtered_list<\/code>\uff0c\u5176\u4e2d\u5305\u542b\u4e86 <code>original_list<\/code> \u4e2d\u6240\u6709\u975e\u5b57\u7b26\u4e32\u7684\u5143\u7d20\u3002\u901a\u8fc7 <code>isinstance(x, str)<\/code> \u5224\u65ad <code>x<\/code> \u662f\u5426\u4e3a\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u4e0d\u662f\u5b57\u7b26\u4e32\u5c31\u5c06\u5176\u4fdd\u7559\u5728\u65b0\u7684\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528filter\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>filter<\/code> \u51fd\u6570\u662fPython\u5185\u5efa\u7684\u4e00\u4e2a\u9ad8\u9636\u51fd\u6570\uff0c\u5b83\u63a5\u6536\u4e00\u4e2a\u51fd\u6570\u548c\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u5bf9\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u51fd\u6570\u5e76\u6839\u636e\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff08True\u6216False\uff09\u51b3\u5b9a\u662f\u5426\u4fdd\u7559\u8be5\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, &#39;apple&#39;, 2, &#39;banana&#39;, 3, &#39;cherry&#39;]<\/p>\n<p>filtered_list = list(filter(lambda x: not isinstance(x, str), original_list))<\/p>\n<p>print(filtered_list)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>filter<\/code> \u51fd\u6570\u548c <code>lambda<\/code> \u8868\u8fbe\u5f0f\u6765\u8fc7\u6ee4\u6389\u6240\u6709\u5b57\u7b26\u4e32\u5143\u7d20\u3002<code>lambda x: not isinstance(x, str)<\/code> \u5b9a\u4e49\u4e86\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff0c\u7528\u4e8e\u5224\u65ad <code>x<\/code> \u662f\u5426\u4e3a\u975e\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528for\u5faa\u73af<\/p>\n<\/p>\n<p><p>\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u662f\u4e00\u79cd\u8f83\u4e3a\u76f4\u89c2\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5224\u65ad\u5176\u7c7b\u578b\uff0c\u5e76\u5c06\u975e\u5b57\u7b26\u4e32\u5143\u7d20\u6dfb\u52a0\u5230\u65b0\u7684\u5217\u8868\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, &#39;apple&#39;, 2, &#39;banana&#39;, 3, &#39;cherry&#39;]<\/p>\n<p>filtered_list = []<\/p>\n<p>for item in original_list:<\/p>\n<p>    if not isinstance(item, str):<\/p>\n<p>        filtered_list.append(item)<\/p>\n<p>print(filtered_list)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u904d\u5386 <code>original_list<\/code> \u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u901a\u8fc7 <code>isinstance(item, str)<\/code> \u5224\u65ad\u5176\u7c7b\u578b\uff0c\u5982\u679c\u4e0d\u662f\u5b57\u7b26\u4e32\u5c31\u5c06\u5176\u6dfb\u52a0\u5230 <code>filtered_list<\/code> \u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u5185\u5efa\u51fd\u6570isinstance<\/p>\n<\/p>\n<p><p><code>isinstance<\/code> \u51fd\u6570\u662fPython\u7684\u5185\u5efa\u51fd\u6570\uff0c\u7528\u4e8e\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u6307\u5b9a\u7684\u7c7b\u578b\u3002\u901a\u8fc7 <code>isinstance<\/code> \u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5224\u65ad\u5217\u8868\u4e2d\u7684\u5143\u7d20\u662f\u5426\u4e3a\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, &#39;apple&#39;, 2, &#39;banana&#39;, 3, &#39;cherry&#39;]<\/p>\n<p>filtered_list = [item for item in original_list if not isinstance(item, str)]<\/p>\n<p>print(filtered_list)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>isinstance<\/code> \u51fd\u6570\u6765\u5224\u65ad <code>item<\/code> \u662f\u5426\u4e3a\u5b57\u7b26\u4e32\uff0c\u5e76\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u8fc7\u6ee4\u6389\u6240\u6709\u5b57\u7b26\u4e32\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5339\u914d\u5de5\u5177\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u8fc7\u6ee4\u5217\u8868\u4e2d\u7684\u5b57\u7b26\u4e32\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>original_list = [1, &#39;apple&#39;, 2, &#39;banana&#39;, 3, &#39;cherry&#39;]<\/p>\n<p>filtered_list = [item for item in original_list if not re.match(r&#39;^[A-Za-z]+$&#39;, str(item))]<\/p>\n<p>print(filtered_list)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>re.match<\/code> \u51fd\u6570\u6765\u5339\u914d\u6240\u6709\u4ec5\u5305\u542b\u5b57\u6bcd\u7684\u5b57\u7b26\u4e32\uff0c\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u8fc7\u6ee4\u6389\u6240\u6709\u5339\u914d\u7684\u5b57\u7b26\u4e32\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528\u9012\u5f52\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u590d\u6742\u7684\u60c5\u51b5\u4e0b\uff0c\u4f8b\u5982\u5d4c\u5957\u5217\u8868\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u6765\u8fc7\u6ee4\u5217\u8868\u4e2d\u7684\u5b57\u7b26\u4e32\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def remove_strings(nested_list):<\/p>\n<p>    if isinstance(nested_list, list):<\/p>\n<p>        return [remove_strings(item) for item in nested_list if not isinstance(item, str)]<\/p>\n<p>    else:<\/p>\n<p>        return nested_list<\/p>\n<p>original_list = [1, &#39;apple&#39;, [2, &#39;banana&#39;, [3, &#39;cherry&#39;]]]<\/p>\n<p>filtered_list = remove_strings(original_list)<\/p>\n<p>print(filtered_list)  # \u8f93\u51fa: [1, [2, [3]]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u9012\u5f52\u51fd\u6570 <code>remove_strings<\/code>\uff0c\u7528\u4e8e\u904d\u5386\u5d4c\u5957\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u8fc7\u6ee4\u6389\u6240\u6709\u5b57\u7b26\u4e32\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/p>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u65b9\u5f0f\u6765\u5904\u7406\u5927\u6570\u636e\u96c6\uff0c\u901a\u8fc7\u751f\u6210\u5668\u8868\u8fbe\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5185\u5b58\u4e2d\u9010\u4e2a\u5904\u7406\u5143\u7d20\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u751f\u6210\u6574\u4e2a\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">original_list = [1, &#39;apple&#39;, 2, &#39;banana&#39;, 3, &#39;cherry&#39;]<\/p>\n<p>filtered_generator = (x for x in original_list if not isinstance(x, str))<\/p>\n<p>filtered_list = list(filtered_generator)<\/p>\n<p>print(filtered_list)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u6765\u8fc7\u6ee4\u6389\u6240\u6709\u5b57\u7b26\u4e32\u5143\u7d20\uff0c\u5e76\u901a\u8fc7 <code>list<\/code> \u51fd\u6570\u5c06\u751f\u6210\u5668\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u4f7f\u7528itertools\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>itertools<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u9ad8\u6548\u5904\u7406\u8fed\u4ee3\u5668\u7684\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>itertools<\/code> \u6a21\u5757\u4e2d\u7684 <code>filterfalse<\/code> \u51fd\u6570\u6765\u8fc7\u6ee4\u6389\u5217\u8868\u4e2d\u7684\u5b57\u7b26\u4e32\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>original_list = [1, &#39;apple&#39;, 2, &#39;banana&#39;, 3, &#39;cherry&#39;]<\/p>\n<p>filtered_list = list(itertools.filterfalse(lambda x: isinstance(x, str), original_list))<\/p>\n<p>print(filtered_list)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>itertools.filterfalse<\/code> \u51fd\u6570\u548c <code>lambda<\/code> \u8868\u8fbe\u5f0f\u6765\u8fc7\u6ee4\u6389\u6240\u6709\u5b57\u7b26\u4e32\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u4e5d\u3001\u4f7f\u7528numpy\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4f7f\u7528 <code>numpy<\/code> \u5e93\u6765\u5904\u7406\u6570\u503c\u6570\u636e\uff0c\u901a\u8fc7 <code>numpy<\/code> \u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u8fc7\u6ee4\u6570\u7ec4\u4e2d\u7684\u5b57\u7b26\u4e32\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>original_array = np.array([1, &#39;apple&#39;, 2, &#39;banana&#39;, 3, &#39;cherry&#39;], dtype=object)<\/p>\n<p>filtered_array = np.array([x for x in original_array if not isinstance(x, str)])<\/p>\n<p>print(filtered_array)  # \u8f93\u51fa: [1 2 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>numpy<\/code> \u5e93\u6765\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u4e32\u5143\u7d20\u7684\u6570\u7ec4\uff0c\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u8fc7\u6ee4\u6389\u6240\u6709\u5b57\u7b26\u4e32\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u5341\u3001\u4f7f\u7528pandas\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0c<code>pandas<\/code> \u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u901a\u8fc7 <code>pandas<\/code> \u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u8fc7\u6ee4\u6570\u636e\u6846\u4e2d\u7684\u5b57\u7b26\u4e32\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>original_series = pd.Series([1, &#39;apple&#39;, 2, &#39;banana&#39;, 3, &#39;cherry&#39;])<\/p>\n<p>filtered_series = original_series[~original_series.apply(lambda x: isinstance(x, str))]<\/p>\n<p>print(filtered_series)  # \u8f93\u51fa: 0    1<\/p>\n<p>                        #       2    2<\/p>\n<p>                        #       4    3<\/p>\n<p>                        #       dtype: object<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>pandas<\/code> \u5e93\u6765\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u4e32\u5143\u7d20\u7684 <code>Series<\/code>\uff0c\u901a\u8fc7 <code>apply<\/code> \u51fd\u6570\u548c <code>lambda<\/code> \u8868\u8fbe\u5f0f\u6765\u8fc7\u6ee4\u6389\u6240\u6709\u5b57\u7b26\u4e32\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4ecePython\u5217\u8868\u4e2d\u53bb\u9664\u5b57\u7b26\u4e32\u5143\u7d20\u3002<strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528filter\u51fd\u6570\u3001\u4f7f\u7528for\u5faa\u73af\u3001\u4f7f\u7528\u5185\u5efa\u51fd\u6570isinstance<\/strong>\u662f\u51e0\u79cd\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u9ad8\u6548\u5730\u5904\u7406\u6570\u636e\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u5217\u8868\u4e2d\u5220\u9664\u7279\u5b9a\u7684\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u8981\u5220\u9664Python\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u4f1a\u67e5\u627e\u5e76\u5220\u9664\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5217\u8868<code>my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;]<\/code>\uff0c\u5e76\u60f3\u5220\u9664<code>&#39;banana&#39;<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>my_list.remove(&#39;banana&#39;)<\/code>\u6765\u5b9e\u73b0\u3002\u6ce8\u610f\uff0c\u5982\u679c\u5217\u8868\u4e2d\u4e0d\u5b58\u5728\u8be5\u5b57\u7b26\u4e32\uff0c\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u60f3\u5220\u9664\u5217\u8868\u4e2d\u6240\u6709\u7684\u6307\u5b9a\u5b57\u7b26\u4e32\uff0c\u8be5\u600e\u4e48\u505a\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216<code>filter()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u5305\u542b\u6240\u6709\u4e0d\u9700\u8981\u5220\u9664\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u60f3\u4ece<code>my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;banana&#39;, &#39;orange&#39;]<\/code>\u4e2d\u5220\u9664\u6240\u6709\u7684<code>&#39;banana&#39;<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>my_list = [item for item in my_list if item != &#39;banana&#39;]<\/code>\uff0c\u8fd9\u6837\u5c31\u80fd\u4fdd\u7559\u5176\u4ed6\u5143\u7d20\u800c\u5220\u9664\u6240\u6709\u7684<code>&#39;banana&#39;<\/code>\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u5217\u8868\u65f6\uff0c\u5982\u4f55\u907f\u514d\u5220\u9664\u64cd\u4f5c\u5bf9\u539f\u5217\u8868\u7684\u5f71\u54cd\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u5728\u5220\u9664\u5b57\u7b26\u4e32\u65f6\u4e0d\u5f71\u54cd\u539f\u5217\u8868\uff0c\u53ef\u4ee5\u521b\u5efa\u539f\u5217\u8868\u7684\u526f\u672c\u3002\u53ef\u4ee5\u4f7f\u7528\u5207\u7247<code>my_list_copy = my_list[:]<\/code>\u6216<code>copy()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u7136\u540e\uff0c\u5728\u526f\u672c\u4e0a\u6267\u884c\u5220\u9664\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c<code>my_list_copy.remove(&#39;banana&#39;)<\/code>\u4e0d\u4f1a\u6539\u53d8\u539f\u59cb\u5217\u8868<code>my_list<\/code>\uff0c\u8fd9\u6837\u4f60\u53ef\u4ee5\u4fdd\u6301\u539f\u59cb\u6570\u636e\u7684\u5b8c\u6574\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u4ecePython\u5217\u8868\u4e2d\u53bb\u9664\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u4f8b\u5982\u5217\u8868\u63a8\u5bfc\u5f0f\u3001filter\u51fd\u6570\u3001for\u5faa\u73af\u7b49\u3002\u4f7f\u7528\u5217\u8868\u63a8 [&hellip;]","protected":false},"author":3,"featured_media":1073342,"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\/1073335"}],"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=1073335"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073335\/revisions"}],"predecessor-version":[{"id":1073344,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073335\/revisions\/1073344"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1073342"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1073335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1073335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1073335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}