{"id":975457,"date":"2024-12-27T06:13:26","date_gmt":"2024-12-26T22:13:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/975457.html"},"modified":"2024-12-27T06:13:27","modified_gmt":"2024-12-26T22:13:27","slug":"%e7%94%a8python%e5%a6%82%e4%bd%95%e7%ad%9b%e9%80%89%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/975457.html","title":{"rendered":"\u7528python\u5982\u4f55\u7b5b\u9009\u6570\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200701\/232df9f6-f220-4692-9ad2-5e80bd0a0980.webp\" alt=\"\u7528python\u5982\u4f55\u7b5b\u9009\u6570\u5b57\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u7b5b\u9009\u6570\u5b57\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u4f7f\u7528filter\u51fd\u6570\u3001\u4f7f\u7528\u5faa\u73af\u8bed\u53e5\u3001\u4f7f\u7528NumPy\u5e93\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5229\u7528\u8fd9\u4e9b\u65b9\u6cd5\u7b5b\u9009\u6570\u5b57\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5229\u7528Python\u8fdb\u884c\u6570\u5b57\u7b5b\u9009\u662f\u4e00\u4e2a\u5e38\u89c1\u4e14\u6709\u7528\u7684\u6280\u5de7\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u3002\u901a\u8fc7\u4f7f\u7528\u5217\u8868\u89e3\u6790\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece\u4e00\u4e2a\u5217\u8868\u4e2d\u63d0\u53d6\u7b26\u5408\u6761\u4ef6\u7684\u6570\u5b57\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u6267\u884c\u6548\u7387\u9ad8\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u60f3\u4ece\u4e00\u4e2a\u5217\u8868\u4e2d\u7b5b\u9009\u51fa\u6240\u6709\u7684\u5076\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\u6765\u5b9e\u73b0\u3002\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u975e\u5e38\u7b80\u6d01\u7684\u5199\u6cd5\uff0c\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b9e\u73b0\u590d\u6742\u7684\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u4f7f\u7528\u5217\u8868\u89e3\u6790\u7b5b\u9009\u5076\u6570<\/p>\n<p>numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>even_numbers = [num for num in numbers if num % 2 == 0]<\/p>\n<p>print(even_numbers)  # \u8f93\u51fa: [2, 4, 6, 8, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u89e3\u6790\u4ece\u4e00\u4e2a\u5305\u542b\u6570\u5b57\u7684\u5217\u8868\u4e2d\u7b5b\u9009\u51fa\u5076\u6570\u3002\u5217\u8868\u89e3\u6790\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u7b80\u6d01\u6027\u548c\u9ad8\u6548\u6027\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u7b5b\u9009\u4efb\u52a1\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5176\u4ed6\u51e0\u79cd\u5e38\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/p>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662fPython\u4e2d\u4e00\u79cd\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u7b5b\u9009\u6570\u5b57\u3002\u5b83\u4e0d\u4ec5\u80fd\u591f\u5b9e\u73b0\u590d\u6742\u7684\u7b5b\u9009\u6761\u4ef6\uff0c\u8fd8\u80fd\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b8c\u6210\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u7b5b\u9009\u5076\u6570<\/strong><\/li>\n<\/ol>\n<p><p>\u5f53\u6211\u4eec\u9700\u8981\u4ece\u4e00\u4e2a\u5217\u8868\u4e2d\u7b5b\u9009\u51fa\u6240\u6709\u7684\u5076\u6570\u65f6\uff0c\u53ef\u4ee5\u5229\u7528\u5217\u8868\u89e3\u6790\u4e2d\u7684\u6761\u4ef6\u5224\u65ad\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u7b5b\u9009\u5076\u6570<\/p>\n<p>numbers = [15, 22, 37, 40, 51, 64, 72, 89, 90]<\/p>\n<p>even_numbers = [num for num in numbers if num % 2 == 0]<\/p>\n<p>print(even_numbers)  # \u8f93\u51fa: [22, 40, 64, 72, 90]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5217\u8868\u89e3\u6790\u901a\u8fc7<code>if num % 2 == 0<\/code>\u5224\u65ad\u6761\u4ef6\u7b5b\u9009\u51fa\u6240\u6709\u7684\u5076\u6570\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7b5b\u9009\u5927\u4e8e\u67d0\u4e2a\u503c\u7684\u6570\u5b57<\/strong><\/li>\n<\/ol>\n<p><p>\u5217\u8868\u89e3\u6790\u4e5f\u53ef\u4ee5\u7528\u4e8e\u7b5b\u9009\u51fa\u5927\u4e8e\u67d0\u4e2a\u7279\u5b9a\u503c\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u7b5b\u9009\u5927\u4e8e50\u7684\u6570\u5b57<\/p>\n<p>numbers = [15, 22, 37, 40, 51, 64, 72, 89, 90]<\/p>\n<p>greater_than_50 = [num for num in numbers if num &gt; 50]<\/p>\n<p>print(greater_than_50)  # \u8f93\u51fa: [51, 64, 72, 89, 90]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u66f4\u6539\u6761\u4ef6\u5224\u65ad\u90e8\u5206\uff0c\u5217\u8868\u89e3\u6790\u53ef\u4ee5\u6839\u636e\u9700\u8981\u7b5b\u9009\u51fa\u5927\u4e8e\u67d0\u4e2a\u503c\u7684\u6570\u5b57\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\u7f6e\u7684\u4e00\u4e2a\u7528\u4e8e\u7b5b\u9009\u6570\u636e\u7684\u5f3a\u5927\u5de5\u5177\u3002\u5b83\u4f1a\u6839\u636e\u4e00\u4e2a\u51fd\u6570\u8fd4\u56de\u503c\u7684\u771f\u5047\u6765\u7b5b\u9009\u6570\u636e\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u7b5b\u9009\u5947\u6570<\/strong><\/li>\n<\/ol>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u5224\u65ad\u4e00\u4e2a\u6570\u5b57\u662f\u5426\u662f\u5947\u6570\uff0c\u7136\u540e\u4f7f\u7528<code>filter()<\/code>\u51fd\u6570\u7b5b\u9009\u51fa\u6240\u6709\u7684\u5947\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u7b5b\u9009\u5947\u6570<\/p>\n<p>numbers = [15, 22, 37, 40, 51, 64, 72, 89, 90]<\/p>\n<p>def is_odd(num):<\/p>\n<p>    return num % 2 != 0<\/p>\n<p>odd_numbers = list(filter(is_odd, numbers))<\/p>\n<p>print(odd_numbers)  # \u8f93\u51fa: [15, 37, 51, 89]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>filter()<\/code>\u51fd\u6570\u5e94\u7528\u4e86<code>is_odd<\/code>\u51fd\u6570\u6765\u7b5b\u9009\u51fa\u6240\u6709\u7684\u5947\u6570\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7b5b\u9009\u5c0f\u4e8e\u67d0\u4e2a\u503c\u7684\u6570\u5b57<\/strong><\/li>\n<\/ol>\n<p><p>\u540c\u6837\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>filter()<\/code>\u51fd\u6570\u6765\u7b5b\u9009\u51fa\u5c0f\u4e8e\u67d0\u4e2a\u7279\u5b9a\u503c\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u7b5b\u9009\u5c0f\u4e8e40\u7684\u6570\u5b57<\/p>\n<p>numbers = [15, 22, 37, 40, 51, 64, 72, 89, 90]<\/p>\n<p>def less_than_40(num):<\/p>\n<p>    return num &lt; 40<\/p>\n<p>less_than_40_numbers = list(filter(less_than_40, numbers))<\/p>\n<p>print(less_than_40_numbers)  # \u8f93\u51fa: [15, 22, 37]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9a\u4e49\u4e0d\u540c\u7684\u5224\u65ad\u51fd\u6570\uff0c<code>filter()<\/code>\u53ef\u4ee5\u7075\u6d3b\u5730\u5b9e\u73b0\u4e0d\u540c\u7684\u7b5b\u9009\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5faa\u73af\u8bed\u53e5<\/p>\n<\/p>\n<p><p>\u867d\u7136\u5217\u8868\u89e3\u6790\u548c<code>filter()<\/code>\u51fd\u6570\u662f\u66f4\u4e3a\u7b80\u6d01\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u5faa\u73af\u8bed\u53e5\u8fdb\u884c\u7b5b\u9009\u53ef\u80fd\u66f4\u4e3a\u76f4\u89c2\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u7b5b\u9009\u8d28\u6570<\/strong><\/li>\n<\/ol>\n<p><p>\u7b5b\u9009\u51fa\u8d28\u6570\u662f\u4e00\u4e2a\u7a0d\u5fae\u590d\u6742\u7684\u4efb\u52a1\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u8bed\u53e5\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u7b5b\u9009\u8d28\u6570<\/p>\n<p>def is_prime(n):<\/p>\n<p>    if n &lt;= 1:<\/p>\n<p>        return False<\/p>\n<p>    for i in range(2, int(n0.5) + 1):<\/p>\n<p>        if n % i == 0:<\/p>\n<p>            return False<\/p>\n<p>    return True<\/p>\n<p>numbers = [15, 22, 37, 40, 51, 64, 72, 89, 90]<\/p>\n<p>prime_numbers = []<\/p>\n<p>for num in numbers:<\/p>\n<p>    if is_prime(num):<\/p>\n<p>        prime_numbers.append(num)<\/p>\n<p>print(prime_numbers)  # \u8f93\u51fa: [37, 89]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>is_prime<\/code>\u51fd\u6570\u7528\u4e8e\u5224\u65ad\u4e00\u4e2a\u6570\u5b57\u662f\u5426\u4e3a\u8d28\u6570\uff0c\u5e76\u901a\u8fc7\u5faa\u73af\u8bed\u53e5\u8fdb\u884c\u7b5b\u9009\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u81ea\u5b9a\u4e49\u590d\u6742\u7b5b\u9009\u6761\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u6839\u636e\u591a\u4e2a\u6761\u4ef6\u8fdb\u884c\u7b5b\u9009\uff0c\u6b64\u65f6\u5faa\u73af\u8bed\u53e5\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u7b5b\u9009\u5076\u6570\u4e14\u5927\u4e8e50\u7684\u6570\u5b57<\/p>\n<p>numbers = [15, 22, 37, 40, 51, 64, 72, 89, 90]<\/p>\n<p>complex_condition_numbers = []<\/p>\n<p>for num in numbers:<\/p>\n<p>    if num % 2 == 0 and num &gt; 50:<\/p>\n<p>        complex_condition_numbers.append(num)<\/p>\n<p>print(complex_condition_numbers)  # \u8f93\u51fa: [64, 72, 90]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5faa\u73af\u8bed\u53e5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0\u591a\u6761\u4ef6\u7684\u7b5b\u9009\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528NumPy\u5e93<\/p>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u7b5b\u9009\u5927\u91cf\u6570\u636e\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15<\/strong><\/li>\n<\/ol>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u5e03\u5c14\u7d22\u5f15\u529f\u80fd\uff0c\u53ef\u4ee5\u975e\u5e38\u9ad8\u6548\u5730\u8fdb\u884c\u6570\u5b57\u7b5b\u9009\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u7b5b\u9009\u5927\u4e8e50\u7684\u6570\u5b57<\/strong><\/h2>\n<p>numbers = np.array([15, 22, 37, 40, 51, 64, 72, 89, 90])<\/p>\n<p>greater_than_50 = numbers[numbers &gt; 50]<\/p>\n<p>print(greater_than_50)  # \u8f93\u51fa: [51 64 72 89 90]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cNumPy\u7684\u5e03\u5c14\u7d22\u5f15\u529f\u80fd\u4f7f\u5f97\u6211\u4eec\u80fd\u591f\u5feb\u901f\u5730\u7b5b\u9009\u51fa\u5927\u4e8e50\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7ed3\u5408\u591a\u6761\u4ef6\u7b5b\u9009<\/strong><\/li>\n<\/ol>\n<p><p>NumPy\u8fd8\u53ef\u4ee5\u7ed3\u5408\u591a\u6761\u4ef6\u8fdb\u884c\u7b5b\u9009\uff0c\u8fdb\u4e00\u6b65\u589e\u5f3a\u4e86\u7b5b\u9009\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u7b5b\u9009\u5076\u6570\u4e14\u5c0f\u4e8e80\u7684\u6570\u5b57<\/strong><\/h2>\n<p>numbers = np.array([15, 22, 37, 40, 51, 64, 72, 89, 90])<\/p>\n<p>complex_condition_numbers = numbers[(numbers % 2 == 0) &amp; (numbers &lt; 80)]<\/p>\n<p>print(complex_condition_numbers)  # \u8f93\u51fa: [22 40 64 72]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u591a\u4e2a\u6761\u4ef6\uff0cNumPy\u80fd\u591f\u5feb\u901f\u5730\u8fdb\u884c\u590d\u6742\u7684\u7b5b\u9009\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u7b5b\u9009\u6570\u5b57\uff0c\u5305\u62ec\u5217\u8868\u89e3\u6790\u3001<code>filter()<\/code>\u51fd\u6570\u3001\u5faa\u73af\u8bed\u53e5\u548cNumPy\u5e93\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u6570\u636e\u89c4\u6a21\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u7b5b\u9009\u4efb\u52a1\uff0c\u5217\u8868\u89e3\u6790\u662f\u4e00\u4e2a\u9ad8\u6548\u4e14\u7b80\u6d01\u7684\u9009\u62e9\uff1b\u5bf9\u4e8e\u9700\u8981\u5b9a\u4e49\u590d\u6742\u6761\u4ef6\u7684\u60c5\u51b5\uff0c\u5faa\u73af\u8bed\u53e5\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\uff1b\u800c\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0cNumPy\u7684\u9ad8\u6548\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\u5c06\u6781\u5927\u5730\u63d0\u5347\u7b5b\u9009\u6548\u7387\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u7406\u89e3\u5176\u80cc\u540e\u7684\u539f\u7406\u548c\u5e94\u7528\u573a\u666f\u90fd\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\uff0c\u60a8\u53ef\u4ee5\u5728\u65e5\u5e38\u7684\u6570\u636e\u5904\u7406\u5de5\u4f5c\u4e2d\u66f4\u52a0\u6e38\u5203\u6709\u4f59\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7b5b\u9009\u7279\u5b9a\u8303\u56f4\u5185\u7684\u6570\u5b57\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216<code>filter()<\/code>\u51fd\u6570\u6765\u7b5b\u9009\u7279\u5b9a\u8303\u56f4\u7684\u6570\u5b57\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u7b5b\u9009\u51fa0\u5230100\u4e4b\u95f4\u7684\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = [1, 22, 45, 67, 89, 101, 150]  \nfiltered_numbers = [num for num in numbers if 0 &lt;= num &lt;= 100]  \nprint(filtered_numbers)  # \u8f93\u51fa: [1, 22, 45, 67, 89]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u7b80\u6d01\u660e\u4e86\uff0c\u9002\u5408\u5feb\u901f\u7b5b\u9009\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528Python\u5e93\u6765\u7b5b\u9009\u6570\u5b57\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u7684<code>numpy<\/code>\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u7b5b\u9009\u6570\u5b57\u3002\u4f7f\u7528<code>numpy<\/code>\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\uff0c\u5e76\u5bf9\u7279\u5b9a\u6761\u4ef6\u8fdb\u884c\u7b5b\u9009\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np  \narr = np.array([1, 22, 45, 67, 89, 101, 150])  \nfiltered_arr = arr[(arr &gt;= 0) &amp; (arr &lt;= 100)]  \nprint(filtered_arr)  # \u8f93\u51fa: [ 1 22 45 67 89]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u5904\u7406\u5927\u578b\u6570\u7ec4\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u7b5b\u9009\u51fa\u91cd\u590d\u7684\u6570\u5b57\uff1f<\/strong><br \/>\u8981\u7b5b\u9009\u51fa\u5217\u8868\u4e2d\u7684\u91cd\u590d\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u6765\u627e\u51fa\u54ea\u4e9b\u6570\u5b57\u51fa\u73b0\u591a\u6b21\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = [1, 2, 2, 3, 4, 4, 5]  \nduplicates = set(num for num in numbers if numbers.count(num) &gt; 1)  \nprint(duplicates)  # \u8f93\u51fa: {2, 4}\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u4f60\u80fd\u591f\u5feb\u901f\u8bc6\u522b\u5e76\u63d0\u53d6\u91cd\u590d\u7684\u6570\u5b57\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u7b5b\u9009\u6570\u5b57\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u4f7f\u7528filter\u51fd\u6570\u3001\u4f7f\u7528\u5faa\u73af\u8bed\u53e5\u3001\u4f7f\u7528NumPy\u5e93\u3002\u4e0b\u9762\u6211 [&hellip;]","protected":false},"author":3,"featured_media":975458,"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\/975457"}],"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=975457"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/975457\/revisions"}],"predecessor-version":[{"id":975459,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/975457\/revisions\/975459"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/975458"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=975457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=975457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=975457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}