{"id":1098393,"date":"2025-01-08T15:20:49","date_gmt":"2025-01-08T07:20:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1098393.html"},"modified":"2025-01-08T15:20:51","modified_gmt":"2025-01-08T07:20:51","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%88%97%e8%a1%a8%e8%bd%ac%e6%8d%a2%e4%b8%ba%e6%95%b4%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1098393.html","title":{"rendered":"python\u5982\u4f55\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6574\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212730\/8e841bf4-95cd-4990-b005-3500356041e7.webp\" alt=\"python\u5982\u4f55\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6574\u6570\" \/><\/p>\n<p><p> <strong>Python\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6574\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd<\/strong>\uff0c\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u8fde\u63a5\u548c\u6574\u6570\u8f6c\u6362\u3001\u5217\u8868\u89e3\u6790\u3001map\u51fd\u6570\u7b49\u65b9\u6cd5\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u6765\u8bf4\u660e\u5982\u4f55\u5b9e\u73b0\u5217\u8868\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u8fde\u63a5\u548c\u6574\u6570\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5217\u8868\u4e2d\u7684\u5143\u7d20\u90fd\u662f\u5355\u4e00\u7684\u6570\u5b57\u5b57\u7b26\u65f6\u3002\u6211\u4eec\u53ef\u4ee5\u5148\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u5c06\u8fd9\u4e9b\u5b57\u7b26\u4e32\u8fde\u63a5\u8d77\u6765\uff0c\u6700\u540e\u518d\u5c06\u8fde\u63a5\u540e\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>list_of_digits = [1, 2, 3, 4]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u8fde\u63a5<\/strong><\/h2>\n<p>str_digits = &#39;&#39;.join(map(str, list_of_digits))<\/p>\n<h2><strong>\u5c06\u8fde\u63a5\u540e\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570<\/strong><\/h2>\n<p>integer_value = int(str_digits)<\/p>\n<p>print(integer_value)  # \u8f93\u51fa\uff1a1234<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\uff08List Comprehension\uff09<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u800c\u5f3a\u5927\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u4e00\u4e2a\u8868\u8fbe\u5f0f\u5185\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5217\u8868\u89e3\u6790\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u518d\u8fdb\u884c\u8fde\u63a5\u548c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>list_of_digits = [1, 2, 3, 4]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u89e3\u6790\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u8fde\u63a5<\/strong><\/h2>\n<p>str_digits = &#39;&#39;.join([str(digit) for digit in list_of_digits])<\/p>\n<h2><strong>\u5c06\u8fde\u63a5\u540e\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570<\/strong><\/h2>\n<p>integer_value = int(str_digits)<\/p>\n<p>print(integer_value)  # \u8f93\u51fa\uff1a1234<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528map\u51fd\u6570<\/h3>\n<\/p>\n<p><p>map\u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u4e2a\u51fd\u6570\u5e94\u7528\u4e8e\u4e00\u4e2a\u5e8f\u5217\uff08\u5982\u5217\u8868\uff09\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528map\u51fd\u6570\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u518d\u8fdb\u884c\u8fde\u63a5\u548c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>list_of_digits = [1, 2, 3, 4]<\/p>\n<h2><strong>\u4f7f\u7528map\u51fd\u6570\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u8fde\u63a5<\/strong><\/h2>\n<p>str_digits = &#39;&#39;.join(map(str, list_of_digits))<\/p>\n<h2><strong>\u5c06\u8fde\u63a5\u540e\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570<\/strong><\/h2>\n<p>integer_value = int(str_digits)<\/p>\n<p>print(integer_value)  # \u8f93\u51fa\uff1a1234<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528reduce\u51fd\u6570<\/h3>\n<\/p>\n<p><p>reduce\u51fd\u6570\u53ef\u4ee5\u5bf9\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u7d2f\u79ef\u8ba1\u7b97\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528reduce\u51fd\u6570\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u7d2f\u79ef\u6210\u4e00\u4e2a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import reduce<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>list_of_digits = [1, 2, 3, 4]<\/p>\n<h2><strong>\u4f7f\u7528reduce\u51fd\u6570\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u7d2f\u79ef\u6210\u4e00\u4e2a\u6574\u6570<\/strong><\/h2>\n<p>integer_value = reduce(lambda x, y: x * 10 + y, list_of_digits)<\/p>\n<p>print(integer_value)  # \u8f93\u51fa\uff1a1234<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u5217\u8868\u8f6c\u6362\u4e3a\u6574\u6570\u7684\u529f\u80fd\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u7075\u6d3b\u8c03\u6574\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>def list_to_integer(lst):<\/p>\n<p>    result = 0<\/p>\n<p>    for digit in lst:<\/p>\n<p>        result = result * 10 + digit<\/p>\n<p>    return result<\/p>\n<p>list_of_digits = [1, 2, 3, 4]<\/p>\n<p>integer_value = list_to_integer(list_of_digits)<\/p>\n<p>print(integer_value)  # \u8f93\u51fa\uff1a1234<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u9012\u5f52\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u662f\u4e00\u79cd\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5229\u7528\u9012\u5f52\u51fd\u6570\u6765\u5b9e\u73b0\u5217\u8868\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>def list_to_integer_recursive(lst):<\/p>\n<p>    if not lst:<\/p>\n<p>        return 0<\/p>\n<p>    return lst[0] * (10  (len(lst) - 1)) + list_to_integer_recursive(lst[1:])<\/p>\n<p>list_of_digits = [1, 2, 3, 4]<\/p>\n<p>integer_value = list_to_integer_recursive(list_of_digits)<\/p>\n<p>print(integer_value)  # \u8f93\u51fa\uff1a1234<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u5305\u542b\u975e\u6574\u6570\u5143\u7d20\u7684\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u53ef\u80fd\u5e76\u4e0d\u90fd\u662f\u6574\u6570\u5b57\u7b26\u3002\u6211\u4eec\u53ef\u4ee5\u5148\u8fc7\u6ee4\u6389\u975e\u6574\u6570\u5b57\u7b26\uff0c\u7136\u540e\u518d\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>def filter_and_convert(lst):<\/p>\n<p>    filtered_list = [int(digit) for digit in lst if isinstance(digit, int)]<\/p>\n<p>    str_digits = &#39;&#39;.join(map(str, filtered_list))<\/p>\n<p>    return int(str_digits)<\/p>\n<p>list_with_non_integers = [1, &#39;a&#39;, 2, &#39;b&#39;, 3, 4]<\/p>\n<p>integer_value = filter_and_convert(list_with_non_integers)<\/p>\n<p>print(integer_value)  # \u8f93\u51fa\uff1a1234<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5904\u7406\u7a7a\u5217\u8868\u548c\u5f02\u5e38\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5217\u8868\u8f6c\u6362\u4e3a\u6574\u6570\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u8003\u8651\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\uff0c\u5982\u7a7a\u5217\u8868\u548c\u5f02\u5e38\u60c5\u51b5\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u5f02\u5e38\u5904\u7406\u6765\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>def <a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_list_to_integer(lst):<\/p>\n<p>    if not lst:<\/p>\n<p>        return 0<\/p>\n<p>    try:<\/p>\n<p>        str_digits = &#39;&#39;.join(map(str, lst))<\/p>\n<p>        return int(str_digits)<\/p>\n<p>    except ValueError:<\/p>\n<p>        return 0<\/p>\n<p>empty_list = []<\/p>\n<p>invalid_list = [1, &#39;a&#39;, 2, None]<\/p>\n<p>print(safe_list_to_integer(empty_list))  # \u8f93\u51fa\uff1a0<\/p>\n<p>print(safe_list_to_integer(invalid_list))  # \u8f93\u51fa\uff1a0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5e76\u5e94\u5bf9\u5404\u79cd\u7279\u6b8a\u60c5\u51b5\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\u5c06Python\u5217\u8868\u4e2d\u7684\u5143\u7d20\u5408\u5e76\u4e3a\u4e00\u4e2a\u6574\u6570\uff1f<\/strong><br \/>\u8981\u5c06\u5217\u8868\u4e2d\u7684\u6570\u5b57\u5143\u7d20\u5408\u5e76\u4e3a\u4e00\u4e2a\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u8fde\u63a5\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u8fde\u63a5\uff0c\u518d\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, 2, 3]\nresult = int(&#39;&#39;.join(map(str, my_list)))\nprint(result)  # \u8f93\u51fa123\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5217\u8868\u4e2d\u5143\u7d20\u7684\u987a\u5e8f\u5f88\u91cd\u8981\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5c06\u5305\u542b\u6570\u5b57\u548c\u975e\u6570\u5b57\u7684\u5217\u8868\u8f6c\u6362\u4e3a\u6574\u6570\uff1f<\/strong><br \/>\u5982\u679c\u5217\u8868\u4e2d\u540c\u65f6\u5305\u542b\u6570\u5b57\u548c\u975e\u6570\u5b57\u5143\u7d20\uff0c\u53ef\u4ee5\u5148\u8fc7\u6ee4\u51fa\u6570\u5b57\u5143\u7d20\uff0c\u7136\u540e\u518d\u8fdb\u884c\u8f6c\u6362\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, &#39;a&#39;, 2, &#39;b&#39;, 3]\nresult = int(&#39;&#39;.join(map(str, filter(lambda x: isinstance(x, int), my_list))))\nprint(result)  # \u8f93\u51fa123\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u53ea\u63d0\u53d6\u6570\u5b57\uff0c\u5e76\u5ffd\u7565\u5176\u4ed6\u7c7b\u578b\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>Python\u4e2d\u662f\u5426\u53ef\u4ee5\u5c06\u5217\u8868\u4e2d\u7684\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6574\u6570\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u5217\u8868\uff0c\u5c06\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u53ef\u4ee5\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u6765\u5b8c\u6210\u8f6c\u6362\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1.2, 2.5, 3.8]\nresult = [int(num) for num in my_list]\nprint(result)  # \u8f93\u51fa[1, 2, 3]\n<\/code><\/pre>\n<p>\u8bf7\u6ce8\u610f\uff0c\u8fd9\u6837\u7684\u8f6c\u6362\u4f1a\u4e22\u5931\u5c0f\u6570\u90e8\u5206\uff0c\u7ed3\u679c\u662f\u5411\u4e0b\u53d6\u6574\u7684\u6574\u6570\u5217\u8868\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6574\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u8fde\u63a5\u548c\u6574\u6570\u8f6c\u6362\u3001\u5217\u8868\u89e3\u6790\u3001map\u51fd\u6570\u7b49\u65b9\u6cd5\u3002\u4e0b\u9762\u6211\u4eec\u5c06 [&hellip;]","protected":false},"author":3,"featured_media":1098400,"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\/1098393"}],"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=1098393"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098393\/revisions"}],"predecessor-version":[{"id":1098404,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098393\/revisions\/1098404"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1098400"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1098393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1098393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1098393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}