{"id":177002,"date":"2024-05-08T19:21:56","date_gmt":"2024-05-08T11:21:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/177002.html"},"modified":"2024-05-08T19:22:03","modified_gmt":"2024-05-08T11:22:03","slug":"python%e5%8e%bb%e9%99%a4%e4%b8%80%e4%b8%aa%e5%88%97%e8%a1%a8%e4%b8%ad%e7%9a%84%e7%b4%a0%e6%95%b0%e7%9a%84%e6%80%9d%e8%b7%af%e8%af%a5%e6%80%8e%e4%b9%88%e5%86%99","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/177002.html","title":{"rendered":"python\u53bb\u9664\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u7d20\u6570\u7684\u601d\u8def\u8be5\u600e\u4e48\u5199"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27060741\/85ca5f78-7628-442c-a238-e9eaa8ad85b3.webp\" alt=\"python\u53bb\u9664\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u7d20\u6570\u7684\u601d\u8def\u8be5\u600e\u4e48\u5199\" \/><\/p>\n<p><p>\u53bb\u9664\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u7d20\u6570\uff0c<strong>\u4e3b\u8981\u6d89\u53ca\u5230\u4e09\u4e2a\u6b65\u9aa4\uff1a\u8bc6\u522b\u7d20\u6570\u3001\u904d\u5386\u5217\u8868\u4ee5\u53ca\u79fb\u9664\u7d20\u6570<\/strong>\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u51fd\u6570\u6765\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u7d20\u6570\uff0c\u8fd9\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u8fd9\u4e2a\u6570\u662f\u5426\u53ea\u80fd\u88ab1\u548c\u5b83\u81ea\u8eab\u6574\u9664\u6765\u5b8c\u6210\u3002\u63a5\u7740\uff0c\u904d\u5386\u7ed9\u5b9a\u7684\u5217\u8868\uff0c\u5e76\u5e94\u7528\u4e4b\u524d\u7684\u7d20\u6570\u5224\u65ad\u51fd\u6570\u6765\u8bc6\u522b\u5217\u8868\u4e2d\u7684\u7d20\u6570\u3002\u6700\u540e\uff0c\u5c06\u8bc6\u522b\u51fa\u7684\u7d20\u6570\u4ece\u5217\u8868\u4e2d\u79fb\u9664\u3002<strong>\u6700\u5173\u952e\u7684\u90e8\u5206\u662f\u7cbe\u786e\u9ad8\u6548\u5730\u8bc6\u522b\u7d20\u6570<\/strong>\uff0c\u8fd9\u4e0d\u4ec5\u5173\u7cfb\u5230\u7a0b\u5e8f\u7684\u6548\u7387\uff0c\u4e5f\u662f\u53bb\u9664\u64cd\u4f5c\u6210\u529f\u4e0e\u5426\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7d20\u6570\u7684\u8bc6\u522b<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8bc6\u522b\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u7d20\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\u5b9e\u73b0\u3002\u8fd9\u4e2a\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u6574\u6570\u4f5c\u4e3a\u8f93\u5165\uff0c\u7136\u540e\u901a\u8fc7\u904d\u5386\u4ece2\u5230\u8fd9\u4e2a\u6570\u7684\u5e73\u65b9\u6839\uff08\u6216\u4f7f\u7528\u5176\u5b83\u66f4\u9ad8\u6548\u7684\u7b97\u6cd5\uff09\u4e4b\u95f4\u7684\u6240\u6709\u6574\u6570\uff0c\u68c0\u67e5\u8f93\u5165\u7684\u6570\u662f\u5426\u80fd\u88ab\u8fd9\u4e9b\u6574\u6570\u6574\u9664\u3002\u5982\u679c\u80fd\u88ab\u6574\u9664\uff0c\u5219\u8fd9\u4e2a\u6570\u4e0d\u662f\u7d20\u6570\uff1b\u5426\u5219\uff0c\u5b83\u5c31\u662f\u7d20\u6570\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u904d\u5386\u4f18\u5316<\/strong>\uff1a\u904d\u5386\u7684\u7ec8\u6b62\u6761\u4ef6\u53ef\u4ee5\u4f18\u5316\u5230\u6570\u7684\u5e73\u65b9\u6839\uff0c\u56e0\u4e3a\u5982\u679c\u4e00\u4e2a\u6570n\u662f\u5408\u6570\uff0c\u90a3\u4e48\u5b83\u7684\u4e00\u4e2a\u56e0\u6570\u5fc5\u5b9a\u5c0f\u4e8e\u6216\u7b49\u4e8e\u221an\u3002\u8fd9\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u904d\u5386\uff0c\u63d0\u9ad8\u5224\u65ad\u6548\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5229\u75286x\u00b11\u89c4\u5219<\/strong>\uff1a\u8fdb\u4e00\u6b65\u4f18\u5316\u7684\u65b9\u6cd5\u662f\uff0c\u9664\u4e86\u68c0\u67e52\u548c3\u4e4b\u5916\uff0c\u53ea\u68c0\u67e5\u5f62\u5f0f\u4e3a6x\u00b11\u7684\u6570\uff0c\u56e0\u4e3a\u7d20\u6570\u9664\u4e862\u548c3\u4e4b\u5916\uff0c\u5176\u5b83\u7684\u7d20\u6570\u4e00\u5b9a\u57286\u7684\u500d\u6570\u7684\u4e24\u4fa7\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u904d\u5386\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u904d\u5386\u5217\u8868\u662f\u53bb\u9664\u5217\u8868\u4e2d\u7d20\u6570\u7684\u7b2c\u4e8c\u6b65\u9aa4\u3002\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u6216while\u5faa\u73af\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u5bf9\u5176\u5e94\u7528\u4e4b\u524d\u63d0\u5230\u7684\u7d20\u6570\u5224\u65ad\u51fd\u6570\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u4f7f\u7528for\u5faa\u73af<\/strong>\uff1afor\u5faa\u73af\u662f\u6700\u76f4\u63a5\u7684\u904d\u5386\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u9010\u4e2a\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u5e76\u8fdb\u884c\u5fc5\u8981\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fb9\u904d\u5386\u8fb9\u4fee\u6539\u7684\u6ce8\u610f\u4e8b\u9879<\/strong>\uff1a\u5728\u904d\u5386\u5217\u8868\u7684\u540c\u65f6\u8fdb\u884c\u5220\u9664\u64cd\u4f5c\uff0c\u8981\u6ce8\u610f\u53ef\u80fd\u4f1a\u56e0\u4e3a\u5217\u8868\u957f\u5ea6\u7684\u53d8\u5316\u5bfc\u81f4\u904d\u5386\u4e2d\u51fa\u73b0\u8df3\u8fc7\u6216\u91cd\u590d\u8bbf\u95ee\u7684\u95ee\u9898\u3002\u4e00\u79cd\u89e3\u51b3\u65b9\u6cd5\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u53ea\u4fdd\u5b58\u975e\u7d20\u6570\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u79fb\u9664\u7d20\u6570<\/h3>\n<\/p>\n<p><p>\u79fb\u9664\u7d20\u6570\u7684\u64cd\u4f5c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u6765\u5b8c\u6210\uff0c\u8fd9\u4e2a\u65b0\u5217\u8868\u53ea\u5305\u542b\u539f\u5217\u8868\u4e2d\u7684\u975e\u7d20\u6570\u5143\u7d20\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u76f4\u63a5\u5728\u904d\u5386\u8fc7\u7a0b\u4e2d\u4fee\u6539\u5217\u8868\u5e26\u6765\u7684\u6f5c\u5728\u95ee\u9898\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u521b\u5efa\u65b0\u5217\u8868<\/strong>\uff1a\u901a\u8fc7\u904d\u5386\u539f\u5217\u8868\uff0c\u5c06\u975e\u7d20\u6570\u7684\u5143\u7d20\u6dfb\u52a0\u5230\u65b0\u5217\u8868\u4e2d\uff0c\u8fd9\u6837\u4e0d\u4ec5\u907f\u514d\u4e86\u4fee\u6539\u539f\u5217\u8868\u7684\u95ee\u9898\uff0c\u4e5f\u4f7f\u5f97\u8fc7\u7a0b\u66f4\u4e3a\u7b80\u6d01\u660e\u4e86\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5229\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\uff1aPython\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5217\u8868\u63a8\u5bfc\u5f0f\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u4e00\u884c\u4ee3\u7801\u5b9e\u73b0\u904d\u5386\u548c\u6761\u4ef6\u7b5b\u9009\uff0c\u4ece\u800c\u5feb\u901f\u751f\u6210\u4e00\u4e2a\u53ea\u5305\u542b\u975e\u7d20\u6570\u5143\u7d20\u7684\u65b0\u5217\u8868\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u603b\u7ed3\u4e0e\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u53bb\u9664\u5217\u8868\u4e2d\u7684\u7d20\u6570\u6d89\u53ca\u5230\u8bc6\u522b\u7d20\u6570\u3001\u904d\u5386\u5217\u8868\u4ee5\u53ca\u79fb\u9664\u7d20\u6570\u4e09\u4e2a\u5173\u952e\u6b65\u9aa4\u3002\u901a\u8fc7\u4f18\u5316\u7d20\u6570\u7684\u8bc6\u522b\u7b97\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6574\u4e2a\u8fc7\u7a0b\u7684\u6548\u7387\u3002\u540c\u65f6\uff0c\u5229\u7528Python\u7684\u9ad8\u7ea7\u7279\u6027\uff0c\u5982\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\uff0c\u4f7f\u5f97\u53bb\u9664\u64cd\u4f5c\u66f4\u52a0\u9ad8\u6548\u548c\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><p>\u4e3a\u52a0\u6df1\u7406\u89e3\uff0c\u4ee5\u4e0b\u63d0\u4f9b\u4e00\u4e2a\u7b80\u5355\u7684Python\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_prime(n):<\/p>\n<p>    if n &lt;= 1:<\/p>\n<p>        return False<\/p>\n<p>    if n &lt;= 3:<\/p>\n<p>        return True<\/p>\n<p>    if n % 2 == 0 or n % 3 == 0:<\/p>\n<p>        return False<\/p>\n<p>    i = 5<\/p>\n<p>    while i * i &lt;= n:<\/p>\n<p>        if n % i == 0 or n % (i + 2) == 0:<\/p>\n<p>            return False<\/p>\n<p>        i += 6<\/p>\n<p>    return True<\/p>\n<p>def remove_primes(lst):<\/p>\n<p>    return [num for num in lst if not is_prime(num)]<\/p>\n<h2><strong>\u793a\u4f8b\u5217\u8868<\/strong><\/h2>\n<p>example_list = [2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<h2><strong>\u79fb\u9664\u7d20\u6570\u540e\u7684\u5217\u8868<\/strong><\/h2>\n<p>filtered_list = remove_primes(example_list)<\/p>\n<p>print(filtered_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u6bb5\u4ee3\u7801\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>is_prime<\/code>\u51fd\u6570\uff0c\u7528\u4e8e\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u7d20\u6570\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>remove_primes<\/code>\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u5217\u8868\uff0c\u8fd4\u56de\u4e00\u4e2a\u53bb\u9664\u4e86\u6240\u6709\u7d20\u6570\u540e\u7684\u65b0\u5217\u8868\u3002\u6700\u540e\u901a\u8fc7\u4e00\u4e2a\u793a\u4f8b\u5217\u8868\u6f14\u793a\u4e86\u8fd9\u4e2a\u8fc7\u7a0b\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u5217\u8868\u4e2d\u7684\u7d20\u6570\u88ab\u9ad8\u6548\u53bb\u9664\uff0c\u800c\u4e14\u6574\u4e2a\u8fc7\u7a0b\u7b80\u6d01\u6613\u61c2\uff0c\u4f53\u73b0\u4e86Python\u7f16\u7a0b\u4e4b\u7f8e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u600e\u6837\u4f7f\u7528Python\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u5c06\u5217\u8868\u4e2d\u7684\u7d20\u6570\u79fb\u9664\uff1f<\/strong><\/p>\n<p>\u4e3b\u8981\u601d\u8def\u662f\u5b9a\u4e49\u4e00\u4e2a\u8f85\u52a9\u51fd\u6570\u6765\u5224\u65ad\u4e00\u4e2a\u6570\u5b57\u662f\u5426\u4e3a\u7d20\u6570\uff0c\u7136\u540e\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8fc7\u6ee4\u6389\u5217\u8868\u4e2d\u7684\u7d20\u6570\u5143\u7d20\u3002<\/p>\n<pre><code class=\"language-python\">def is_prime(num):\n    &quot;&quot;&quot;\u5224\u65ad\u4e00\u4e2a\u6570\u5b57\u662f\u5426\u4e3a\u7d20\u6570&quot;&quot;&quot;\n    if num &lt; 2:\n        return False\n    for i in range(2, int(num ** 0.5) + 1):\n        if num % i == 0:\n            return False\n    return True\n\ndef remove_primes(lst):\n    &quot;&quot;&quot;\u79fb\u9664\u5217\u8868\u4e2d\u7684\u7d20\u6570\u5143\u7d20&quot;&quot;&quot;\n    return [num for num in lst if not is_prime(num)]\n<\/code><\/pre>\n<p><strong>2. \u5982\u4f55\u7f16\u5199\u4e00\u4e2aPython\u7a0b\u5e8f\uff0c\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u5220\u9664\u6240\u6709\u7684\u7d20\u6570\uff1f<\/strong><\/p>\n<p>\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5faa\u73af\u548c\u6761\u4ef6\u5224\u65ad\u6765\u5b9e\u73b0\u3002\u6211\u4eec\u53ef\u4ee5\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u4f7f\u7528\u4e00\u4e2a\u8f85\u52a9\u51fd\u6570\u6765\u5224\u65ad\u662f\u5426\u4e3a\u7d20\u6570\u3002\u5982\u679c\u662f\u7d20\u6570\u5219\u53ef\u4ee5\u5c06\u5176\u4ece\u5217\u8868\u4e2d\u5220\u9664\u3002<\/p>\n<pre><code class=\"language-python\">def is_prime(num):\n    &quot;&quot;&quot;\u5224\u65ad\u4e00\u4e2a\u6570\u5b57\u662f\u5426\u4e3a\u7d20\u6570&quot;&quot;&quot;\n    if num &lt; 2:\n        return False\n    for i in range(2, int(num ** 0.5) + 1):\n        if num % i == 0:\n            return False\n    return True\n\ndef remove_primes(lst):\n    &quot;&quot;&quot;\u79fb\u9664\u5217\u8868\u4e2d\u7684\u7d20\u6570\u5143\u7d20&quot;&quot;&quot;\n    i = 0\n    while i &lt; len(lst):\n        if is_prime(lst[i]):\n            del lst[i]\n        else:\n            i += 1\n    return lst\n<\/code><\/pre>\n<p><strong>3. \u600e\u4e48\u4f7f\u7528Python\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u4ece\u5217\u8868\u4e2d\u79fb\u9664\u6240\u6709\u7684\u7d20\u6570\uff1f<\/strong><\/p>\n<p>\u7f16\u5199\u4e00\u4e2a\u8f85\u52a9\u51fd\u6570\u6765\u5224\u65ad\u4e00\u4e2a\u6570\u5b57\u662f\u5426\u4e3a\u7d20\u6570\uff0c\u5e76\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8fc7\u6ee4\u6389\u5217\u8868\u4e2d\u7684\u7d20\u6570\u5143\u7d20\u3002<\/p>\n<pre><code class=\"language-python\">def is_prime(num):\n    &quot;&quot;&quot;\u5224\u65ad\u4e00\u4e2a\u6570\u5b57\u662f\u5426\u4e3a\u7d20\u6570&quot;&quot;&quot;\n    if num &lt; 2:\n        return False\n    for i in range(2, int(num ** 0.5) + 1):\n        if num % i == 0:\n            return False\n    return True\n\ndef remove_primes(lst):\n    &quot;&quot;&quot;\u79fb\u9664\u5217\u8868\u4e2d\u7684\u7d20\u6570\u5143\u7d20&quot;&quot;&quot;\n    return [num for num in lst if not is_prime(num)]\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u53bb\u9664\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u7d20\u6570\uff0c\u4e3b\u8981\u6d89\u53ca\u5230\u4e09\u4e2a\u6b65\u9aa4\uff1a\u8bc6\u522b\u7d20\u6570\u3001\u904d\u5386\u5217\u8868\u4ee5\u53ca\u79fb\u9664\u7d20\u6570\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u51fd\u6570\u6765\u5224\u65ad\u4e00\u4e2a\u6570\u662f [&hellip;]","protected":false},"author":3,"featured_media":177013,"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\/177002"}],"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=177002"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/177002\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/177013"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=177002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=177002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=177002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}