{"id":1171397,"date":"2025-01-15T16:34:44","date_gmt":"2025-01-15T08:34:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1171397.html"},"modified":"2025-01-15T16:34:47","modified_gmt":"2025-01-15T08:34:47","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86list%e6%95%b4%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1171397.html","title":{"rendered":"python\u5982\u4f55\u5c06list\u6574\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26073739\/ffb61009-28c2-4d09-bd39-66b72ab1664c.webp\" alt=\"python\u5982\u4f55\u5c06list\u6574\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5c06list\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5305\u62ec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001map\u51fd\u6570\u548c\u5faa\u73af\u7b49\u65b9\u6cd5\u3002<\/strong>\u5e38\u89c1\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528map\u51fd\u6570\u3001\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u521b\u5efa\u5217\u8868\u7684\u65b9\u5f0f\u3002\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u4e00\u4e2alist\u4e2d\u7684\u5143\u7d20\u9010\u4e2a\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u4e32\u6570\u5b57\u7684\u5217\u8868<\/p>\n<p>str_list = [&#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570<\/strong><\/h2>\n<p>int_list = [int(i) for i in str_list]<\/p>\n<p>print(int_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u6d01\u4e14\u6613\u8bfb\uff0c\u9002\u5408\u5904\u7406\u7b80\u5355\u7684\u8f6c\u6362\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528map\u51fd\u6570<\/p>\n<\/p>\n<p><p>map\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u9ad8\u9636\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u4e00\u4e2a\u51fd\u6570\u4f5c\u7528\u5230\u4e00\u4e2a\u5217\u8868\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u4e0a\uff0c\u4ece\u800c\u5b9e\u73b0\u6279\u91cf\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u4e32\u6570\u5b57\u7684\u5217\u8868<\/p>\n<p>str_list = [&#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;]<\/p>\n<h2><strong>\u4f7f\u7528map\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570<\/strong><\/h2>\n<p>int_list = list(map(int, str_list))<\/p>\n<p>print(int_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528map\u51fd\u6570\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u907f\u514d\u663e\u5f0f\u7684\u5faa\u73af\uff0c\u4f7f\u4ee3\u7801\u66f4\u7b80\u6d01\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cmap\u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u56e0\u6b64\u9700\u8981\u4f7f\u7528list\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5faa\u73af\u7ed3\u6784<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528for\u5faa\u73af\uff0c\u53ef\u4ee5\u9010\u4e2a\u904d\u5386\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u4e32\u6570\u5b57\u7684\u5217\u8868<\/p>\n<p>str_list = [&#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;]<\/p>\n<h2><strong>\u4f7f\u7528for\u5faa\u73af\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570<\/strong><\/h2>\n<p>int_list = []<\/p>\n<p>for i in str_list:<\/p>\n<p>    int_list.append(int(i))<\/p>\n<p>print(int_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u76f4\u89c2\uff0c\u5bb9\u6613\u7406\u89e3\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u8f6c\u6362\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5904\u7406\u590d\u6742\u60c5\u51b5<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0clist\u4e2d\u7684\u5143\u7d20\u53ef\u80fd\u4e0d\u4ec5\u4ec5\u662f\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u6570\u5b57\uff0c\u53ef\u80fd\u8fd8\u5305\u542b\u5176\u4ed6\u7c7b\u578b\u7684\u5143\u7d20\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u6765\u5904\u7406\u590d\u6742\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u5143\u7d20\u7684\u5217\u8868<\/p>\n<p>mixed_list = [&#39;1&#39;, &#39;2&#39;, &#39;three&#39;, &#39;4&#39;, &#39;5.5&#39;, None]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u5c06\u5b57\u7b26\u4e32\u6570\u5b57\u548c\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6574\u6570<\/strong><\/h2>\n<p>int_list = [int(float(i)) for i in mixed_list if isinstance(i, (str, float)) and i.replace(&#39;.&#39;, &#39;&#39;, 1).isdigit()]<\/p>\n<p>print(int_list)  # \u8f93\u51fa: [1, 2, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u901a\u8fc7\u6761\u4ef6\u5224\u65ad\u8fc7\u6ee4\u6389\u975e\u6570\u5b57\u5b57\u7b26\u4e32\u548cNone\uff0c\u7136\u540e\u518d\u5c06\u5b57\u7b26\u4e32\u6570\u5b57\u548c\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u9519\u8bef\u5904\u7406<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u65e0\u6cd5\u8f6c\u6362\u7684\u60c5\u51b5\uff0c\u4f8b\u5982\u5217\u8868\u4e2d\u5305\u542b\u975e\u6570\u5b57\u7684\u5b57\u7b26\u4e32\u3002\u8fd9\u65f6\u53ef\u4ee5\u901a\u8fc7try-except\u7ed3\u6784\u8fdb\u884c\u9519\u8bef\u5904\u7406\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u5143\u7d20\u7684\u5217\u8868<\/p>\n<p>mixed_list = [&#39;1&#39;, &#39;2&#39;, &#39;three&#39;, &#39;4&#39;, &#39;5.5&#39;, None]<\/p>\n<h2><strong>\u4f7f\u7528for\u5faa\u73af\u548ctry-except\u7ed3\u6784\u5904\u7406\u9519\u8bef<\/strong><\/h2>\n<p>int_list = []<\/p>\n<p>for i in mixed_list:<\/p>\n<p>    try:<\/p>\n<p>        int_list.append(int(float(i)))<\/p>\n<p>    except (ValueError, TypeError):<\/p>\n<p>        pass<\/p>\n<p>print(int_list)  # \u8f93\u51fa: [1, 2, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5c06list\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5e76\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5404\u79cd\u60c5\u51b5\u3002\u9009\u62e9\u9002\u5408\u7684\u8f6c\u6362\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u4e2d\u7684\u5b57\u7b26\u4e32\u5217\u8868\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408<code>int()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u5217\u8868\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32\u5217\u8868<code>str_list = [&#39;1&#39;, &#39;2&#39;, &#39;3&#39;]<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u5c06\u6bcf\u4e2a\u5143\u7d20\u8f6c\u6362\u4e3a\u6574\u6570\uff1a<code>int_list = [int(i) for i in str_list]<\/code>\u3002\u8fd9\u6837\uff0c<code>int_list<\/code>\u5c06\u5305\u542b\u6574\u6570<code>[1, 2, 3]<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u8fc7\u6ee4\u6389\u5217\u8868\u4e2d\u7684\u975e\u6574\u6570\u5143\u7d20\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u548c<code>isinstance()<\/code>\u51fd\u6570\u6765\u8fc7\u6ee4\u975e\u6574\u6570\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u5143\u7d20\u7684\u5217\u8868<code>mixed_list = [1, &#39;2&#39;, 3.0, 4]<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>[x for x in mixed_list if isinstance(x, int)]<\/code>\u6765\u751f\u6210\u4e00\u4e2a\u53ea\u5305\u542b\u6574\u6570\u7684\u5217\u8868\uff0c\u7ed3\u679c\u4e3a<code>[1, 4]<\/code>\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5185\u7f6e\u51fd\u6570\u53ef\u4ee5\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u6574\u6570\uff1f<\/strong><br \/>\u867d\u7136Python\u6ca1\u6709\u4e13\u95e8\u7684\u5185\u7f6e\u51fd\u6570\u6765\u76f4\u63a5\u5c06\u6574\u4e2a\u5217\u8868\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u4f46\u7ed3\u5408<code>map()<\/code>\u51fd\u6570\u548c<code>list()<\/code>\u53ef\u4ee5\u5b9e\u73b0\u8be5\u6548\u679c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>int<\/code>\u4f5c\u4e3a\u6620\u5c04\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u6574\u6570\u5217\u8868<code>int_list = list(map(int, str_list))<\/code>\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u6574\u6570\u5217\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u800c\u6709\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5c06list\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5305\u62ec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001map\u51fd\u6570\u548c\u5faa\u73af\u7b49\u65b9\u6cd5\u3002\u5e38 [&hellip;]","protected":false},"author":3,"featured_media":1171404,"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\/1171397"}],"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=1171397"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1171397\/revisions"}],"predecessor-version":[{"id":1171405,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1171397\/revisions\/1171405"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1171404"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1171397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1171397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1171397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}