{"id":1133495,"date":"2025-01-08T21:06:58","date_gmt":"2025-01-08T13:06:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1133495.html"},"modified":"2025-01-08T21:07:00","modified_gmt":"2025-01-08T13:07:00","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e8%be%93%e5%87%ba%e7%99%be%e4%bd%8d%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1133495.html","title":{"rendered":"\u5982\u4f55\u7528python\u8f93\u51fa\u767e\u4f4d\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102723\/03c5fd9f-0075-40ce-bbe4-9ff853a3b805.webp\" alt=\"\u5982\u4f55\u7528python\u8f93\u51fa\u767e\u4f4d\u6570\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u8f93\u51fa\u767e\u4f4d\u6570\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u5b57\u7b26\u4e32\u5904\u7406\u3001\u6570\u5b66\u8fd0\u7b97\u3001\u6a21\u5757\u51fd\u6570<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u5b57\u7b26\u4e32\u5904\u7406\u6765\u83b7\u53d6\u767e\u4f4d\u6570\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u5e76\u7ed9\u51fa\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5b57\u7b26\u4e32\u5904\u7406<\/h2>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u5904\u7406\u662f\u83b7\u53d6\u767e\u4f4d\u6570\u7684\u4e00\u4e2a\u76f4\u89c2\u4e14\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u5b83\u4e3b\u8981\u4f9d\u8d56\u4e8e\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u901a\u8fc7\u7d22\u5f15\u6216\u5207\u7247\u64cd\u4f5c\u6765\u83b7\u53d6\u6240\u9700\u7684\u767e\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><h3>1. \u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u901a\u8fc7\u7d22\u5f15\u83b7\u53d6\u767e\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_hundred_digit(num):<\/p>\n<p>    # \u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/p>\n<p>    num_str = str(num)<\/p>\n<p>    # \u68c0\u67e5\u6570\u5b57\u957f\u5ea6\u662f\u5426\u5927\u4e8e\u7b49\u4e8e3<\/p>\n<p>    if len(num_str) &lt; 3:<\/p>\n<p>        return None  # \u8fd4\u56deNone\u8868\u793a\u8be5\u6570\u5b57\u6ca1\u6709\u767e\u4f4d\u6570<\/p>\n<p>    # \u83b7\u53d6\u767e\u4f4d\u6570<\/p>\n<p>    hundred_digit = num_str[-3]<\/p>\n<p>    return int(hundred_digit)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5b57\u7b26\u4e32\u5207\u7247<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u5b57\u7b26\u4e32\u5207\u7247\uff0c\u6211\u4eec\u53ef\u4ee5\u622a\u53d6\u6700\u540e\u4e09\u4f4d\uff0c\u7136\u540e\u83b7\u53d6\u767e\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_hundred_digit(num):<\/p>\n<p>    num_str = str(num)<\/p>\n<p>    if len(num_str) &lt; 3:<\/p>\n<p>        return None<\/p>\n<p>    return int(num_str[-3])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u6570\u5b66\u8fd0\u7b97<\/h2>\n<\/p>\n<p><p>\u6570\u5b66\u8fd0\u7b97\u65b9\u6cd5\u662f\u901a\u8fc7\u9664\u6cd5\u548c\u53d6\u6a21\u8fd0\u7b97\u6765\u83b7\u53d6\u767e\u4f4d\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4f9d\u8d56\u4e8e\u5b57\u7b26\u4e32\u5904\u7406\uff0c\u56e0\u6b64\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u66f4\u4e3a\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528\u6574\u9664\u548c\u53d6\u6a21\u8fd0\u7b97<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u5bf9\u6570\u5b57\u8fdb\u884c\u6574\u9664\u548c\u53d6\u6a21\u8fd0\u7b97\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u83b7\u53d6\u767e\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_hundred_digit(num):<\/p>\n<p>    if num &lt; 100:<\/p>\n<p>        return None<\/p>\n<p>    # \u5148\u53bb\u6389\u4e2a\u4f4d\u548c\u5341\u4f4d\uff0c\u7136\u540e\u53d6\u6a2110\u5f97\u5230\u767e\u4f4d\u6570<\/p>\n<p>    return (num \/\/ 100) % 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u9012\u5f52\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u65b9\u6cd5\u662f\u4e00\u79cd\u66f4\u4e3a\u7075\u6d3b\u7684\u6570\u5b66\u8fd0\u7b97\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_hundred_digit(num):<\/p>\n<p>    if num &lt; 100:<\/p>\n<p>        return None<\/p>\n<p>    if num &lt; 1000:<\/p>\n<p>        return num \/\/ 100<\/p>\n<p>    return get_hundred_digit(num \/\/ 10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u6a21\u5757\u51fd\u6570<\/h2>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u6a21\u5757\u548c\u7b2c\u4e09\u65b9\u5e93\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u4fbf\u6377\u5730\u83b7\u53d6\u767e\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528<code>math<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>math<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u4fbf\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8fdb\u884c\u6570\u5b57\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>def get_hundred_digit(num):<\/p>\n<p>    if num &lt; 100:<\/p>\n<p>        return None<\/p>\n<p>    # \u8ba1\u7b97\u6570\u5b57\u7684\u957f\u5ea6<\/p>\n<p>    digits = int(math.log10(num)) + 1<\/p>\n<p>    if digits &lt; 3:<\/p>\n<p>        return None<\/p>\n<p>    # \u53bb\u6389\u4e2a\u4f4d\u548c\u5341\u4f4d<\/p>\n<p>    num = num \/\/ 100<\/p>\n<p>    return num % 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>\u6709\u4e9b\u7b2c\u4e09\u65b9\u5e93\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5de5\u5177\u51fd\u6570\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u83b7\u53d6\u6570\u5b57\u7684\u767e\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import digits<\/p>\n<p>def get_hundred_digit(num):<\/p>\n<p>    if num &lt; 100:<\/p>\n<p>        return None<\/p>\n<p>    num_str = str(num)<\/p>\n<p>    return int(digits(num_str)[-3])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u5b9e\u9645\u5e94\u7528\u6848\u4f8b<\/h2>\n<\/p>\n<p><h3>1. \u4ece\u5217\u8868\u4e2d\u83b7\u53d6\u767e\u4f4d\u6570<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6570\u5b57\u7684\u5217\u8868\uff0c\u9700\u8981\u83b7\u53d6\u5176\u4e2d\u6bcf\u4e2a\u6570\u5b57\u7684\u767e\u4f4d\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_hundred_digits(nums):<\/p>\n<p>    return [get_hundred_digit(num) for num in nums]<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>nums = [1234, 56789, 45, 908, 123]<\/p>\n<p>hundred_digits = get_hundred_digits(nums)<\/p>\n<p>print(hundred_digits)  # \u8f93\u51fa: [2, 7, None, 9, 1]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5904\u7406\u5927\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6570\u636e\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6765\u52a0\u901f\u83b7\u53d6\u767e\u4f4d\u6570\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from concurrent.futures import ThreadPoolExecutor<\/p>\n<p>def get_hundred_digits_parallel(nums):<\/p>\n<p>    with ThreadPoolExecutor() as executor:<\/p>\n<p>        return list(executor.map(get_hundred_digit, nums))<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>nums = [1234, 56789, 45, 908, 123]<\/p>\n<p>hundred_digits = get_hundred_digits_parallel(nums)<\/p>\n<p>print(hundred_digits)  # \u8f93\u51fa: [2, 7, None, 9, 1]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. Web\u5e94\u7528\u4e2d\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5728Web\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4ece\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u4e2d\u63d0\u53d6\u767e\u4f4d\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Flask\u5e94\u7528\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, request, jsonify<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/get_hundred_digit&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def get_hundred_digit_api():<\/p>\n<p>    data = request.json<\/p>\n<p>    num = data.get(&#39;num&#39;)<\/p>\n<p>    if num is None:<\/p>\n<p>        return jsonify({&#39;error&#39;: &#39;No number provided&#39;}), 400<\/p>\n<p>    hundred_digit = get_hundred_digit(num)<\/p>\n<p>    if hundred_digit is None:<\/p>\n<p>        return jsonify({&#39;error&#39;: &#39;Number does not have a hundred digit&#39;}), 400<\/p>\n<p>    return jsonify({&#39;hundred_digit&#39;: hundred_digit})<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684Flask\u5e94\u7528\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7POST\u8bf7\u6c42\u63d0\u4ea4\u4e00\u4e2a\u6570\u5b57\uff0c\u670d\u52a1\u5668\u4f1a\u8fd4\u56de\u8be5\u6570\u5b57\u7684\u767e\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u7ed3\u8bba<\/h2>\n<\/p>\n<p><p><strong>\u901a\u8fc7\u5b57\u7b26\u4e32\u5904\u7406\u3001\u6570\u5b66\u8fd0\u7b97\u3001\u6a21\u5757\u51fd\u6570\u7b49\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u4efb\u610f\u6570\u5b57\u7684\u767e\u4f4d\u6570<\/strong>\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u5927\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6765\u63d0\u9ad8\u6548\u7387\uff1b\u5728Web\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528API\u63a5\u53e3\u6765\u65b9\u4fbf\u5730\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u767e\u4f4d\u6570\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u7406\u89e3\u5176\u80cc\u540e\u7684\u539f\u7406\u548c\u903b\u8f91\u90fd\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u8f93\u51fa\u4e00\u4e2a\u4e09\u4f4d\u6570\u7684\u968f\u673a\u6570\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>random<\/code>\u6a21\u5757\u6765\u751f\u6210\u4e00\u4e2a\u4e09\u4f4d\u6570\u7684\u968f\u673a\u6570\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import random\n\nrandom_number = random.randint(100, 999)\nprint(random_number)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u4f1a\u968f\u673a\u751f\u6210\u4e00\u4e2a\u4ecb\u4e8e100\u5230999\u4e4b\u95f4\u7684\u6574\u6570\u5e76\u8f93\u51fa\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5982\u4f55\u68c0\u67e5\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u4e09\u4f4d\u6570\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u6761\u4ef6\u8bed\u53e5\u6765\u5224\u65ad\u4e00\u4e2a\u6570\u5b57\u662f\u5426\u4e3a\u4e09\u4f4d\u6570\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">def is_three_digit(number):\n    return 100 &lt;= number &lt;= 999\n\nnumber = 256  # \u4f60\u53ef\u4ee5\u66f4\u6539\u8fd9\u4e2a\u6570\u5b57\u6765\u6d4b\u8bd5\nif is_three_digit(number):\n    print(f&quot;{number} \u662f\u4e00\u4e2a\u4e09\u4f4d\u6570\u3002&quot;)\nelse:\n    print(f&quot;{number} \u4e0d\u662f\u4e00\u4e2a\u4e09\u4f4d\u6570\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u5c06\u8fd4\u56de\u5e03\u5c14\u503c\uff0c\u6307\u793a\u8f93\u5165\u7684\u6570\u5b57\u662f\u5426\u5728\u4e09\u4f4d\u6570\u7684\u8303\u56f4\u5185\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u683c\u5f0f\u5316\u8f93\u51fa\u4e00\u4e2a\u4e09\u4f4d\u6570\uff1f<\/strong><br \/>\u53ef\u4ee5\u5229\u7528Python\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u529f\u80fd\u5c06\u6570\u5b57\u683c\u5f0f\u5316\u4e3a\u4e09\u4f4d\u6570\uff0c\u4f8b\u5982\u4f7f\u7528<code>zfill()<\/code>\u65b9\u6cd5\u6216\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4e24\u79cd\u65b9\u6cd5\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">number = 7\nformatted_number = str(number).zfill(3)\nprint(formatted_number)  # \u8f93\u51fa &#39;007&#39;\n\nformatted_number = f&quot;{number:03}&quot;\nprint(formatted_number)  # \u8f93\u51fa &#39;007&#39;\n<\/code><\/pre>\n<p>\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u4f1a\u8f93\u51fa\u4e09\u4f4d\u6570\u683c\u5f0f\uff0c\u5982\u679c\u539f\u59cb\u6570\u5b57\u4e0d\u8db3\u4e09\u4f4d\uff0c\u4f1a\u5728\u524d\u9762\u8865\u96f6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u8f93\u51fa\u767e\u4f4d\u6570\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u5b57\u7b26\u4e32\u5904\u7406\u3001\u6570\u5b66\u8fd0\u7b97\u3001\u6a21\u5757\u51fd\u6570\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u5b57\u7b26\u4e32\u5904\u7406\u6765\u83b7 [&hellip;]","protected":false},"author":3,"featured_media":1133500,"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\/1133495"}],"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=1133495"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133495\/revisions"}],"predecessor-version":[{"id":1133502,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1133495\/revisions\/1133502"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1133500"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1133495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1133495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1133495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}