{"id":1111650,"date":"2025-01-08T17:31:15","date_gmt":"2025-01-08T09:31:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1111650.html"},"modified":"2025-01-08T17:31:17","modified_gmt":"2025-01-08T09:31:17","slug":"python%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e6%95%b0%e5%ad%97%e7%9a%84%e6%af%8f%e4%b8%80%e4%bd%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1111650.html","title":{"rendered":"python\u5982\u4f55\u83b7\u53d6\u6570\u5b57\u7684\u6bcf\u4e00\u4f4d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074023\/dc58996b-cf48-4579-9d46-672d66b9a448.webp\" alt=\"python\u5982\u4f55\u83b7\u53d6\u6570\u5b57\u7684\u6bcf\u4e00\u4f4d\" \/><\/p>\n<p><p> <strong>Python\u83b7\u53d6\u6570\u5b57\u6bcf\u4e00\u4f4d\u7684\u65b9\u6cd5\u6709\uff1a\u8f6c\u5b57\u7b26\u4e32\u3001\u6570\u5b66\u65b9\u6cd5\u3001\u9012\u5f52\u65b9\u6cd5\u3002<\/strong> \u8fd9\u91cc\u6211\u4eec\u91cd\u70b9\u5c55\u5f00<strong>\u8f6c\u5b57\u7b26\u4e32<\/strong>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u5c06\u6570\u5b57\u8f6c\u4e3a\u5b57\u7b26\u4e32\u662f\u6700\u76f4\u63a5\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5c06\u4e00\u4e2a\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u518d\u5c06\u5176\u8f6c\u6362\u56de\u6574\u6570\uff0c\u8fd9\u6837\u5c31\u80fd\u83b7\u53d6\u6570\u5b57\u7684\u6bcf\u4e00\u4f4d\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u6570\u5b571234\uff0c\u901a\u8fc7\u8f6c\u5b57\u7b26\u4e32\u540e\u5f97\u5230\u7684\u5b57\u7b26\u4e32\u201c1234\u201d\uff0c\u518d\u8fdb\u884c\u904d\u5386\u5e76\u8f6c\u6362\u56de\u6574\u6570\uff0c\u5c31\u53ef\u4ee5\u5f97\u52301, 2, 3, 4\u8fd9\u56db\u4e2a\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684Python\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 1234<\/p>\n<p>digits = [int(digit) for digit in str(number)]<\/p>\n<p>print(digits)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u9996\u5148\u5c06\u6570\u5b571234\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u201c1234\u201d\uff0c\u7136\u540e\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u6bcf\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u6700\u540e\u5f97\u5230\u5305\u542b\u6bcf\u4e00\u4f4d\u6570\u5b57\u7684\u5217\u8868\uff1a[1, 2, 3, 4]\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001\u8f6c\u5b57\u7b26\u4e32\u65b9\u6cd5<\/h2>\n<\/p>\n<p><h3>\u8f6c\u5b57\u7b26\u4e32\u65b9\u6cd5\u7684\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u518d\u5c06\u5176\u8f6c\u6362\u56de\u6574\u6570\u3002\u5177\u4f53\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 56789<\/p>\n<p>digits = [int(digit) for digit in str(number)]<\/p>\n<p>print(digits)  # \u8f93\u51fa: [5, 6, 7, 8, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\uff0c\u4ee3\u7801\u7b80\u6d01\u660e\u4e86\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u83b7\u53d6\u6570\u5b57\u7684\u6bcf\u4e00\u4f4d\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u5904\u7406\u8f83\u77ed\u7684\u6570\u5b57\uff0c\u6216\u8005\u9700\u8981\u5feb\u901f\u5b9e\u73b0\u7684\u573a\u666f\u3002\u7531\u4e8ePython\u7684\u5b57\u7b26\u4e32\u5904\u7406\u901f\u5ea6\u8f83\u5feb\uff0c\u56e0\u6b64\u8fd9\u79cd\u65b9\u6cd5\u4e5f\u5177\u6709\u8f83\u9ad8\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u6570\u5b66\u65b9\u6cd5<\/h2>\n<\/p>\n<p><h3>\u6570\u5b66\u65b9\u6cd5\u7684\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u6570\u5b66\u65b9\u6cd5\u662f\u901a\u8fc7\u4e0d\u65ad\u5bf9\u6570\u5b57\u53d6\u6a21\u548c\u6574\u9664\u7684\u65b9\u5f0f\u6765\u63d0\u53d6\u6bcf\u4e00\u4f4d\u6570\u5b57\u3002\u8fd9\u79cd\u65b9\u6cd5\u65e0\u9700\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u76f4\u63a5\u5728\u6570\u5b57\u4e0a\u8fdb\u884c\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_digits(number):<\/p>\n<p>    digits = []<\/p>\n<p>    while number &gt; 0:<\/p>\n<p>        digits.append(number % 10)<\/p>\n<p>        number = number \/\/ 10<\/p>\n<p>    return digits[::-1]<\/p>\n<p>number = 56789<\/p>\n<p>digits = get_digits(number)<\/p>\n<p>print(digits)  # \u8f93\u51fa: [5, 6, 7, 8, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u6570\u5b66\u65b9\u6cd5\u9002\u7528\u4e8e\u5904\u7406\u8f83\u5927\u6570\u5b57\uff0c\u6216\u8005\u5728\u5bf9\u6027\u80fd\u6709\u66f4\u9ad8\u8981\u6c42\u7684\u573a\u666f\u4e0b\u4f7f\u7528\u3002\u901a\u8fc7\u76f4\u63a5\u5728\u6570\u5b57\u4e0a\u8fdb\u884c\u64cd\u4f5c\uff0c\u53ef\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\uff0c\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u9012\u5f52\u65b9\u6cd5<\/h2>\n<\/p>\n<p><h3>\u9012\u5f52\u65b9\u6cd5\u7684\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u65b9\u6cd5\u901a\u8fc7\u9012\u5f52\u8c03\u7528\u81ea\u8eab\u6765\u9010\u4f4d\u63d0\u53d6\u6570\u5b57\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_digits_recursive(number):<\/p>\n<p>    if number == 0:<\/p>\n<p>        return []<\/p>\n<p>    else:<\/p>\n<p>        return get_digits_recursive(number \/\/ 10) + [number % 10]<\/p>\n<p>number = 56789<\/p>\n<p>digits = get_digits_recursive(number)<\/p>\n<p>print(digits)  # \u8f93\u51fa: [5, 6, 7, 8, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u65b9\u6cd5\u9002\u7528\u4e8e\u5bf9\u9012\u5f52\u7b97\u6cd5\u6709\u9700\u6c42\u7684\u573a\u666f\uff0c\u4f8b\u5982\u5728\u4e00\u4e9b\u7279\u5b9a\u7684\u7b97\u6cd5\u9898\u4e2d\uff0c\u9012\u5f52\u65b9\u6cd5\u53ef\u80fd\u66f4\u7b26\u5408\u9898\u76ee\u7684\u8981\u6c42\u6216\u9650\u5236\u3002\u4e0d\u8fc7\uff0c\u9700\u8981\u6ce8\u610f\u9012\u5f52\u65b9\u6cd5\u53ef\u80fd\u4f1a\u53d7\u5230Python\u9012\u5f52\u6df1\u5ea6\u9650\u5236\u7684\u5f71\u54cd\uff0c\u4e0d\u9002\u5408\u5904\u7406\u8fc7\u5927\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u5bf9\u6bd4\u4e0e\u603b\u7ed3<\/h2>\n<\/p>\n<p><h3>\u65b9\u6cd5\u5bf9\u6bd4<\/h3>\n<\/p>\n<ol>\n<li><strong>\u8f6c\u5b57\u7b26\u4e32\u65b9\u6cd5<\/strong>\uff1a\u4ee3\u7801\u7b80\u6d01\uff0c\u9002\u5408\u5904\u7406\u8f83\u77ed\u7684\u6570\u5b57\uff0c\u6548\u7387\u8f83\u9ad8\u3002<\/li>\n<li><strong>\u6570\u5b66\u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u5904\u7406\u8f83\u5927\u6570\u5b57\uff0c\u6027\u80fd\u8f83\u4f18\u3002<\/li>\n<li><strong>\u9012\u5f52\u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u7279\u5b9a\u573a\u666f\uff0c\u4f46\u53ef\u80fd\u53d7\u9012\u5f52\u6df1\u5ea6\u9650\u5236\u3002<\/li>\n<\/ol>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u83b7\u53d6\u6570\u5b57\u6bcf\u4e00\u4f4d\u7684\u65b9\u6cd5\u65f6\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u573a\u666f\u6765\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5982\u679c\u8ffd\u6c42\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u548c\u53ef\u8bfb\u6027\uff0c\u53ef\u4ee5\u9009\u62e9\u8f6c\u5b57\u7b26\u4e32\u65b9\u6cd5\uff1b\u5982\u679c\u9700\u8981\u5904\u7406\u8f83\u5927\u6570\u5b57\uff0c\u4e14\u5bf9\u6027\u80fd\u6709\u66f4\u9ad8\u8981\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u6570\u5b66\u65b9\u6cd5\uff1b\u5728\u7279\u5b9a\u7684\u9012\u5f52\u7b97\u6cd5\u9700\u6c42\u573a\u666f\u4e0b\uff0c\u53ef\u4ee5\u9009\u62e9\u9012\u5f52\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u6709\u6548\u5730\u83b7\u53d6\u6570\u5b57\u7684\u6bcf\u4e00\u4f4d\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8fd8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u4f18\u5316\u548c\u6539\u8fdb\uff0c\u4ee5\u8fbe\u5230\u66f4\u597d\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u4e00\u4e2a\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u4ee5\u4fbf\u83b7\u53d6\u6bcf\u4e00\u4f4d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u6765\u8f7b\u677e\u83b7\u53d6\u6bcf\u4e00\u4f4d\u6570\u5b57\u3002\u4f7f\u7528<code>str()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u63a5\u7740\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u6216\u5faa\u73af\u904d\u5386\u6bcf\u4e00\u4f4d\u3002\u4f8b\u5982\uff0c<code>number = 12345<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>str(number)<\/code>\u5c06\u5176\u8f6c\u6362\uff0c\u7136\u540e\u4f7f\u7528<code>for digit in str(number)<\/code>\u6765\u83b7\u53d6\u6bcf\u4e00\u4f4d\u6570\u5b57\u3002<\/p>\n<p><strong>\u4f7f\u7528\u5217\u8868\u89e3\u6790\u83b7\u53d6\u6570\u5b57\u7684\u6bcf\u4e00\u4f4d\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u65b9\u5f0f\u6765\u83b7\u53d6\u6570\u5b57\u7684\u6bcf\u4e00\u4f4d\u3002\u901a\u8fc7<code>digits = [int(d) for d in str(number)]<\/code>\uff0c\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b8c\u6210\u8f6c\u6362\u548c\u63d0\u53d6\uff0c\u6bcf\u4e00\u4f4d\u6570\u5b57\u4f1a\u88ab\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u4ee3\u7801\u7b80\u6d01\uff0c\u800c\u4e14\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u6570\u5b66\u8fd0\u7b97\u800c\u975e\u5b57\u7b26\u4e32\u5904\u7406\u6765\u83b7\u53d6\u6570\u5b57\u7684\u6bcf\u4e00\u4f4d\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u6570\u5b66\u8fd0\u7b97\u6765\u83b7\u53d6\u6570\u5b57\u7684\u6bcf\u4e00\u4f4d\u3002\u53ef\u4ee5\u4f7f\u7528\u53d6\u4f59\u548c\u6574\u9664\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u548c<code>%<\/code>\u8fd0\u7b97\u7b26\u83b7\u53d6\u6700\u540e\u4e00\u4f4d\u6570\u5b57\uff0c\u7136\u540e\u4f7f\u7528<code>\/\/<\/code>\u8fd0\u7b97\u7b26\u53bb\u6389\u6700\u540e\u4e00\u4f4d\uff0c\u91cd\u590d\u8fd9\u4e2a\u8fc7\u7a0b\u76f4\u5230\u6570\u5b57\u4e3a0\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u5b57\u7b26\u4e32\u8f6c\u6362\uff0c\u76f4\u63a5\u64cd\u4f5c\u6570\u5b57\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u83b7\u53d6\u6570\u5b57\u6bcf\u4e00\u4f4d\u7684\u65b9\u6cd5\u6709\uff1a\u8f6c\u5b57\u7b26\u4e32\u3001\u6570\u5b66\u65b9\u6cd5\u3001\u9012\u5f52\u65b9\u6cd5\u3002 \u8fd9\u91cc\u6211\u4eec\u91cd\u70b9\u5c55\u5f00\u8f6c\u5b57\u7b26\u4e32\u65b9\u6cd5\u3002 \u5c06\u6570\u5b57\u8f6c\u4e3a [&hellip;]","protected":false},"author":3,"featured_media":1111657,"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\/1111650"}],"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=1111650"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1111650\/revisions"}],"predecessor-version":[{"id":1111658,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1111650\/revisions\/1111658"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1111657"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1111650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1111650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1111650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}