{"id":925813,"date":"2024-12-26T15:37:25","date_gmt":"2024-12-26T07:37:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/925813.html"},"modified":"2024-12-26T15:37:27","modified_gmt":"2024-12-26T07:37:27","slug":"python%e5%a6%82%e4%bd%95%e8%be%a8%e5%88%ab","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/925813.html","title":{"rendered":"python\u5982\u4f55\u8fa8\u522b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212710\/2f9bb556-cdb0-46b7-9c75-b3619b81510b.webp\" alt=\"python\u5982\u4f55\u8fa8\u522b\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u8fa8\u522b\u6570\u636e\u7c7b\u578b\u3001\u9519\u8bef\u548c\u6a21\u5f0f\u7b49\uff0c\u5176\u4e2d\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u5f02\u5e38\u5904\u7406\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u4ecb\u7ecd\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8fa8\u522b\u6570\u636e\u7c7b\u578b<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5185\u7f6e\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8fa8\u522b\u6570\u636e\u7c7b\u578b\uff0c\u6bd4\u5982<code>type()<\/code>\u3001<code>isinstance()<\/code>\u7b49\u3002\u5176\u4e2d\uff0c<code>type()<\/code>\u51fd\u6570\u8fd4\u56de\u5bf9\u8c61\u7684\u6570\u636e\u7c7b\u578b\uff0c\u800c<code>isinstance()<\/code>\u51fd\u6570\u5219\u7528\u4e8e\u68c0\u67e5\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u67d0\u4e2a\u7279\u5b9a\u7c7b\u7684\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5f53\u6211\u4eec\u5904\u7406\u8f93\u5165\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u8fa8\u522b\u6570\u636e\u7c7b\u578b\u4ee5\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u9700\u8981\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>type()<\/code>\u6765\u68c0\u67e5\u8f93\u5165\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5f02\u5e38\u5904\u7406\u7528\u4e8e\u8fa8\u522b\u9519\u8bef<\/p>\n<\/p>\n<p><p>\u5728\u7f16\u5199Python\u4ee3\u7801\u65f6\uff0c\u9519\u8bef\u548c\u5f02\u5e38\u662f\u4e0d\u53ef\u907f\u514d\u7684\u3002\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u9760\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u8fa8\u522b\u548c\u5904\u7406\u9519\u8bef\u3002Python\u4f7f\u7528<code>try<\/code>\u3001<code>except<\/code>\u5757\u6765\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6765\u68c0\u67e5\u9664\u96f6\u9519\u8bef\u3001\u6587\u4ef6\u64cd\u4f5c\u9519\u8bef\u6216\u5176\u4ed6\u7c7b\u578b\u7684\u8fd0\u884c\u65f6\u9519\u8bef\u3002\u901a\u8fc7\u6355\u83b7\u7279\u5b9a\u7684\u5f02\u5e38\u7c7b\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u63d0\u4f9b\u6709\u610f\u4e49\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u5e76\u6267\u884c\u76f8\u5e94\u7684\u9519\u8bef\u5904\u7406\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7528\u4e8e\u6a21\u5f0f\u5339\u914d<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5728\u6587\u672c\u4e2d\u641c\u7d22\u548c\u5339\u914d\u7279\u5b9a\u7684\u6a21\u5f0f\u3002Python\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u652f\u6301\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8bc6\u522b\u548c\u5904\u7406\u6587\u672c\u6570\u636e\u4e2d\u7684\u590d\u6742\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5f53\u6211\u4eec\u5904\u7406\u6587\u672c\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5339\u914d\u7279\u5b9a\u7684\u5b57\u7b26\u4e32\u6a21\u5f0f\uff0c\u5982\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u3001\u7535\u8bdd\u53f7\u7801\u6216IP\u5730\u5740\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u627e\u5230\u8fd9\u4e9b\u6a21\u5f0f\uff0c\u5e76\u63d0\u53d6\u76f8\u5173\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u548c\u7c7b\u8fa8\u522b\u590d\u6742\u6a21\u5f0f<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5185\u7f6e\u51fd\u6570\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u80fd\u4e0d\u8db3\u4ee5\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u6a21\u5f0f\u3002\u8fd9\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u6216\u7c7b\u6765\u5b9e\u73b0\u7279\u5b9a\u7684\u8fa8\u522b\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5f53\u6211\u4eec\u9700\u8981\u5904\u7406\u5177\u6709\u590d\u6742\u7ed3\u6784\u7684\u6570\u636e\uff0c\u5982\u5d4c\u5957\u7684JSON\u5bf9\u8c61\u6216XML\u6587\u6863\u65f6\uff0c\u53ef\u4ee5\u7f16\u5199\u81ea\u5b9a\u4e49\u89e3\u6790\u5668\u6216\u7c7b\u6765\u63d0\u53d6\u6240\u9700\u4fe1\u606f\u3002\u901a\u8fc7\u8bbe\u8ba1\u826f\u597d\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u9ad8\u6548\u5730\u8fa8\u522b\u548c\u5904\u7406\u590d\u6742\u6570\u636e\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5e94\u7528<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6280\u672f\u8fdb\u884c\u9ad8\u7ea7\u6570\u636e\u8fa8\u522b<\/p>\n<\/p>\n<p><p>\u5728\u9ad8\u7ea7\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u5c24\u5176\u662f\u6d89\u53ca\u5927\u89c4\u6a21\u6570\u636e\u5206\u6790\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u673a\u5668\u5b66\u4e60\u6280\u672f\u6765\u81ea\u52a8\u8fa8\u522b\u548c\u5206\u7c7b\u6570\u636e\u3002Python\u7684\u673a\u5668\u5b66\u4e60\u5e93\uff0c\u5982scikit-learn\u3001TensorFlow\u548cPyTorch\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u7b97\u6cd5\u6765\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5728\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4efb\u52a1\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u673a\u5668\u5b66\u4e60\u6a21\u578b\u6765\u8bc6\u522b\u6587\u672c\u7684\u60c5\u611f\u3001\u4e3b\u9898\u6216\u4f5c\u8005\u3002\u901a\u8fc7\u8bad\u7ec3\u548c\u8bc4\u4f30\u5408\u9002\u7684\u6a21\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u7cbe\u5ea6\u7684\u6570\u636e\u8fa8\u522b\u548c\u5206\u7c7b\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4ee5\u4e0a\u5404\u4e2a\u65b9\u9762\uff1a<\/p>\n<\/p>\n<hr>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8fa8\u522b\u6570\u636e\u7c7b\u578b<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5185\u7f6e\u51fd\u6570\u6765\u5e2e\u52a9\u6211\u4eec\u8fa8\u522b\u6570\u636e\u7c7b\u578b\u3002\u8fd9\u4e9b\u51fd\u6570\u975e\u5e38\u6709\u7528\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u8f93\u5165\u6570\u636e\u548c\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u65f6\u3002<\/p>\n<\/p>\n<ol>\n<li><strong><code>type()<\/code>\u51fd\u6570<\/strong><\/li>\n<\/ol>\n<p><p><code>type()<\/code>\u51fd\u6570\u7528\u4e8e\u8fd4\u56de\u5bf9\u8c61\u7684\u6570\u636e\u7c7b\u578b\u3002\u5b83\u662fPython\u4e2d\u6700\u57fa\u672c\u7684\u7c7b\u578b\u8fa8\u522b\u5de5\u5177\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>print(type(x))  # &lt;class &#39;int&#39;&gt;<\/p>\n<p>y = &quot;Hello&quot;<\/p>\n<p>print(type(y))  # &lt;class &#39;str&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>type()<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u68c0\u67e5\u53d8\u91cf\u7684\u7c7b\u578b\uff0c\u4ece\u800c\u6839\u636e\u7c7b\u578b\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong><code>isinstance()<\/code>\u51fd\u6570<\/strong><\/li>\n<\/ol>\n<p><p><code>isinstance()<\/code>\u51fd\u6570\u7528\u4e8e\u68c0\u67e5\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u67d0\u4e2a\u7279\u5b9a\u7c7b\u7684\u5b9e\u4f8b\u3002\u4e0e<code>type()<\/code>\u4e0d\u540c\uff0c<code>isinstance()<\/code>\u53ef\u4ee5\u68c0\u67e5\u5bf9\u8c61\u662f\u5426\u662f\u67d0\u4e2a\u7c7b\u7684\u5b50\u7c7b\u7684\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Animal:<\/p>\n<p>    pass<\/p>\n<p>class Dog(Animal):<\/p>\n<p>    pass<\/p>\n<p>d = Dog()<\/p>\n<p>print(isinstance(d, Dog))       # True<\/p>\n<p>print(isinstance(d, Animal))    # True<\/p>\n<p>print(isinstance(d, object))    # True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>isinstance()<\/code>\u51fd\u6570\u5728\u9700\u8981\u68c0\u67e5\u591a\u6001\u6027\u65f6\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u786e\u8ba4\u5bf9\u8c61\u662f\u5426\u5c5e\u4e8e\u67d0\u4e2a\u7c7b\u7684\u7ee7\u627f\u94fe\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u7c7b\u578b\u8f6c\u6362\u51fd\u6570<\/strong><\/li>\n<\/ol>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u7c7b\u578b\u8f6c\u6362\u51fd\u6570\uff0c\u5982<code>int()<\/code>\u3001<code>float()<\/code>\u3001<code>str()<\/code>\u7b49\uff0c\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">s = &quot;123&quot;<\/p>\n<p>n = int(s)  # \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570<\/p>\n<p>print(type(n))  # &lt;class &#39;int&#39;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7c7b\u578b\u8f6c\u6362\u51fd\u6570\u5728\u9700\u8981\u624b\u52a8\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u65f6\u975e\u5e38\u6709\u7528\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u6216\u6587\u4ef6\u6570\u636e\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5f02\u5e38\u5904\u7406\u7528\u4e8e\u8fa8\u522b\u9519\u8bef<\/p>\n<\/p>\n<p><p>\u5f02\u5e38\u5904\u7406\u662fPython\u4e2d\u5904\u7406\u9519\u8bef\u548c\u5f02\u5e38\u7684\u91cd\u8981\u673a\u5236\u3002\u901a\u8fc7\u4f7f\u7528<code>try<\/code>\u3001<code>except<\/code>\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u6355\u83b7\u548c\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\uff0c\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u57fa\u672c\u5f02\u5e38\u5904\u7406<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7<code>try<\/code>\u3001<code>except<\/code>\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u6355\u83b7\u7279\u5b9a\u7684\u5f02\u5e38\u7c7b\u578b\uff0c\u5e76\u6267\u884c\u76f8\u5e94\u7684\u9519\u8bef\u5904\u7406\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    result = 10 \/ 0<\/p>\n<p>except ZeroDivisionError:<\/p>\n<p>    print(&quot;\u9664\u96f6\u9519\u8bef\uff01&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>ZeroDivisionError<\/code>\u5f02\u5e38\u88ab\u6355\u83b7\uff0c\u5e76\u8f93\u51fa\u76f8\u5e94\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6355\u83b7\u591a\u4e2a\u5f02\u5e38<\/strong><\/li>\n<\/ol>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5728\u4e00\u4e2a<code>try<\/code>\u5757\u4e2d\u6355\u83b7\u591a\u4e2a\u5f02\u5e38\u7c7b\u578b\uff0c\u5e76\u4e3a\u6bcf\u79cd\u5f02\u5e38\u7c7b\u578b\u6307\u5b9a\u4e0d\u540c\u7684\u5904\u7406\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    value = int(input(&quot;Enter a number: &quot;))<\/p>\n<p>    result = 10 \/ value<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;\u8f93\u5165\u7684\u4e0d\u662f\u6570\u5b57\uff01&quot;)<\/p>\n<p>except ZeroDivisionError:<\/p>\n<p>    print(&quot;\u4e0d\u80fd\u9664\u4ee5\u96f6\uff01&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u5e94\u5bf9\u4e0d\u540c\u7c7b\u578b\u7684\u9519\u8bef\u60c5\u51b5\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u4f7f\u7528<code>finally<\/code>\u5b50\u53e5<\/strong><\/li>\n<\/ol>\n<p><p><code>finally<\/code>\u5b50\u53e5\u7528\u4e8e\u5728\u5f02\u5e38\u5904\u7406\u5b8c\u6210\u540e\u6267\u884c\u6e05\u7406\u4ee3\u7801\uff0c\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    f = open(&quot;file.txt&quot;, &quot;r&quot;)<\/p>\n<p>    # \u6267\u884c\u6587\u4ef6\u64cd\u4f5c<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(&quot;\u6587\u4ef6\u672a\u627e\u5230\uff01&quot;)<\/p>\n<p>finally:<\/p>\n<p>    f.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>finally<\/code>\u5757\u4e2d\u7684\u4ee3\u7801\u603b\u662f\u4f1a\u6267\u884c\uff0c\u901a\u5e38\u7528\u4e8e\u91ca\u653e\u8d44\u6e90\u6216\u8fdb\u884c\u5176\u4ed6\u6e05\u7406\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7528\u4e8e\u6a21\u5f0f\u5339\u914d<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5728\u6587\u672c\u4e2d\u641c\u7d22\u548c\u5339\u914d\u7279\u5b9a\u7684\u6a21\u5f0f\u3002Python\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u652f\u6301\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u57fa\u672c\u5339\u914d<\/strong><\/li>\n<\/ol>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>re.match()<\/code>\u3001<code>re.search()<\/code>\u548c<code>re.findall()<\/code>\u7b49\u51fd\u6570\u6765\u5339\u914d\u6587\u672c\u4e2d\u7684\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&quot;\\d+&quot;<\/p>\n<p>text = &quot;There are 123 apples and 456 oranges.&quot;<\/p>\n<p>match = re.search(pattern, text)<\/p>\n<p>if match:<\/p>\n<p>    print(&quot;\u627e\u5230\u5339\u914d\uff1a&quot;, match.group())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f<code>\\d+<\/code>\u7528\u4e8e\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u6570\u5b57\u5b57\u7b26\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5206\u7ec4\u548c\u66ff\u6362<\/strong><\/li>\n<\/ol>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u652f\u6301\u5206\u7ec4\u548c\u66ff\u6362\u64cd\u4f5c\uff0c\u901a\u8fc7\u4f7f\u7528\u62ec\u53f7\u5206\u7ec4\uff0c\u6211\u4eec\u53ef\u4ee5\u63d0\u53d6\u5339\u914d\u7684\u5b50\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&quot;(\\d+) apples and (\\d+) oranges&quot;<\/p>\n<p>text = &quot;There are 123 apples and 456 oranges.&quot;<\/p>\n<p>match = re.search(pattern, text)<\/p>\n<p>if match:<\/p>\n<p>    apples = match.group(1)<\/p>\n<p>    oranges = match.group(2)<\/p>\n<p>    print(f&quot;Apples: {apples}, Oranges: {oranges}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u7684<code>sub()<\/code>\u51fd\u6570\u7528\u4e8e\u66ff\u6362\u5339\u914d\u7684\u6587\u672c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">new_text = re.sub(r&quot;apples&quot;, &quot;bananas&quot;, text)<\/p>\n<p>print(new_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u590d\u6742\u6a21\u5f0f\u5339\u914d<\/strong><\/li>\n<\/ol>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u4e8e\u5339\u914d\u590d\u6742\u7684\u6587\u672c\u6a21\u5f0f\uff0c\u4f8b\u5982\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u3001IP\u5730\u5740\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l_pattern = r&quot;\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\\b&quot;<\/p>\n<p>email_text = &quot;Contact us at support@example.com.&quot;<\/p>\n<p>email_match = re.search(email_pattern, email_text)<\/p>\n<p>if email_match:<\/p>\n<p>    print(&quot;\u627e\u5230\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff1a&quot;, email_match.group())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9a\u4e49\u590d\u6742\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u51c6\u786e\u5730\u5339\u914d\u548c\u63d0\u53d6\u6240\u9700\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u548c\u7c7b\u8fa8\u522b\u590d\u6742\u6a21\u5f0f<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5185\u7f6e\u51fd\u6570\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u80fd\u4e0d\u8db3\u4ee5\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u6a21\u5f0f\u3002\u8fd9\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u6216\u7c7b\u6765\u5b9e\u73b0\u7279\u5b9a\u7684\u8fa8\u522b\u903b\u8f91\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u7279\u5b9a\u7684\u903b\u8f91\u6765\u5904\u7406\u590d\u6742\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_valid_ip(ip_address):<\/p>\n<p>    parts = ip_address.split(&quot;.&quot;)<\/p>\n<p>    if len(parts) != 4:<\/p>\n<p>        return False<\/p>\n<p>    for part in parts:<\/p>\n<p>        if not part.isdigit() or not 0 &lt;= int(part) &lt;= 255:<\/p>\n<p>            return False<\/p>\n<p>    return True<\/p>\n<p>print(is_valid_ip(&quot;192.168.0.1&quot;))  # True<\/p>\n<p>print(is_valid_ip(&quot;999.999.999.999&quot;))  # False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u81ea\u5b9a\u4e49\u51fd\u6570<code>is_valid_ip()<\/code>\u7528\u4e8e\u9a8c\u8bc1IP\u5730\u5740\u7684\u6709\u6548\u6027\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528\u7c7b<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u9700\u8981\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u548c\u884c\u4e3a\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u7c7b\u6765\u5c01\u88c5\u903b\u8f91\u548c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class EmailValidator:<\/p>\n<p>    def __init__(self, email):<\/p>\n<p>        self.email = email<\/p>\n<p>    def is_valid(self):<\/p>\n<p>        pattern = r&quot;\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\\b&quot;<\/p>\n<p>        return re.match(pattern, self.email) is not None<\/p>\n<p>validator = EmailValidator(&quot;support@example.com&quot;)<\/p>\n<p>print(validator.is_valid())  # True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u7c7b\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u53ef\u91cd\u7528\u548c\u6269\u5c55\u6027\u66f4\u5f3a\u7684\u4ee3\u7801\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5e94\u7528\u673a\u5668\u5b66\u4e60\u6280\u672f\u8fdb\u884c\u9ad8\u7ea7\u6570\u636e\u8fa8\u522b<\/p>\n<\/p>\n<p><p>\u5728\u9ad8\u7ea7\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u5c24\u5176\u662f\u6d89\u53ca\u5927\u89c4\u6a21\u6570\u636e\u5206\u6790\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u673a\u5668\u5b66\u4e60\u6280\u672f\u6765\u81ea\u52a8\u8fa8\u522b\u548c\u5206\u7c7b\u6570\u636e\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528scikit-learn\u8fdb\u884c\u5206\u7c7b<\/strong><\/li>\n<\/ol>\n<p><p>scikit-learn\u662f\u4e00\u4e2a\u5e38\u7528\u7684\u673a\u5668\u5b66\u4e60\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5206\u7c7b\u548c\u56de\u5f52\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.datasets import load_iris<\/p>\n<p>from sklearn.model_selection import train_test_split<\/p>\n<p>from sklearn.ensemble import RandomForestClassifier<\/p>\n<p>from sklearn.metrics import accuracy_score<\/p>\n<h2><strong>\u52a0\u8f7d\u6570\u636e<\/strong><\/h2>\n<p>iris = load_iris()<\/p>\n<p>X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.3)<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model = RandomForestClassifier()<\/p>\n<p>model.fit(X_train, y_train)<\/p>\n<h2><strong>\u9884\u6d4b<\/strong><\/h2>\n<p>y_pred = model.predict(X_test)<\/p>\n<h2><strong>\u8bc4\u4f30<\/strong><\/h2>\n<p>print(&quot;\u51c6\u786e\u7387\uff1a&quot;, accuracy_score(y_test, y_pred))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u968f\u673a\u68ee\u6797\u5206\u7c7b\u5668\u6765\u5bf9Iris\u6570\u636e\u96c6\u8fdb\u884c\u5206\u7c7b\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528\u81ea\u7136\u8bed\u8a00\u5904\u7406\u8fdb\u884c\u6587\u672c\u8fa8\u522b<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4e2d\uff0c\u673a\u5668\u5b66\u4e60\u6a21\u578b\u53ef\u4ee5\u7528\u4e8e\u6587\u672c\u5206\u7c7b\u3001\u60c5\u611f\u5206\u6790\u7b49\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.feature_extraction.text import CountVectorizer<\/p>\n<p>from sklearn.naive_bayes import MultinomialNB<\/p>\n<h2><strong>\u6837\u672c\u6587\u672c<\/strong><\/h2>\n<p>texts = [&quot;I love this product&quot;, &quot;This is a terrible product&quot;]<\/p>\n<p>labels = [1, 0]  # 1\u8868\u793a\u6b63\u9762\uff0c0\u8868\u793a\u8d1f\u9762<\/p>\n<h2><strong>\u7279\u5f81\u63d0\u53d6<\/strong><\/h2>\n<p>vectorizer = CountVectorizer()<\/p>\n<p>X = vectorizer.fit_transform(texts)<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model = MultinomialNB()<\/p>\n<p>model.fit(X, labels)<\/p>\n<h2><strong>\u9884\u6d4b\u65b0\u6587\u672c<\/strong><\/h2>\n<p>new_text = [&quot;I hate this product&quot;]<\/p>\n<p>new_X = vectorizer.transform(new_text)<\/p>\n<p>prediction = model.predict(new_X)<\/p>\n<p>print(&quot;\u9884\u6d4b\u7ed3\u679c\uff1a&quot;, prediction)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8bad\u7ec3\u6587\u672c\u5206\u7c7b\u6a21\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u81ea\u52a8\u8bc6\u522b\u6587\u672c\u7684\u60c5\u611f\u548c\u4e3b\u9898\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6df1\u5ea6\u5b66\u4e60\u6280\u672f<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u66f4\u590d\u6742\u7684\u4efb\u52a1\u4e2d\uff0c\u6df1\u5ea6\u5b66\u4e60\u6280\u672f\u53ef\u4ee5\u63d0\u4f9b\u66f4\u5f3a\u5927\u7684\u6570\u636e\u8fa8\u522b\u80fd\u529b\u3002\u901a\u8fc7\u4f7f\u7528TensorFlow\u6216PyTorch\u7b49\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff0c\u6211\u4eec\u53ef\u4ee5\u6784\u5efa\u548c\u8bad\u7ec3\u590d\u6742\u7684\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5728\u56fe\u50cf\u5206\u7c7b\u4efb\u52a1\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5377\u79ef\u795e\u7ecf\u7f51\u7edc\uff08CNN\uff09\u6765\u81ea\u52a8\u8fa8\u522b\u56fe\u50cf\u4e2d\u7684\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<p>from tensorflow.keras.datasets import mnist<\/p>\n<p>from tensorflow.keras.models import Sequential<\/p>\n<p>from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D<\/p>\n<h2><strong>\u52a0\u8f7d\u6570\u636e<\/strong><\/h2>\n<p>(X_train, y_train), (X_test, y_test) = mnist.load_data()<\/p>\n<p>X_train, X_test = X_train \/ 255.0, X_test \/ 255.0<\/p>\n<h2><strong>\u6784\u5efa\u6a21\u578b<\/strong><\/h2>\n<p>model = Sequential([<\/p>\n<p>    Conv2D(32, (3, 3), activation=&#39;relu&#39;, input_shape=(28, 28, 1)),<\/p>\n<p>    MaxPooling2D((2, 2)),<\/p>\n<p>    Flatten(),<\/p>\n<p>    Dense(64, activation=&#39;relu&#39;),<\/p>\n<p>    Dense(10, activation=&#39;softmax&#39;)<\/p>\n<p>])<\/p>\n<h2><strong>\u7f16\u8bd1\u548c\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model.compile(optimizer=&#39;adam&#39;, loss=&#39;sparse_categorical_crossentropy&#39;, metrics=[&#39;accuracy&#39;])<\/p>\n<p>model.fit(X_train, y_train, epochs=5)<\/p>\n<h2><strong>\u8bc4\u4f30\u6a21\u578b<\/strong><\/h2>\n<p>test_loss, test_acc = model.evaluate(X_test, y_test)<\/p>\n<p>print(&quot;\u6d4b\u8bd5\u51c6\u786e\u7387\uff1a&quot;, test_acc)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u6df1\u5ea6\u5b66\u4e60\u6280\u672f\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u590d\u6742\u7684\u6a21\u5f0f\u8bc6\u522b\u548c\u6570\u636e\u8fa8\u522b\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bc6\u522b\u5b57\u7b26\u4e32\u7684\u7c7b\u578b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>type()<\/code>\u6765\u8bc6\u522b\u4e00\u4e2a\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u5bf9\u4e8e\u5b57\u7b26\u4e32\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>isinstance(variable, str)<\/code>\u6765\u786e\u8ba4\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b\u3002\u6b64\u5916\uff0cPython\u7684<code>str<\/code>\u7c7b\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\uff0c\u5982<code>isalpha()<\/code>\u3001<code>isdigit()<\/code>\u7b49\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5224\u65ad\u5b57\u7b26\u4e32\u7684\u7279\u5f81\u3002<\/p>\n<p><strong>Python\u5982\u4f55\u8bc6\u522b\u6587\u4ef6\u683c\u5f0f\uff1f<\/strong><br \/>\u5728\u5904\u7406\u6587\u4ef6\u65f6\uff0cPython\u53ef\u4ee5\u901a\u8fc7\u6587\u4ef6\u6269\u5c55\u540d\u6216\u6587\u4ef6\u5934\u4fe1\u606f\u6765\u8bc6\u522b\u6587\u4ef6\u683c\u5f0f\u3002\u4f7f\u7528<code>os.path.splitext()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u63d0\u53d6\u6587\u4ef6\u540d\u7684\u6269\u5c55\u540d\uff0c\u800c\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u7ed3\u5408<code>read()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8bfb\u53d6\u6587\u4ef6\u5934\u90e8\u7684\u5b57\u8282\uff0c\u4ee5\u5224\u65ad\u6587\u4ef6\u683c\u5f0f\u3002\u7b2c\u4e09\u65b9\u5e93\u5982<code>python-magic<\/code>\u4e5f\u63d0\u4f9b\u4e86\u66f4\u4e3a\u51c6\u786e\u7684\u6587\u4ef6\u7c7b\u578b\u8bc6\u522b\u529f\u80fd\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u8fa8\u522b\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u591a\u79cd\u65b9\u6cd5\u6765\u8fa8\u522b\u6570\u636e\u7c7b\u578b\u3002\u4f7f\u7528<code>type()<\/code>\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u83b7\u53d6\u4e00\u4e2a\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c\u800c\u4f7f\u7528<code>isinstance()<\/code>\u51fd\u6570\u53ef\u4ee5\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u5c5e\u4e8e\u67d0\u79cd\u7279\u5b9a\u7c7b\u578b\u3002\u5bf9\u4e8e\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5217\u8868\u3001\u5b57\u5178\u7b49\uff0c\u7ed3\u5408<code>len()<\/code>\u51fd\u6570\u548c<code>keys()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u5206\u6790\u6570\u636e\u7684\u7ec4\u6210\u90e8\u5206\uff0c\u4ece\u800c\u66f4\u597d\u5730\u7406\u89e3\u5176\u7c7b\u578b\u7279\u5f81\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u8fa8\u522b\u6570\u636e\u7c7b\u578b\u3001\u9519\u8bef\u548c\u6a21\u5f0f\u7b49\uff0c\u5176\u4e2d\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u5f02\u5e38\u5904\u7406\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002\u4ee5\u4e0b\u662f [&hellip;]","protected":false},"author":3,"featured_media":925818,"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\/925813"}],"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=925813"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/925813\/revisions"}],"predecessor-version":[{"id":925820,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/925813\/revisions\/925820"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/925818"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=925813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=925813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=925813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}