{"id":1132302,"date":"2025-01-08T20:55:53","date_gmt":"2025-01-08T12:55:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1132302.html"},"modified":"2025-01-08T20:55:57","modified_gmt":"2025-01-08T12:55:57","slug":"python-%e5%a6%82%e4%bd%95%e5%b0%86%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%95%b0%e7%bb%84%e8%bd%ac%e6%8d%a2%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1132302.html","title":{"rendered":"python \u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u6570\u7ec4\u8f6c\u6362\u7c7b\u578b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101903\/456d865a-56f0-42b9-93d2-dc630634251c.webp\" alt=\"python \u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u6570\u7ec4\u8f6c\u6362\u7c7b\u578b\" \/><\/p>\n<p><p> <strong>\u8981\u5c06Python\u4e2d\u7684\u5b57\u7b26\u4e32\u6570\u7ec4\u8f6c\u6362\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u5229\u7528\u5217\u8868\u89e3\u6790\u3001\u4f7f\u7528map\u51fd\u6570\u3001\u4f7f\u7528numpy\u5e93\u3001\u4f7f\u7528pandas\u5e93\u3002<\/strong> \u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u91cd\u70b9\u4ecb\u7ecd\u4f7f\u7528\u5217\u8868\u89e3\u6790\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u800c\u9ad8\u6548\u7684\u65b9\u5f0f\u6765\u904d\u5386\u5217\u8868\u5e76\u5bf9\u5176\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\u3002\u901a\u8fc7\u5217\u8868\u89e3\u6790\uff0c\u6211\u4eec\u53ef\u4ee5\u975e\u5e38\u8f7b\u677e\u5730\u5c06\u5b57\u7b26\u4e32\u6570\u7ec4\u8f6c\u6362\u4e3a\u5176\u4ed6\u7c7b\u578b\uff0c\u4f8b\u5982\u6574\u6570\u3001\u6d6e\u70b9\u6570\u7b49\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\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u5217\u8868\u6216\u5bf9\u73b0\u6709\u5217\u8868\u8fdb\u884c\u64cd\u4f5c\u3002\u5b83\u901a\u8fc7\u4e00\u4e2a\u8868\u8fbe\u5f0f\u548c\u4e00\u4e2afor\u5faa\u73af\u6765\u751f\u6210\u65b0\u7684\u5217\u8868\u3002\u6bd4\u5982\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u4e32\u7684\u6570\u7ec4\uff0c\u60f3\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string_array = [&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;]<\/p>\n<p>int_array = [int(x) for x in string_array]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u6548\u7387\u9ad8\u3002\u4f60\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b8c\u6210\u8f6c\u6362\uff0c\u975e\u5e38\u9002\u5408\u5904\u7406\u5c0f\u578b\u6570\u636e\u96c6\u6216\u7b80\u5355\u7684\u8f6c\u6362\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u4f18\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u7b80\u6d01\u660e\u4e86<\/strong>\uff1a\u4ee3\u7801\u7b80\u6d01\uff0c\u5bb9\u6613\u9605\u8bfb\u548c\u7406\u89e3\u3002<\/li>\n<li><strong>\u9ad8\u6548<\/strong>\uff1a\u5217\u8868\u89e3\u6790\u7684\u6267\u884c\u901f\u5ea6\u901a\u5e38\u6bd4\u4f7f\u7528for\u5faa\u73af\u66f4\u5feb\u3002<\/li>\n<\/ol>\n<p><h3>\u7f3a\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u53ef\u8bfb\u6027<\/strong>\uff1a\u5bf9\u4e8e\u590d\u6742\u7684\u8f6c\u6362\u903b\u8f91\uff0c\u5217\u8868\u89e3\u6790\u53ef\u80fd\u4e0d\u592a\u5bb9\u6613\u7406\u89e3\u3002<\/li>\n<li><strong>\u5185\u5b58\u5360\u7528<\/strong>\uff1a\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\uff0c\u5217\u8868\u89e3\u6790\u53ef\u80fd\u4f1a\u5360\u7528\u5927\u91cf\u5185\u5b58\u3002<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528map\u51fd\u6570<\/p>\n<\/p>\n<p><p>map\u51fd\u6570\u662f\u4e00\u79cd\u9ad8\u9636\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u5c06\u4e00\u4e2a\u51fd\u6570\u5e94\u7528\u4e8e\u4e00\u4e2a\u6216\u591a\u4e2a\u5e8f\u5217\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002\u4f7f\u7528map\u51fd\u6570\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5c06\u5b57\u7b26\u4e32\u6570\u7ec4\u8f6c\u6362\u4e3a\u5176\u4ed6\u7c7b\u578b\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">string_array = [&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;]<\/p>\n<p>int_array = list(map(int, string_array))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0e\u5217\u8868\u89e3\u6790\u76f8\u6bd4\uff0cmap\u51fd\u6570\u7684\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u4f46\u53ef\u80fd\u4e0d\u5982\u5217\u8868\u89e3\u6790\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><h3>\u4f18\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u7b80\u6d01<\/strong>\uff1a\u4ee3\u7801\u66f4\u77ed\uff0c\u5c24\u5176\u9002\u5408\u7b80\u5355\u7684\u8f6c\u6362\u4efb\u52a1\u3002<\/li>\n<li><strong>\u7075\u6d3b<\/strong>\uff1a\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u81ea\u5b9a\u4e49\u51fd\u6570\u5e94\u7528\u4e8e\u6bcf\u4e2a\u5143\u7d20\u3002<\/li>\n<\/ol>\n<p><h3>\u7f3a\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u53ef\u8bfb\u6027<\/strong>\uff1a\u5bf9\u4e8e\u4e0d\u719f\u6089map\u51fd\u6570\u7684\u4eba\u6765\u8bf4\uff0c\u4ee3\u7801\u53ef\u80fd\u4e0d\u592a\u5bb9\u6613\u7406\u89e3\u3002<\/li>\n<li><strong>\u6027\u80fd<\/strong>\uff1a\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\uff0cmap\u51fd\u6570\u7684\u6027\u80fd\u53ef\u80fd\u4e0d\u5982numpy\u6216pandas\u3002<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528numpy\u5e93<\/p>\n<\/p>\n<p><p>numpy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u548c\u6570\u503c\u8ba1\u7b97\u529f\u80fd\u3002\u4f7f\u7528numpy\uff0c\u6211\u4eec\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5c06\u5b57\u7b26\u4e32\u6570\u7ec4\u8f6c\u6362\u4e3a\u5176\u4ed6\u7c7b\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>string_array = np.array([&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;])<\/p>\n<p>int_array = string_array.astype(int)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>numpy\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u548c\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u9002\u5408\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u548c\u590d\u6742\u7684\u6570\u503c\u8ba1\u7b97\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u4f18\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u9ad8\u6548<\/strong>\uff1anumpy\u7684\u6570\u7ec4\u64cd\u4f5c\u901f\u5ea6\u975e\u5e38\u5feb\uff0c\u9002\u5408\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u3002<\/li>\n<li><strong>\u529f\u80fd\u4e30\u5bcc<\/strong>\uff1a\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u503c\u8ba1\u7b97\u548c\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\u3002<\/li>\n<\/ol>\n<p><h3>\u7f3a\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u4f9d\u8d56\u5e93<\/strong>\uff1a\u9700\u8981\u5b89\u88c5\u548c\u5bfc\u5165numpy\u5e93\uff0c\u5bf9\u4e8e\u7b80\u5355\u4efb\u52a1\u53ef\u80fd\u663e\u5f97\u591a\u4f59\u3002<\/li>\n<li><strong>\u5b66\u4e60\u6210\u672c<\/strong>\uff1anumpy\u7684\u5b66\u4e60\u66f2\u7ebf\u76f8\u5bf9\u8f83\u9661\uff0c\u521d\u5b66\u8005\u53ef\u80fd\u9700\u8981\u4e00\u4e9b\u65f6\u95f4\u6765\u638c\u63e1\u3002<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f7f\u7528pandas\u5e93<\/p>\n<\/p>\n<p><p>pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u529f\u80fd\u3002\u4f7f\u7528pandas\uff0c\u6211\u4eec\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5c06\u5b57\u7b26\u4e32\u6570\u7ec4\u8f6c\u6362\u4e3a\u5176\u4ed6\u7c7b\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>string_array = pd.Series([&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;])<\/p>\n<p>int_array = string_array.astype(int)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>pandas\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u9ad8\u6548\u7684\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u529f\u80fd\uff0c\u9002\u5408\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u548c\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u4f18\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u9ad8\u6548<\/strong>\uff1apandas\u7684\u6570\u636e\u64cd\u4f5c\u901f\u5ea6\u975e\u5e38\u5feb\uff0c\u9002\u5408\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u3002<\/li>\n<li><strong>\u529f\u80fd\u4e30\u5bcc<\/strong>\uff1a\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u529f\u80fd\u3002<\/li>\n<\/ol>\n<p><h3>\u7f3a\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u4f9d\u8d56\u5e93<\/strong>\uff1a\u9700\u8981\u5b89\u88c5\u548c\u5bfc\u5165pandas\u5e93\uff0c\u5bf9\u4e8e\u7b80\u5355\u4efb\u52a1\u53ef\u80fd\u663e\u5f97\u591a\u4f59\u3002<\/li>\n<li><strong>\u5b66\u4e60\u6210\u672c<\/strong>\uff1apandas\u7684\u5b66\u4e60\u66f2\u7ebf\u76f8\u5bf9\u8f83\u9661\uff0c\u521d\u5b66\u8005\u53ef\u80fd\u9700\u8981\u4e00\u4e9b\u65f6\u95f4\u6765\u638c\u63e1\u3002<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u5b57\u7b26\u4e32\u6570\u7ec4\u7684\u8f6c\u6362\u3002\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u4f18\u52bf\u5728\u4e8e\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u8c03\u6574\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def convert_string_array(string_array, target_type):<\/p>\n<p>    return [target_type(x) for x in string_array]<\/p>\n<p>string_array = [&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;]<\/p>\n<p>int_array = convert_string_array(string_array, int)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u4f18\u52bf\u5728\u4e8e\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u8c03\u6574\u3002<\/p>\n<\/p>\n<p><h3>\u4f18\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u7075\u6d3b<\/strong>\uff1a\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u8c03\u6574\uff0c\u9002\u5e94\u6027\u5f3a\u3002<\/li>\n<li><strong>\u53ef\u8bfb\u6027<\/strong>\uff1a\u4ee3\u7801\u6e05\u6670\uff0c\u5bb9\u6613\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/li>\n<\/ol>\n<p><h3>\u7f3a\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u6027\u80fd<\/strong>\uff1a\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\uff0c\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u6027\u80fd\u53ef\u80fd\u4e0d\u5982numpy\u6216pandas\u3002<\/li>\n<li><strong>\u91cd\u590d\u4ee3\u7801<\/strong>\uff1a\u5bf9\u4e8e\u4e0d\u540c\u7684\u8f6c\u6362\u4efb\u52a1\uff0c\u53ef\u80fd\u9700\u8981\u7f16\u5199\u4e0d\u540c\u7684\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u589e\u52a0\u4e86\u4ee3\u7801\u7684\u590d\u6742\u6027\u3002<\/li>\n<\/ol>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5c06\u5b57\u7b26\u4e32\u6570\u7ec4\u8f6c\u6362\u4e3a\u5176\u4ed6\u7c7b\u578b\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u5217\u8868\u89e3\u6790\u3001map\u51fd\u6570\u3001numpy\u5e93\u3001pandas\u5e93\u548c\u81ea\u5b9a\u4e49\u51fd\u6570\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u8f6c\u6362\u4efb\u52a1\uff0c\u5217\u8868\u89e3\u6790\u548cmap\u51fd\u6570\u662f\u975e\u5e38\u597d\u7684\u9009\u62e9\uff1b\u5bf9\u4e8e\u5927\u578b\u6570\u636e\u96c6\u548c\u590d\u6742\u7684\u6570\u503c\u8ba1\u7b97\u4efb\u52a1\uff0cnumpy\u548cpandas\u662f\u66f4\u597d\u7684\u9009\u62e9\uff1b\u800c\u81ea\u5b9a\u4e49\u51fd\u6570\u5219\u9002\u7528\u4e8e\u9700\u8981\u7075\u6d3b\u8c03\u6574\u7684\u573a\u666f\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u6570\u7ec4\u8f6c\u6362\u4e3a\u6574\u578b\u6570\u7ec4\uff1f<\/strong><br \/>\u8981\u5c06\u5b57\u7b26\u4e32\u6570\u7ec4\u8f6c\u6362\u4e3a\u6574\u578b\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>map<\/code>\u51fd\u6570\u7ed3\u5408<code>int<\/code>\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4<code>str_array = [&quot;1&quot;, &quot;2&quot;, &quot;3&quot;]<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u578b\u6570\u7ec4\uff1a<code>int_array = list(map(int, str_array))<\/code>\u3002\u8fd9\u6837\uff0c<code>int_array<\/code>\u5c06\u53d8\u4e3a<code>[1, 2, 3]<\/code>\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u6570\u7ec4\u8f6c\u6362\u4e3a\u6d6e\u70b9\u578b\u6570\u7ec4\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u8981\u5c06\u5b57\u7b26\u4e32\u6570\u7ec4\u8f6c\u6362\u4e3a\u6d6e\u70b9\u578b\u6570\u7ec4\uff0c\u4f7f\u7528<code>float<\/code>\u51fd\u6570\u66ff\u4ee3<code>int<\/code>\u51fd\u6570\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4<code>str_array = [&quot;1.1&quot;, &quot;2.2&quot;, &quot;3.3&quot;]<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>float_array = list(map(float, str_array))<\/code>\u5c06\u5176\u8f6c\u6362\u4e3a\u6d6e\u70b9\u578b\u6570\u7ec4\uff0c\u7ed3\u679c\u4e3a<code>[1.1, 2.2, 3.3]<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\u4e32\u7684\u6570\u7ec4\uff1f<\/strong><br \/>\u5728\u5904\u7406\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\u4e32\u7684\u6570\u7ec4\u65f6\uff0c\u76f4\u63a5\u8f6c\u6362\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6355\u83b7\u5f02\u5e38\u5e76\u5904\u7406\uff0c\u6216\u8005\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u6570\u7ec4<code>str_array = [&quot;1&quot;, &quot;abc&quot;, &quot;3&quot;]<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8fc7\u6ee4\u6389\u975e\u6570\u5b57\u9879\uff1a  <\/p>\n<pre><code class=\"language-python\">int_array = [int(x) for x in str_array if x.isdigit()]\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<code>int_array<\/code>\u5c06\u4e3a<code>[1, 3]<\/code>\uff0c\u800c<code>&quot;abc&quot;<\/code>\u5c06\u88ab\u5ffd\u7565\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06Python\u4e2d\u7684\u5b57\u7b26\u4e32\u6570\u7ec4\u8f6c\u6362\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u5229\u7528\u5217\u8868\u89e3\u6790\u3001\u4f7f\u7528map\u51fd\u6570\u3001\u4f7f\u7528numpy\u5e93\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1132312,"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\/1132302"}],"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=1132302"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132302\/revisions"}],"predecessor-version":[{"id":1132313,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132302\/revisions\/1132313"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1132312"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1132302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1132302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1132302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}