{"id":1185348,"date":"2025-01-15T19:39:00","date_gmt":"2025-01-15T11:39:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1185348.html"},"modified":"2025-01-15T19:39:29","modified_gmt":"2025-01-15T11:39:29","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e6%89%be%e5%90%8c%e6%9e%84%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1185348.html","title":{"rendered":"\u5982\u4f55\u7528python\u627e\u540c\u6784\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25134522\/5e8f2501-ba7a-4861-85e3-c80b277e272a.webp\" alt=\"\u5982\u4f55\u7528python\u627e\u540c\u6784\u6570\" \/><\/p>\n<p><p> <strong>\u7528Python\u627e\u540c\u6784\u6570\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u904d\u5386\u6240\u6709\u53ef\u80fd\u7684\u6570\u3001\u68c0\u67e5\u6bcf\u4e2a\u6570\u7684\u5e73\u65b9\u662f\u5426\u4fdd\u6301\u76f8\u540c\u7684\u6570\u5b57\u987a\u5e8f\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u64cd\u4f5c\u8fdb\u884c\u6bd4\u8f83\u3002<\/strong>\u5176\u4e2d\uff0c\u904d\u5386\u6240\u6709\u53ef\u80fd\u7684\u6570\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u4e00\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u627e\u540c\u6784\u6570\u7684\u5b9a\u4e49\u548c\u524d\u63d0<\/h3>\n<\/p>\n<p><p>\u540c\u6784\u6570\uff08Automorphic number\uff09\u662f\u6307\u4e00\u4e2a\u6570\u7684\u5e73\u65b9\u6570\u7684\u6700\u540e\u51e0\u4f4d\u6570\u5b57\u4e0e\u8be5\u6570\u672c\u8eab\u76f8\u540c\u3002\u4f8b\u5982\uff0c76\u7684\u5e73\u65b9\u662f5776\uff0c76\u7684\u6700\u540e\u4e24\u4f4d\u6570\u5b57\u4e0e76\u672c\u8eab\u4e00\u81f4\u3002\u56e0\u6b64\uff0c76\u662f\u4e00\u4e2a\u540c\u6784\u6570\u3002\u8981\u627e\u51fa\u540c\u6784\u6570\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u6bcf\u4e2a\u6570\u7684\u5e73\u65b9\u8fdb\u884c\u68c0\u67e5\uff0c\u786e\u5b9a\u5176\u672b\u5c3e\u662f\u5426\u4e0e\u8be5\u6570\u672c\u8eab\u76f8\u540c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u904d\u5386\u6240\u6709\u53ef\u80fd\u7684\u6570<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5b9a\u4e49\u540c\u6784\u6570\u7684\u8303\u56f4<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u6211\u4eec\u8981\u68c0\u67e5\u7684\u6570\u7684\u8303\u56f4\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u68c0\u67e5\u4ece0\u523010000\u4e4b\u95f4\u7684\u6240\u6709\u6570\u3002\u6839\u636e\u9700\u6c42\u7684\u4e0d\u540c\uff0c\u8303\u56f4\u53ef\u4ee5\u968f\u65f6\u8c03\u6574\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_automorphic_numbers(limit):<\/p>\n<p>    automorphic_numbers = []<\/p>\n<p>    for num in range(limit):<\/p>\n<p>        if is_automorphic(num):<\/p>\n<p>            automorphic_numbers.append(num)<\/p>\n<p>    return automorphic_numbers<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u68c0\u67e5\u6bcf\u4e2a\u6570\u7684\u5e73\u65b9\u662f\u5426\u4fdd\u6301\u76f8\u540c\u7684\u6570\u5b57\u987a\u5e8f<\/strong><\/li>\n<\/ol>\n<p><p>\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u51fd\u6570\u6765\u68c0\u67e5\u4e00\u4e2a\u6570\u662f\u5426\u662f\u540c\u6784\u6570\u3002\u8fd9\u6d89\u53ca\u5230\u5c06\u6570\u5e73\u65b9\uff0c\u7136\u540e\u68c0\u67e5\u5e73\u65b9\u6570\u7684\u6700\u540e\u51e0\u4f4d\u662f\u5426\u7b49\u4e8e\u539f\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_automorphic(num):<\/p>\n<p>    squared = num  2<\/p>\n<p>    return str(squared).endswith(str(num))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u64cd\u4f5c\u8fdb\u884c\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u64cd\u4f5c\u662f\u68c0\u67e5\u540c\u6784\u6570\u7684\u5173\u952e\u3002\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>endswith<\/code>\u65b9\u6cd5\u6765\u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\u662f\u5426\u7b49\u4e8e\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_automorphic(num):<\/p>\n<p>    squared = num  2<\/p>\n<p>    num_str = str(num)<\/p>\n<p>    squared_str = str(squared)<\/p>\n<p>    return squared_str.endswith(num_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5b8c\u6574\u7684Python\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684Python\u7a0b\u5e8f\uff0c\u627e\u51fa\u7ed9\u5b9a\u8303\u56f4\u5185\u7684\u6240\u6709\u540c\u6784\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_automorphic_numbers(limit):<\/p>\n<p>    automorphic_numbers = []<\/p>\n<p>    for num in range(limit):<\/p>\n<p>        if is_automorphic(num):<\/p>\n<p>            automorphic_numbers.append(num)<\/p>\n<p>    return automorphic_numbers<\/p>\n<p>def is_automorphic(num):<\/p>\n<p>    squared = num  2<\/p>\n<p>    num_str = str(num)<\/p>\n<p>    squared_str = str(squared)<\/p>\n<p>    return squared_str.endswith(num_str)<\/p>\n<h2><strong>\u8bbe\u7f6e\u8303\u56f4<\/strong><\/h2>\n<p>limit = 10000<\/p>\n<p>automorphic_numbers = find_automorphic_numbers(limit)<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>print(f&quot;Automorphic numbers up to {limit}: {automorphic_numbers}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f18\u5316\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u4e0a\u8ff0\u65b9\u6cd5\u5df2\u7ecf\u53ef\u4ee5\u627e\u5230\u540c\u6784\u6570\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\u4ee3\u7801\uff0c\u63d0\u9ad8\u6548\u7387\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u51cf\u5c11\u5b57\u7b26\u4e32\u8f6c\u6362\u7684\u6b21\u6570\u6765\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_automorphic(num):<\/p>\n<p>    squared = num  2<\/p>\n<p>    return str(squared)[-len(str(num)):] == str(num)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u6269\u5c55\u5e94\u7528<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5927\u8303\u56f4\u67e5\u627e<\/strong><\/li>\n<\/ol>\n<p><p>\u5982\u679c\u6211\u4eec\u9700\u8981\u5728\u66f4\u5927\u7684\u8303\u56f4\u5185\u67e5\u627e\u540c\u6784\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u5e76\u884c\u8ba1\u7b97\u6765\u52a0\u901f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from multiprocessing import Pool<\/p>\n<p>def find_automorphic_numbers_in_range(start, end):<\/p>\n<p>    return [num for num in range(start, end) if is_automorphic(num)]<\/p>\n<p>if __name__ == &quot;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&quot;:<\/p>\n<p>    limit = 1000000<\/p>\n<p>    num_processes = 4<\/p>\n<p>    pool = Pool(processes=num_processes)<\/p>\n<p>    chunk_size = limit \/\/ num_processes<\/p>\n<p>    results = pool.starmap(find_automorphic_numbers_in_range, [(i * chunk_size, (i + 1) * chunk_size) for i in range(num_processes)])<\/p>\n<p>    automorphic_numbers = [num for sublist in results for num in sublist]<\/p>\n<p>    print(f&quot;Automorphic numbers up to {limit}: {automorphic_numbers}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4e0d\u540c\u8fdb\u5236\u7684\u540c\u6784\u6570<\/strong><\/li>\n<\/ol>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u6269\u5c55\u540c\u6784\u6570\u7684\u6982\u5ff5\u5230\u5176\u4ed6\u8fdb\u5236\u7cfb\u7edf\u4e2d\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u68c0\u67e5\u4e00\u4e2a\u6570\u5728\u4e8c\u8fdb\u5236\u6216\u5341\u516d\u8fdb\u5236\u4e2d\u7684\u540c\u6784\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_automorphic_in_base(num, base):<\/p>\n<p>    squared = num  2<\/p>\n<p>    num_str = convert_to_base(num, base)<\/p>\n<p>    squared_str = convert_to_base(squared, base)<\/p>\n<p>    return squared_str.endswith(num_str)<\/p>\n<p>def convert_to_base(num, base):<\/p>\n<p>    if num == 0:<\/p>\n<p>        return &quot;0&quot;<\/p>\n<p>    digits = []<\/p>\n<p>    while num:<\/p>\n<p>        digits.append(int(num % base))<\/p>\n<p>        num \/\/= base<\/p>\n<p>    return &#39;&#39;.join(str(x) for x in digits[::-1])<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>print(is_automorphic_in_base(76, 10))  # True<\/p>\n<p>print(is_automorphic_in_base(3, 2))    # False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<ol>\n<li><strong>\u6570\u5b66\u7814\u7a76<\/strong><\/li>\n<\/ol>\n<p><p>\u540c\u6784\u6570\u5728\u6570\u8bba\u7814\u7a76\u4e2d\u6709\u91cd\u8981\u610f\u4e49\u3002\u6570\u5b66\u5bb6\u4eec\u53ef\u4ee5\u5229\u7528\u540c\u6784\u6570\u7684\u7279\u6027\u6765\u63a2\u7d22\u548c\u8bc1\u660e\u5176\u4ed6\u6570\u5b66\u547d\u9898\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7f16\u7a0b\u7ec3\u4e60<\/strong><\/li>\n<\/ol>\n<p><p>\u627e\u540c\u6784\u6570\u662f\u4e00\u4e2a\u7ecf\u5178\u7684\u7f16\u7a0b\u7ec3\u4e60\u9898\u76ee\uff0c\u9002\u5408\u7528\u6765\u7ec3\u4e60\u5faa\u73af\u3001\u6761\u4ef6\u5224\u65ad\u3001\u5b57\u7b26\u4e32\u64cd\u4f5c\u7b49\u57fa\u672c\u7f16\u7a0b\u6280\u5de7\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6570\u636e\u6821\u9a8c<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u67d0\u4e9b\u6570\u636e\u6821\u9a8c\u548c\u52a0\u5bc6\u7b97\u6cd5\u4e2d\uff0c\u53ef\u4ee5\u5229\u7528\u540c\u6784\u6570\u7684\u7279\u6027\u6765\u8bbe\u8ba1\u548c\u5b9e\u73b0\u7279\u5b9a\u7684\u6821\u9a8c\u89c4\u5219\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u904d\u5386\u6240\u6709\u53ef\u80fd\u7684\u6570\u3001\u68c0\u67e5\u6bcf\u4e2a\u6570\u7684\u5e73\u65b9\u662f\u5426\u4fdd\u6301\u76f8\u540c\u7684\u6570\u5b57\u987a\u5e8f\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u64cd\u4f5c\u8fdb\u884c\u6bd4\u8f83\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u6548\u5730\u627e\u51fa\u540c\u6784\u6570\u3002\u8fdb\u4e00\u6b65\u7684\u4f18\u5316\u548c\u6269\u5c55\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u67e5\u627e\u6548\u7387\uff0c\u9002\u5e94\u4e0d\u540c\u5e94\u7528\u573a\u666f\u3002<strong>\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u89e3\u51b3\u540c\u6784\u6570\u7684\u95ee\u9898\uff0c\u8fd8\u80fd\u63d0\u5347\u7f16\u7a0b\u80fd\u529b\u548c\u903b\u8f91\u601d\u7ef4\u80fd\u529b\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u540c\u6784\u6570\u662f\u4ec0\u4e48\uff0c\u5b83\u5728\u6570\u5b66\u4e2d\u6709\u4ec0\u4e48\u5e94\u7528\uff1f<\/strong><br \/>\u540c\u6784\u6570\u662f\u6307\u5728\u67d0\u79cd\u53d8\u6362\u4e0b\uff0c\u4e24\u4e2a\u6570\u7684\u6570\u5b57\u7ed3\u6784\u4fdd\u6301\u4e0d\u53d8\u3002\u4f8b\u5982\uff0c\u6570\u5b57 123 \u548c 321 \u662f\u540c\u6784\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u90fd\u5305\u542b\u76f8\u540c\u7684\u6570\u5b57\uff0c\u53ea\u662f\u987a\u5e8f\u4e0d\u540c\u3002\u540c\u6784\u6570\u5728\u5bc6\u7801\u5b66\u3001\u7ec4\u5408\u6570\u5b66\u53ca\u6570\u636e\u7ed3\u6784\u4e2d\u6709\u7740\u5e7f\u6cdb\u5e94\u7528\uff0c\u5c24\u5176\u662f\u5728\u7406\u89e3\u548c\u4f18\u5316\u7b97\u6cd5\u65f6\uff0c\u80fd\u591f\u5e2e\u52a9\u5f00\u53d1\u8005\u8bc6\u522b\u548c\u5904\u7406\u76f8\u4f3c\u7684\u6570\u5b57\u6a21\u5f0f\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u67e5\u627e\u540c\u6784\u6570\u7684\u57fa\u672c\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u67e5\u627e\u540c\u6784\u6570\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\uff1a\u9996\u5148\uff0c\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5f62\u5f0f\u4ee5\u4fbf\u4e8e\u64cd\u4f5c\uff1b\u63a5\u7740\uff0c\u901a\u8fc7\u6392\u5e8f\u6216\u4f7f\u7528\u96c6\u5408\u7b49\u65b9\u6cd5\u6765\u6bd4\u8f83\u6570\u5b57\u7684\u7ec4\u6210\uff1b\u6700\u540e\uff0c\u7f16\u5199\u51fd\u6570\u6765\u5224\u65ad\u4e24\u4e2a\u6570\u662f\u5426\u540c\u6784\u3002\u8fd9\u4e00\u8fc7\u7a0b\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u4ee3\u7801\u5b9e\u73b0\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u8bc6\u522b\u540c\u6784\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f18\u5316Python\u4ee3\u7801\u4ee5\u63d0\u9ad8\u540c\u6784\u6570\u7684\u67e5\u627e\u6548\u7387\uff1f<\/strong><br \/>\u4e3a\u4e86\u63d0\u9ad8\u67e5\u627e\u540c\u6784\u6570\u7684\u6548\u7387\uff0c\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f7f\u7528\u54c8\u5e0c\u8868\u6765\u5b58\u50a8\u5df2\u68c0\u67e5\u7684\u6570\u5b57\uff0c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\uff1b\u91c7\u7528\u66f4\u9ad8\u6548\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u6216\u8005\u76f4\u63a5\u6bd4\u8f83\u6570\u5b57\u7684\u9891\u7387\u5206\u5e03\uff1b\u540c\u65f6\uff0c\u5229\u7528\u5e76\u884c\u5904\u7406\u6280\u672f\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u663e\u8457\u7f29\u77ed\u67e5\u627e\u65f6\u95f4\u3002\u6b64\u5916\uff0c\u5408\u7406\u4f7f\u7528Python\u5185\u7f6e\u51fd\u6570\u548c\u5e93\uff0c\u5982collections.Counter\uff0c\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\u5e76\u63d0\u5347\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u627e\u540c\u6784\u6570\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u904d\u5386\u6240\u6709\u53ef\u80fd\u7684\u6570\u3001\u68c0\u67e5\u6bcf\u4e2a\u6570\u7684\u5e73\u65b9\u662f\u5426\u4fdd\u6301\u76f8\u540c\u7684\u6570\u5b57\u987a\u5e8f\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u64cd\u4f5c\u8fdb\u884c [&hellip;]","protected":false},"author":3,"featured_media":1185385,"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\/1185348"}],"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=1185348"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1185348\/revisions"}],"predecessor-version":[{"id":1185395,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1185348\/revisions\/1185395"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1185385"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1185348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1185348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1185348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}