{"id":1078362,"date":"2025-01-08T12:10:52","date_gmt":"2025-01-08T04:10:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1078362.html"},"modified":"2025-01-08T12:10:54","modified_gmt":"2025-01-08T04:10:54","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e8%ae%a1%e7%ae%97%e6%b0%b4%e4%bb%99%e8%8a%b1%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1078362.html","title":{"rendered":"\u5982\u4f55\u7528python\u8ba1\u7b97\u6c34\u4ed9\u82b1\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181943\/a5fbe69f-f559-4091-9238-41fd013317ae.webp\" alt=\"\u5982\u4f55\u7528python\u8ba1\u7b97\u6c34\u4ed9\u82b1\u6570\" \/><\/p>\n<p><p> <strong>\u7528Python\u8ba1\u7b97\u6c34\u4ed9\u82b1\u6570\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528for\u5faa\u73af\u3001\u4f7f\u7528while\u5faa\u73af\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7for\u5faa\u73af\u6765\u9010\u4e2a\u68c0\u9a8c\u6bcf\u4e00\u4e2a\u6570\u5b57\u662f\u5426\u6ee1\u8db3\u6c34\u4ed9\u82b1\u6570\u7684\u5b9a\u4e49\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u6765\u8ba1\u7b97\u6c34\u4ed9\u82b1\u6570\u3002<\/p>\n<\/p>\n<p><p>\u6c34\u4ed9\u82b1\u6570\uff08Narcissistic number\uff0c\u4e5f\u79f0\u4e3a\u81ea\u604b\u6570\u3001\u81ea\u5e42\u6570\u6216\u963f\u59c6\u65af\u7279\u6717\u6570\uff09\u662f\u6307\u4e00\u4e2an\u4f4d\u7684\u6b63\u6574\u6570\uff0c\u5176\u5404\u4e2a\u4f4d\u4e0a\u7684\u6570\u5b57\u7684n\u6b21\u5e42\u4e4b\u548c\u7b49\u4e8e\u5b83\u672c\u8eab\u3002\u6bd4\u5982153\u662f\u4e00\u4e2a\u6c34\u4ed9\u82b1\u6570\uff0c\u56e0\u4e3a1^3 + 5^3 + 3^3 = 153\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528for\u5faa\u73af\u8ba1\u7b97\u6c34\u4ed9\u82b1\u6570<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528for\u5faa\u73af\u662f\u8ba1\u7b97\u6c34\u4ed9\u82b1\u6570\u7684\u5e38\u89c1\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u67d0\u4e2a\u8303\u56f4\u5185\u7684\u6240\u6709\u6570\uff0c\u5e76\u9010\u4e2a\u5224\u65ad\u5b83\u4eec\u662f\u5426\u662f\u6c34\u4ed9\u82b1\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_narcissistic(number):<\/p>\n<p>    num_str = str(number)<\/p>\n<p>    num_len = len(num_str)<\/p>\n<p>    sum_of_powers = sum(int(digit)  num_len for digit in num_str)<\/p>\n<p>    return sum_of_powers == number<\/p>\n<p>def find_narcissistic_numbers(start, end):<\/p>\n<p>    result = []<\/p>\n<p>    for number in range(start, end + 1):<\/p>\n<p>        if is_narcissistic(number):<\/p>\n<p>            result.append(number)<\/p>\n<p>    return result<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u67e5\u627e100\u5230999\u4e4b\u95f4\u7684\u6240\u6709\u6c34\u4ed9\u82b1\u6570<\/strong><\/h2>\n<p>narcissistic_numbers = find_narcissistic_numbers(100, 999)<\/p>\n<p>print(narcissistic_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>is_narcissistic<\/code>\u51fd\u6570\u7528\u4e8e\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u662f\u6c34\u4ed9\u82b1\u6570\uff0c\u800c<code>find_narcissistic_numbers<\/code>\u51fd\u6570\u5219\u7528\u4e8e\u5728\u6307\u5b9a\u8303\u56f4\u5185\u67e5\u627e\u6240\u6709\u7684\u6c34\u4ed9\u82b1\u6570\u3002\u901a\u8fc7\u8c03\u7528<code>find_narcissistic_numbers<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u627e\u5230\u5e76\u6253\u5370\u51fa\u6240\u6709\u7684\u6c34\u4ed9\u82b1\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528while\u5faa\u73af\u8ba1\u7b97\u6c34\u4ed9\u82b1\u6570<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528for\u5faa\u73af\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528while\u5faa\u73af\u6765\u8ba1\u7b97\u6c34\u4ed9\u82b1\u6570\u3002while\u5faa\u73af\u53ef\u4ee5\u5728\u66f4\u7075\u6d3b\u7684\u6761\u4ef6\u4e0b\u7ec8\u6b62\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_narcissistic(number):<\/p>\n<p>    num_str = str(number)<\/p>\n<p>    num_len = len(num_str)<\/p>\n<p>    sum_of_powers = sum(int(digit)  num_len for digit in num_str)<\/p>\n<p>    return sum_of_powers == number<\/p>\n<p>def find_narcissistic_numbers_while(start, end):<\/p>\n<p>    result = []<\/p>\n<p>    number = start<\/p>\n<p>    while number &lt;= end:<\/p>\n<p>        if is_narcissistic(number):<\/p>\n<p>            result.append(number)<\/p>\n<p>        number += 1<\/p>\n<p>    return result<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u67e5\u627e100\u5230999\u4e4b\u95f4\u7684\u6240\u6709\u6c34\u4ed9\u82b1\u6570<\/strong><\/h2>\n<p>narcissistic_numbers = find_narcissistic_numbers_while(100, 999)<\/p>\n<p>print(narcissistic_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0efor\u5faa\u73af\u7c7b\u4f3c\uff0cwhile\u5faa\u73af\u4e5f\u53ef\u4ee5\u6210\u529f\u5730\u627e\u5230\u6307\u5b9a\u8303\u56f4\u5185\u7684\u6240\u6709\u6c34\u4ed9\u82b1\u6570\u3002\u8fd9\u91cc\u7684<code>find_narcissistic_numbers_while<\/code>\u51fd\u6570\u4f7f\u7528\u4e86while\u5faa\u73af\u6765\u904d\u5386\u6240\u6709\u7684\u6570\u5b57\uff0c\u5e76\u5c06\u6ee1\u8db3\u6761\u4ef6\u7684\u6570\u6dfb\u52a0\u5230\u7ed3\u679c\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8ba1\u7b97\u6c34\u4ed9\u82b1\u6570<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662fPython\u4e2d\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u5217\u8868\u3002\u6211\u4eec\u4e5f\u53ef\u4ee5\u5229\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u8ba1\u7b97\u6c34\u4ed9\u82b1\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_narcissistic(number):<\/p>\n<p>    num_str = str(number)<\/p>\n<p>    num_len = len(num_str)<\/p>\n<p>    sum_of_powers = sum(int(digit)  num_len for digit in num_str)<\/p>\n<p>    return sum_of_powers == number<\/p>\n<p>def find_narcissistic_numbers_comprehension(start, end):<\/p>\n<p>    return [number for number in range(start, end + 1) if is_narcissistic(number)]<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u67e5\u627e100\u5230999\u4e4b\u95f4\u7684\u6240\u6709\u6c34\u4ed9\u82b1\u6570<\/strong><\/h2>\n<p>narcissistic_numbers = find_narcissistic_numbers_comprehension(100, 999)<\/p>\n<p>print(narcissistic_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u7b80\u5316\u4ee3\u7801\uff0c\u4f7f\u5176\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u5feb\u5730\u627e\u5230\u6307\u5b9a\u8303\u56f4\u5185\u7684\u6240\u6709\u6c34\u4ed9\u82b1\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f18\u5316\u6c34\u4ed9\u82b1\u6570\u8ba1\u7b97<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1\u4e0a\u8ff0\u65b9\u6cd5\u5df2\u7ecf\u53ef\u4ee5\u8ba1\u7b97\u6c34\u4ed9\u82b1\u6570\uff0c\u4f46\u6211\u4eec\u8fd8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\u4ee3\u7801\uff0c\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7f13\u5b58\u6765\u5b58\u50a8\u4e2d\u95f4\u8ba1\u7b97\u7ed3\u679c\uff0c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_narcissistic_optimized(number, cache={}):<\/p>\n<p>    num_str = str(number)<\/p>\n<p>    num_len = len(num_str)<\/p>\n<p>    if num_len not in cache:<\/p>\n<p>        cache[num_len] = [int(digit)  num_len for digit in &#39;0123456789&#39;]<\/p>\n<p>    sum_of_powers = sum(cache[num_len][int(digit)] for digit in num_str)<\/p>\n<p>    return sum_of_powers == number<\/p>\n<p>def find_narcissistic_numbers_optimized(start, end):<\/p>\n<p>    return [number for number in range(start, end + 1) if is_narcissistic_optimized(number)]<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u67e5\u627e100\u5230999\u4e4b\u95f4\u7684\u6240\u6709\u6c34\u4ed9\u82b1\u6570<\/strong><\/h2>\n<p>narcissistic_numbers = find_narcissistic_numbers_optimized(100, 999)<\/p>\n<p>print(narcissistic_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f18\u5316\u7248\u672c\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a\u7f13\u5b58\uff08cache\uff09\u6765\u5b58\u50a8\u6bcf\u4e2a\u4f4d\u4e0a\u7684\u6570\u5b57\u7684n\u6b21\u5e42\uff0c\u4ece\u800c\u907f\u514d\u4e86\u91cd\u590d\u8ba1\u7b97\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u4f7f\u7528Python\u6765\u8ba1\u7b97\u6c34\u4ed9\u82b1\u6570\u3002<strong>\u4f7f\u7528for\u5faa\u73af\u3001while\u5faa\u73af\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u90fd\u662f\u53ef\u884c\u7684\u65b9\u6cd5\uff0c\u5404\u6709\u4f18\u7f3a\u70b9<\/strong>\u3002\u5bf9\u4e8e\u6027\u80fd\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7f13\u5b58\u6765\u8fdb\u4e00\u6b65\u4f18\u5316\u4ee3\u7801\u3002\u603b\u4e4b\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u548c\u4f18\u5316\u7b56\u7565\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u8ba1\u7b97\u6c34\u4ed9\u82b1\u6570\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u6c34\u4ed9\u82b1\u6570\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p>\u6c34\u4ed9\u82b1\u6570\u662f\u6307\u4e00\u4e2an\u4f4d\u6570\uff0c\u5176\u6bcf\u4e2a\u4f4d\u4e0a\u7684\u6570\u5b57\u7684n\u6b21\u65b9\u548c\u7b49\u4e8e\u5b83\u672c\u8eab\u3002\u4f8b\u5982\uff0c153\u662f\u4e00\u4e2a\u4e09\u4f4d\u6c34\u4ed9\u82b1\u6570\uff0c\u56e0\u4e3a1^3 + 5^3 + 3^3 = 153\u3002\u4e86\u89e3\u6c34\u4ed9\u82b1\u6570\u7684\u5b9a\u4e49\u6709\u52a9\u4e8e\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u7528Python\u8fdb\u884c\u76f8\u5173\u8ba1\u7b97\u3002<\/p>\n<p><strong>\u7528Python\u8ba1\u7b97\u6c34\u4ed9\u82b1\u6570\u9700\u8981\u54ea\u4e9b\u6b65\u9aa4\uff1f<\/strong><\/p>\n<p>\u8ba1\u7b97\u6c34\u4ed9\u82b1\u6570\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\uff1a\u5faa\u73af\u904d\u5386\u4e00\u5b9a\u8303\u56f4\u5185\u7684\u6570\u5b57\uff0c\u5206\u89e3\u6bcf\u4e2a\u6570\u5b57\u7684\u5404\u4e2a\u6570\u5b57\u4f4d\uff0c\u8ba1\u7b97\u6bcf\u4e2a\u6570\u5b57\u4f4d\u7684n\u6b21\u65b9\u548c\uff0c\u6700\u540e\u5224\u65ad\u8fd9\u4e2a\u548c\u662f\u5426\u7b49\u4e8e\u539f\u6570\u3002\u4f7f\u7528Python\u7684\u5faa\u73af\u548c\u6570\u5b66\u8fd0\u7b97\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5b9e\u73b0\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u7528Python\u7684\u54ea\u4e9b\u51fd\u6570\u6765\u7b80\u5316\u6c34\u4ed9\u82b1\u6570\u7684\u8ba1\u7b97\u8fc7\u7a0b\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>map()<\/code>\u51fd\u6570\u6765\u7b80\u5316\u6570\u5b57\u4f4d\u7684\u63d0\u53d6\u548c\u6b21\u65b9\u8ba1\u7b97\uff0c\u540c\u65f6\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u63d0\u9ad8\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u548c\u53ef\u8bfb\u6027\u3002\u6b64\u5916\uff0c<code>str()<\/code>\u51fd\u6570\u53ef\u7528\u4e8e\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u4ee5\u4fbf\u8f7b\u677e\u8bbf\u95ee\u6bcf\u4e00\u4f4d\u6570\u5b57\u3002\u5229\u7528\u8fd9\u4e9b\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u9ad8\u6548\u4e14\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<p><strong>\u6c34\u4ed9\u82b1\u6570\u7684\u5e94\u7528\u573a\u666f\u6709\u54ea\u4e9b\uff1f<\/strong><\/p>\n<p>\u6c34\u4ed9\u82b1\u6570\u4e0d\u4ec5\u662f\u6570\u5b66\u4e2d\u7684\u4e00\u4e2a\u6709\u8da3\u6982\u5ff5\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u7f16\u7a0b\u7ec3\u4e60\uff0c\u5e2e\u52a9\u521d\u5b66\u8005\u7406\u89e3\u5faa\u73af\u3001\u6761\u4ef6\u5224\u65ad\u548c\u51fd\u6570\u7684\u4f7f\u7528\u3002\u6b64\u5916\uff0c\u5728\u67d0\u4e9b\u7b97\u6cd5\u548c\u6e38\u620f\u4e2d\uff0c\u6c34\u4ed9\u82b1\u6570\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u6311\u6218\uff0c\u589e\u52a0\u8da3\u5473\u6027\u548c\u96be\u5ea6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u8ba1\u7b97\u6c34\u4ed9\u82b1\u6570\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528for\u5faa\u73af\u3001\u4f7f\u7528while\u5faa\u73af\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a [&hellip;]","protected":false},"author":3,"featured_media":1078372,"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\/1078362"}],"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=1078362"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1078362\/revisions"}],"predecessor-version":[{"id":1078375,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1078362\/revisions\/1078375"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1078372"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1078362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1078362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1078362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}