{"id":1056236,"date":"2024-12-31T14:58:02","date_gmt":"2024-12-31T06:58:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1056236.html"},"modified":"2024-12-31T14:58:05","modified_gmt":"2024-12-31T06:58:05","slug":"python%e5%88%97%e8%a1%a8%e5%85%83%e7%b4%a0%e5%a6%82%e4%bd%95%e8%bf%9b%e8%a1%8c%e5%bc%ba%e5%88%b6%e8%bd%ac%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1056236.html","title":{"rendered":"python\u5217\u8868\u5143\u7d20\u5982\u4f55\u8fdb\u884c\u5f3a\u5236\u8f6c\u6362"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/214ab9ec-2101-4fee-862f-e08113cb9d88.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5217\u8868\u5143\u7d20\u5982\u4f55\u8fdb\u884c\u5f3a\u5236\u8f6c\u6362\" \/><\/p>\n<p><p> <strong>Python\u5217\u8868\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001map()\u51fd\u6570\u3001for\u5faa\u73af\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5f3a\u5236\u8f6c\u6362<\/strong>\u3002\u5176\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u800c\u9ad8\u6548\u7684\u65b9\u5f0f\uff0c\u9002\u5408\u5927\u90e8\u5206\u8f6c\u6362\u9700\u6c42\uff1bmap()\u51fd\u6570\u5219\u9002\u7528\u4e8e\u9700\u8981\u5c06\u4e00\u4e2a\u51fd\u6570\u5e94\u7528\u4e8e\u5217\u8868\u7684\u6bcf\u4e2a\u5143\u7d20\u7684\u60c5\u51b5\uff1bfor\u5faa\u73af\u5219\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u64cd\u4f5c\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u52a0\u5165\u66f4\u591a\u590d\u6742\u7684\u903b\u8f91\u3002<strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8fdb\u884c\u8f6c\u6362\u662f\u6700\u7b80\u6d01\u548c\u9ad8\u6548\u7684\u65b9\u5f0f<\/strong>\uff0c\u4f8b\u5982\u5c06\u4e00\u4e2a\u5305\u542b\u6570\u5b57\u5b57\u7b26\u4e32\u7684\u5217\u8868\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>[int(x) for x in list_of_strings]<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u548c\u8f6c\u6362\u5217\u8868\u3002\u5b83\u4e0d\u4ec5\u8bed\u6cd5\u7b80\u5355\uff0c\u800c\u4e14\u6267\u884c\u6548\u7387\u9ad8\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u7528\u4e8e\u5404\u79cd\u8f6c\u6362\u64cd\u4f5c\uff0c\u4f8b\u5982\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3001\u6d6e\u70b9\u6570\uff0c\u6216\u8005\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a\u7279\u5b9a\u7684\u5c5e\u6027\u503c\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u4e32\u6570\u5b57\u7684\u5217\u8868\uff0c\u6211\u4eec\u5e0c\u671b\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list_of_strings = [&#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;]<\/p>\n<p>list_of_ints = [int(x) for x in list_of_strings]<\/p>\n<p>print(list_of_ints)  # \u8f93\u51fa: [1, 2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>int(x)<\/code>\u662f\u8f6c\u6362\u51fd\u6570\uff0c<code>for x in list_of_strings<\/code>\u662f\u8fed\u4ee3\u90e8\u5206\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u4f1a\u904d\u5386<code>list_of_strings<\/code>\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u6700\u7ec8\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868<code>list_of_ints<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5305\u542b\u6761\u4ef6\u7684\u5217\u8868\u63a8\u5bfc\u5f0f<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u5728\u5217\u8868\u63a8\u5bfc\u5f0f\u4e2d\u52a0\u5165\u6761\u4ef6\uff0c\u4f8b\u5982\u53ea\u8f6c\u6362\u90a3\u4e9b\u53ef\u4ee5\u8f6c\u6362\u7684\u5143\u7d20\uff0c\u5ffd\u7565\u5176\u4ed6\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list_of_strings = [&#39;1&#39;, &#39;2&#39;, &#39;three&#39;, &#39;4&#39;]<\/p>\n<p>list_of_ints = [int(x) for x in list_of_strings if x.isdigit()]<\/p>\n<p>print(list_of_ints)  # \u8f93\u51fa: [1, 2, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>if x.isdigit()<\/code>\u662f\u6761\u4ef6\u90e8\u5206\uff0c\u53ea\u6709\u6ee1\u8db3\u8be5\u6761\u4ef6\u7684\u5143\u7d20\u624d\u4f1a\u88ab\u8f6c\u6362\u5e76\u5305\u542b\u5728\u65b0\u7684\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001map()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>map()<\/code>\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u9ad8\u9636\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u5c06\u4e00\u4e2a\u51fd\u6570\u5e94\u7528\u5230\u4e00\u4e2a\u6216\u591a\u4e2a\u5217\u8868\u7684\u6bcf\u4e2a\u5143\u7d20\u4e0a\u3002<code>map()<\/code>\u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u9700\u8981\u8f6c\u6362\u4e3a\u5217\u8868\u624d\u80fd\u67e5\u770b\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>map()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u5217\u8868\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list_of_strings = [&#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;]<\/p>\n<p>list_of_ints = list(map(int, list_of_strings))<\/p>\n<p>print(list_of_ints)  # \u8f93\u51fa: [1, 2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>map(int, list_of_strings)<\/code>\u5c06<code>int<\/code>\u51fd\u6570\u5e94\u7528\u5230<code>list_of_strings<\/code>\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\u6211\u4eec\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u5c06\u8fed\u4ee3\u5668\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u8fdb\u884c\u8f6c\u6362\uff0c\u4f8b\u5982\u5c06\u5217\u8868\u4e2d\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5176\u957f\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def str_length(s):<\/p>\n<p>    return len(s)<\/p>\n<p>list_of_strings = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>list_of_lengths = list(map(str_length, list_of_strings))<\/p>\n<p>print(list_of_lengths)  # \u8f93\u51fa: [5, 6, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>str_length<\/code>\u662f\u81ea\u5b9a\u4e49\u7684\u8f6c\u6362\u51fd\u6570\uff0c\u5b83\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002<code>map(str_length, list_of_strings)<\/code>\u5c06\u8be5\u51fd\u6570\u5e94\u7528\u5230<code>list_of_strings<\/code>\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001for\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u8fdb\u884c\u8f6c\u6362\u867d\u7136\u6ca1\u6709\u5217\u8868\u63a8\u5bfc\u5f0f\u548c<code>map()<\/code>\u51fd\u6570\u90a3\u6837\u7b80\u6d01\uff0c\u4f46\u5b83\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u8f6c\u6362\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u5c06\u5b57\u7b26\u4e32\u5217\u8868\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list_of_strings = [&#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;]<\/p>\n<p>list_of_ints = []<\/p>\n<p>for x in list_of_strings:<\/p>\n<p>    list_of_ints.append(int(x))<\/p>\n<p>print(list_of_ints)  # \u8f93\u51fa: [1, 2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u904d\u5386<code>list_of_strings<\/code>\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u540e\u6dfb\u52a0\u5230<code>list_of_ints<\/code>\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5305\u542b\u6761\u4ef6\u7684for\u5faa\u73af<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5728<code>for<\/code>\u5faa\u73af\u4e2d\u52a0\u5165\u6761\u4ef6\uff0c\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u8f6c\u6362\u903b\u8f91\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list_of_strings = [&#39;1&#39;, &#39;2&#39;, &#39;three&#39;, &#39;4&#39;]<\/p>\n<p>list_of_ints = []<\/p>\n<p>for x in list_of_strings:<\/p>\n<p>    if x.isdigit():<\/p>\n<p>        list_of_ints.append(int(x))<\/p>\n<p>print(list_of_ints)  # \u8f93\u51fa: [1, 2, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>if x.isdigit()<\/code>\u662f\u6761\u4ef6\u90e8\u5206\uff0c\u53ea\u6709\u6ee1\u8db3\u8be5\u6761\u4ef6\u7684\u5143\u7d20\u624d\u4f1a\u88ab\u8f6c\u6362\u5e76\u6dfb\u52a0\u5230\u65b0\u7684\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5e94\u7528\u793a\u4f8b<\/h3>\n<\/p>\n<p><h4>1\u3001\u5c06\u5d4c\u5957\u5217\u8868\u7684\u5143\u7d20\u8fdb\u884c\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5d4c\u5957\u5217\u8868\uff0c\u9700\u8981\u5c06\u5176\u4e2d\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_list = [[&#39;1&#39;, &#39;2&#39;], [&#39;3&#39;, &#39;4&#39;]]<\/p>\n<p>converted_list = [[int(x) for x in sublist] for sublist in nested_list]<\/p>\n<p>print(converted_list)  # \u8f93\u51fa: [[1, 2], [3, 4]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u5d4c\u5957\u7684\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u9996\u5148\u904d\u5386<code>nested_list<\/code>\u4e2d\u7684\u5b50\u5217\u8868\uff0c\u7136\u540e\u5bf9\u6bcf\u4e2a\u5b50\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5c06\u5b57\u5178\u4e2d\u7684\u503c\u8fdb\u884c\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5b57\u5178\uff0c\u9700\u8981\u5c06\u5176\u4e2d\u7684\u503c\u4ece\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dict_of_strings = {&#39;a&#39;: &#39;1&#39;, &#39;b&#39;: &#39;2&#39;, &#39;c&#39;: &#39;3&#39;}<\/p>\n<p>dict_of_ints = {k: int(v) for k, v in dict_of_strings.items()}<\/p>\n<p>print(dict_of_ints)  # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\uff0c\u904d\u5386<code>dict_of_strings<\/code>\u4e2d\u7684\u952e\u503c\u5bf9\uff0c\u5e76\u5c06\u503c\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5e38\u89c1\u9519\u8bef\u548c\u89e3\u51b3\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>1\u3001\u65e0\u6cd5\u8f6c\u6362\u7684\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u65e0\u6cd5\u8f6c\u6362\u7684\u5143\u7d20\uff0c\u4f8b\u5982\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8fc7\u6ee4\u6216\u5f02\u5e38\u5904\u7406\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h5>\u6761\u4ef6\u8fc7\u6ee4<\/h5>\n<\/p>\n<p><pre><code class=\"language-python\">list_of_strings = [&#39;1&#39;, &#39;2&#39;, &#39;three&#39;, &#39;4&#39;]<\/p>\n<p>list_of_ints = [int(x) for x in list_of_strings if x.isdigit()]<\/p>\n<p>print(list_of_ints)  # \u8f93\u51fa: [1, 2, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h5>\u5f02\u5e38\u5904\u7406<\/h5>\n<\/p>\n<p><pre><code class=\"language-python\">list_of_strings = [&#39;1&#39;, &#39;2&#39;, &#39;three&#39;, &#39;4&#39;]<\/p>\n<p>list_of_ints = []<\/p>\n<p>for x in list_of_strings:<\/p>\n<p>    try:<\/p>\n<p>        list_of_ints.append(int(x))<\/p>\n<p>    except ValueError:<\/p>\n<p>        continue<\/p>\n<p>print(list_of_ints)  # \u8f93\u51fa: [1, 2, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u7c7b\u578b\u4e0d\u5339\u914d<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u65f6\uff0c\u786e\u4fdd\u6240\u6709\u5143\u7d20\u90fd\u53ef\u4ee5\u8f6c\u6362\u4e3a\u76ee\u6807\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u65f6\uff0c\u5fc5\u987b\u786e\u4fdd\u6240\u6709\u5b57\u7b26\u4e32\u90fd\u53ef\u4ee5\u8868\u793a\u4e3a\u6709\u6548\u7684\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list_of_strings = [&#39;1.1&#39;, &#39;2.2&#39;, &#39;three&#39;, &#39;4.4&#39;]<\/p>\n<p>list_of_floats = []<\/p>\n<p>for x in list_of_strings:<\/p>\n<p>    try:<\/p>\n<p>        list_of_floats.append(float(x))<\/p>\n<p>    except ValueError:<\/p>\n<p>        continue<\/p>\n<p>print(list_of_floats)  # \u8f93\u51fa: [1.1, 2.2, 4.4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5bf9\u5217\u8868\u5143\u7d20\u8fdb\u884c\u5f3a\u5236\u8f6c\u6362\u7684\u5e38\u7528\u65b9\u6cd5\u6709\u5217\u8868\u63a8\u5bfc\u5f0f\u3001map()\u51fd\u6570\u548cfor\u5faa\u73af\u3002<strong>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u6700\u7b80\u6d01\u548c\u9ad8\u6548\u7684\u65b9\u5f0f<\/strong>\uff0c\u9002\u7528\u4e8e\u5927\u90e8\u5206\u8f6c\u6362\u9700\u6c42\uff1bmap()\u51fd\u6570\u9002\u7528\u4e8e\u9700\u8981\u5c06\u4e00\u4e2a\u51fd\u6570\u5e94\u7528\u4e8e\u5217\u8868\u7684\u6bcf\u4e2a\u5143\u7d20\u7684\u60c5\u51b5\uff1bfor\u5faa\u73af\u5219\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u64cd\u4f5c\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u52a0\u5165\u66f4\u591a\u590d\u6742\u7684\u903b\u8f91\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u5217\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8f6c\u6362\u4e3a\u6574\u6570\uff1f<\/strong><br \/>\u8981\u5c06\u5217\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216<code>map()<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u4e32\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u8fd9\u6837\u8fdb\u884c\u8f6c\u6362\uff1a  <\/p>\n<pre><code class=\"language-python\">string_list = [&#39;1&#39;, &#39;2&#39;, &#39;3&#39;]\nint_list = [int(x) for x in string_list]\n# \u6216\u8005\nint_list = list(map(int, string_list))\n<\/code><\/pre>\n<p>\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u4f1a\u751f\u6210\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u5176\u4e2d\u6240\u6709\u5143\u7d20\u90fd\u88ab\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06\u4e0d\u540c\u7c7b\u578b\u7684\u5143\u7d20\u5f3a\u5236\u8f6c\u6362\u4e3a\u7279\u5b9a\u7c7b\u578b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5f3a\u5236\u8f6c\u6362\u7684\u53ef\u884c\u6027\u53d6\u51b3\u4e8e\u5143\u7d20\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u548c\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u4f46\u5982\u679c\u5c1d\u8bd5\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u201cabc\u201d\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5219\u4f1a\u5f15\u53d1\u9519\u8bef\u3002\u5728\u8fdb\u884c\u5f3a\u5236\u8f6c\u6362\u4e4b\u524d\uff0c\u786e\u4fdd\u5143\u7d20\u80fd\u591f\u6210\u529f\u8f6c\u6362\uff0c\u4ee5\u907f\u514d\u8fd0\u884c\u65f6\u5f02\u5e38\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5217\u8868\u4e2d\u65e0\u6cd5\u8f6c\u6362\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u5728\u5904\u7406\u9700\u8981\u8f6c\u6362\u7684\u5217\u8868\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u65e0\u6cd5\u8f6c\u6362\u7684\u5143\u7d20\u3002\u53ef\u4ee5\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>try...except<\/code>\u7ed3\u6784\u6765\u6355\u83b7\u8f6c\u6362\u9519\u8bef\uff0c\u5e76\u51b3\u5b9a\u5982\u4f55\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\u5143\u7d20\uff1a  <\/p>\n<pre><code class=\"language-python\">original_list = [&#39;1&#39;, &#39;2&#39;, &#39;abc&#39;, &#39;4.5&#39;]\nconverted_list = []\n\nfor item in original_list:\n    try:\n        converted_list.append(int(float(item)))  # \u5148\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\uff0c\u518d\u8f6c\u6362\u4e3a\u6574\u6570\n    except ValueError:\n        print(f&quot;\u65e0\u6cd5\u8f6c\u6362\u5143\u7d20: {item}&quot;)\n\n# \u6b64\u65f6converted_list\u4f1a\u5305\u542b\u53ef\u4ee5\u6210\u529f\u8f6c\u6362\u7684\u5143\u7d20\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u5728\u9047\u5230\u9519\u8bef\u65f6\u4e0d\u4f1a\u5d29\u6e83\uff0c\u540c\u65f6\u4e5f\u80fd\u5904\u7406\u5176\u4ed6\u53ef\u8f6c\u6362\u7684\u5143\u7d20\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5217\u8868\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001map()\u51fd\u6570\u3001for\u5faa\u73af\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5f3a\u5236\u8f6c\u6362\u3002\u5176\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f [&hellip;]","protected":false},"author":3,"featured_media":1056250,"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\/1056236"}],"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=1056236"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1056236\/revisions"}],"predecessor-version":[{"id":1056252,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1056236\/revisions\/1056252"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1056250"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1056236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1056236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1056236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}