{"id":972837,"date":"2024-12-27T05:51:55","date_gmt":"2024-12-26T21:51:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/972837.html"},"modified":"2024-12-27T05:51:57","modified_gmt":"2024-12-26T21:51:57","slug":"python%e5%a6%82%e4%bd%95%e7%ad%9b%e9%80%89%e6%8c%87%e5%ae%9a%e5%86%85%e5%ae%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/972837.html","title":{"rendered":"python\u5982\u4f55\u7b5b\u9009\u6307\u5b9a\u5185\u5bb9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24195314\/1302ab69-b72f-4809-95ed-4bacf951baf3.webp\" alt=\"python\u5982\u4f55\u7b5b\u9009\u6307\u5b9a\u5185\u5bb9\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u7b5b\u9009\u6307\u5b9a\u5185\u5bb9\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001filter()\u51fd\u6570\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001Pandas\u5e93\u3002<\/strong>\u5176\u4e2d\uff0c<strong>\u5217\u8868\u89e3\u6790<\/strong>\u53ef\u4ee5\u901a\u8fc7\u6761\u4ef6\u8868\u8fbe\u5f0f\u76f4\u63a5\u4ece\u5217\u8868\u4e2d\u7b5b\u9009\u51fa\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\uff0c<strong>filter()\u51fd\u6570<\/strong>\u5219\u901a\u8fc7\u63d0\u4f9b\u7684\u51fd\u6570\u5bf9\u5e8f\u5217\u8fdb\u884c\u8fc7\u6ee4\uff0c<strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\u9002\u7528\u4e8e\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u7279\u5b9a\u6a21\u5f0f\u7684\u5185\u5bb9\uff0c<strong>Pandas\u5e93<\/strong>\u5219\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u548c\u7b5b\u9009\u5927\u578b\u6570\u636e\u96c6\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u4e2a\u65b9\u6cd5\u2014\u2014\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u7b5b\u9009\u3002<\/p>\n<\/p>\n<p><p><strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\u662f\u4e00\u79cd\u5f3a\u5927\u800c\u7075\u6d3b\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5728\u6587\u672c\u4e2d\u641c\u7d22\u548c\u5339\u914d\u590d\u6742\u7684\u5b57\u7b26\u4e32\u6a21\u5f0f\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u6765\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u8981\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7b5b\u9009\u6307\u5b9a\u5185\u5bb9\uff0c\u9996\u5148\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u5408\u9002\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u7136\u540e\u4f7f\u7528<code>re.match()<\/code>\u3001<code>re.search()<\/code>\u3001<code>re.findall()<\/code>\u7b49\u51fd\u6570\u4ece\u5b57\u7b26\u4e32\u4e2d\u7b5b\u9009\u51fa\u5339\u914d\u7684\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u8981\u4ece\u4e00\u6bb5\u6587\u672c\u4e2d\u7b5b\u9009\u51fa\u6240\u6709\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff1a<code>[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}<\/code>\u3002\u901a\u8fc7<code>re.findall()<\/code>\u51fd\u6570\uff0c\u5c31\u80fd\u4ece\u6587\u672c\u4e2d\u63d0\u53d6\u6240\u6709\u7b26\u5408\u8be5\u6a21\u5f0f\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7Python\u7684\u4e0d\u540c\u65b9\u6cd5\u7b5b\u9009\u6307\u5b9a\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5217\u8868\u89e3\u6790<\/p>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u65b9\u5f0f\uff0c\u80fd\u591f\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u7b5b\u9009\u51fa\u5217\u8868\u4e2d\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u3002\u901a\u8fc7\u5728\u65b9\u62ec\u53f7\u4e2d\u4f7f\u7528for\u5faa\u73af\u548c\u6761\u4ef6\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u5feb\u901f\u6784\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u7684\u7528\u6cd5\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u7b5b\u9009\u51fa\u5217\u8868\u4e2d\u5927\u4e8e5\u7684\u5143\u7d20<\/p>\n<p>numbers = [1, 3, 5, 7, 9, 11]<\/p>\n<p>filtered_numbers = [num for num in numbers if num &gt; 5]<\/p>\n<p>print(filtered_numbers)  # \u8f93\u51fa\uff1a[7, 9, 11]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>filtered_numbers<\/code>\u5217\u8868\u4e2d\u5305\u542b\u4e86<code>numbers<\/code>\u5217\u8868\u4e2d\u6240\u6709\u5927\u4e8e5\u7684\u5143\u7d20\u3002\u5217\u8868\u89e3\u6790\u4e0d\u4ec5\u53ef\u4ee5\u5e94\u7528\u4e8e\u7b80\u5355\u7684\u6761\u4ef6\u7b5b\u9009\uff0c\u8fd8\u80fd\u7528\u4e8e\u66f4\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\u5bf9\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u8fdb\u884c\u8f6c\u6362\u6216\u7ec4\u5408\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001filter()\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>filter()<\/code>\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u7528\u4e8e\u8fc7\u6ee4\u5e8f\u5217\u7684\u51fd\u6570\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u51fd\u6570\u548c\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u6240\u6709\u4f7f\u51fd\u6570\u8fd4\u56de<code>True<\/code>\u7684\u5143\u7d20\u3002\u4ee5\u4e0b\u662f<code>filter()<\/code>\u51fd\u6570\u7684\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u7b5b\u9009\u51fa\u5217\u8868\u4e2d\u662f\u5076\u6570\u7684\u5143\u7d20<\/p>\n<p>numbers = [1, 2, 3, 4, 5, 6]<\/p>\n<p>def is_even(num):<\/p>\n<p>    return num % 2 == 0<\/p>\n<p>filtered_numbers = filter(is_even, numbers)<\/p>\n<p>print(list(filtered_numbers))  # \u8f93\u51fa\uff1a[2, 4, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>is_even<\/code>\u51fd\u6570\u7528\u4e8e\u5224\u65ad\u4e00\u4e2a\u6570\u5b57\u662f\u5426\u4e3a\u5076\u6570\uff0c<code>filter()<\/code>\u51fd\u6570\u6839\u636e\u8fd9\u4e2a\u5224\u65ad\u6761\u4ef6\u5bf9<code>numbers<\/code>\u5217\u8868\u8fdb\u884c\u4e86\u8fc7\u6ee4\uff0c\u5e76\u8fd4\u56de\u4e86\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5076\u6570\u7684\u8fed\u4ee3\u5668\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u7528\u4e8e\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7ec4\u5408\u7684\u6a21\u5f0f\u3002\u5728Python\u4e2d\uff0c<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\u7528\u4e8e\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4ece\u6587\u672c\u4e2d\u7b5b\u9009\u7279\u5b9a\u5185\u5bb9\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u4ece\u6587\u672c\u4e2d\u7b5b\u9009\u51fa\u6240\u6709\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740<\/strong><\/h2>\n<p>text = &quot;\u8054\u7cfb\u6211\uff1aexample@example.com\uff0c\u6216\u8005\u8bbf\u95ee\u6211\u4eec\u7684\u7f51\u7ad9\uff1awww.example.com&quot;<\/p>\n<p>em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l_pattern = r&#39;[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}&#39;<\/p>\n<p>emails = re.findall(email_pattern, text)<\/p>\n<p>print(emails)  # \u8f93\u51fa\uff1a[&#39;example@example.com&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>re.findall()<\/code>\u51fd\u6570\u6839\u636e\u6307\u5b9a\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u4ece<code>text<\/code>\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u51fa\u6240\u6709\u7b26\u5408\u7535\u5b50\u90ae\u4ef6\u683c\u5f0f\u7684\u5b50\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001Pandas\u5e93<\/p>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u5e7f\u6cdb\u7528\u4e8e\u5904\u7406\u548c\u5206\u6790\u7ed3\u6784\u5316\u6570\u636e\u3002Pandas\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u51fd\u6570\u7528\u4e8e\u7b5b\u9009\u6570\u636e\u3002\u5728\u4f7f\u7528Pandas\u8fdb\u884c\u6570\u636e\u7b5b\u9009\u65f6\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u548c\u6761\u4ef6\u8868\u8fbe\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Pandas\u7b5b\u9009\u6570\u636e\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u7b5b\u9009\u51faDataFrame\u4e2d\u5e74\u9f84\u5927\u4e8e30\u7684\u884c<\/strong><\/h2>\n<p>data = {&#39;name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;, &#39;David&#39;],<\/p>\n<p>        &#39;age&#39;: [25, 32, 30, 35]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>filtered_df = df[df[&#39;age&#39;] &gt; 30]<\/p>\n<p>print(filtered_df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u59d3\u540d\u548c\u5e74\u9f84\u7684DataFrame\uff0c\u7136\u540e\u901a\u8fc7\u5e03\u5c14\u7d22\u5f15\u548c\u6761\u4ef6\u8868\u8fbe\u5f0f\u7b5b\u9009\u51fa\u4e86\u5e74\u9f84\u5927\u4e8e30\u7684\u884c\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0a\u662fPython\u4e2d\u7b5b\u9009\u6307\u5b9a\u5185\u5bb9\u7684\u51e0\u79cd\u5e38\u7528\u65b9\u6cd5\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u70b9\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u7b5b\u9009\u7279\u5b9a\u7c7b\u578b\u7684\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001filter()\u51fd\u6570\u6216\u8005Pandas\u5e93\u6765\u7b5b\u9009\u7279\u5b9a\u7c7b\u578b\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u7b80\u5355\u5730\u901a\u8fc7\u6761\u4ef6\u8868\u8fbe\u5f0f\u6765\u7b5b\u9009\u51fa\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u3002\u4f7f\u7528Pandas\u65f6\uff0c\u53ef\u4ee5\u5229\u7528\u6761\u4ef6\u7d22\u5f15\u6765\u5feb\u901f\u7b5b\u9009DataFrame\u4e2d\u7684\u884c\u3002<\/p>\n<p><strong>\u7b5b\u9009\u5185\u5bb9\u65f6\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\uff1f<\/strong><br \/>\u5e38\u89c1\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\u5305\u62ec\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff08\u5982==\u3001&gt;\u3001&lt;\u3001!=\u7b49\uff09\u3001\u903b\u8f91\u8fd0\u7b97\u7b26\uff08\u5982and\u3001or\u3001not\uff09\u4ee5\u53ca\u5b57\u7b26\u4e32\u65b9\u6cd5\uff08\u5982str.contains()\u3001str.startswith()\u7b49\uff09\u3002\u8fd9\u4e9b\u8868\u8fbe\u5f0f\u80fd\u591f\u5e2e\u52a9\u7528\u6237\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u7b5b\u9009\u51fa\u6240\u9700\u7684\u5185\u5bb9\u3002<\/p>\n<p><strong>\u5728\u5927\u578b\u6570\u636e\u96c6\u4e2d\uff0c\u5982\u4f55\u63d0\u9ad8\u7b5b\u9009\u6548\u7387\uff1f<\/strong><br \/>\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528NumPy\u548cPandas\u5e93\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\u3002\u6b64\u5916\uff0c\u5408\u7406\u5229\u7528\u7d22\u5f15\u3001\u907f\u514d\u4f7f\u7528\u5faa\u73af\u800c\u91c7\u7528\u5411\u91cf\u5316\u64cd\u4f5c\u7b49\u65b9\u6cd5\uff0c\u90fd\u80fd\u663e\u8457\u63d0\u9ad8\u6570\u636e\u7b5b\u9009\u7684\u6548\u7387\u3002\u5bf9\u4e8e\u9700\u8981\u9891\u7e41\u7b5b\u9009\u7684\u573a\u666f\uff0c\u9884\u5904\u7406\u6570\u636e\u6216\u8fdb\u884c\u6570\u636e\u5206\u5757\u4e5f\u662f\u6709\u6548\u7684\u7b56\u7565\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u7b5b\u9009\u6307\u5b9a\u5185\u5bb9\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001filter()\u51fd\u6570\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001Pandas\u5e93\u3002\u5176\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":972842,"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\/972837"}],"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=972837"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/972837\/revisions"}],"predecessor-version":[{"id":972844,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/972837\/revisions\/972844"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/972842"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=972837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=972837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=972837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}