{"id":970734,"date":"2024-12-27T05:28:38","date_gmt":"2024-12-26T21:28:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/970734.html"},"modified":"2024-12-27T05:28:39","modified_gmt":"2024-12-26T21:28:39","slug":"python%e5%a6%82%e4%bd%95%e6%af%94%e8%be%83n%e4%b8%aa%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/970734.html","title":{"rendered":"python\u5982\u4f55\u6bd4\u8f83n\u4e2a\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24193919\/0b9ccc48-54fd-4fe2-a7ba-060ce2351879.webp\" alt=\"python\u5982\u4f55\u6bd4\u8f83n\u4e2a\u6570\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u6bd4\u8f83n\u4e2a\u6570\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u5faa\u73af\u6bd4\u8f83\u3001\u5185\u7f6e\u51fd\u6570\u3001\u6392\u5e8f\u7b97\u6cd5\u7b49\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5185\u7f6e\u7684max()\u548cmin()\u51fd\u6570\u6765\u627e\u51fa\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u6392\u5e8f\u5217\u8868\u6765\u6bd4\u8f83\u6570\u503c\u7684\u5927\u5c0f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u79cd\u7b80\u5355\u800c\u6709\u6548\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Python\u5185\u7f6e\u7684max()\u548cmin()\u51fd\u6570\u3002\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u63a5\u53d7\u4e00\u4e2a\u5217\u8868\u6216\u591a\u4e2a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u5176\u4e2d\u7684\u6700\u5927\u503c\u6216\u6700\u5c0f\u503c\u3002\u8fd9\u4f7f\u5f97\u5b83\u4eec\u975e\u5e38\u9002\u5408\u7528\u4e8e\u6bd4\u8f83n\u4e2a\u6570\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u6bd4\u8f83\u9700\u6c42\uff0c\u6bd4\u5982\u9700\u8981\u540c\u65f6\u627e\u5230\u6700\u5927\u548c\u6700\u5c0f\u503c\uff0c\u6216\u8005\u9700\u8981\u5bf9\u6570\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528sorted()\u51fd\u6570\u6216\u81ea\u5b9a\u4e49\u7684\u6bd4\u8f83\u51fd\u6570\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<\/h2>\n<\/p>\n<p><h3>1. \u4f7f\u7528max()\u548cmin()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u5185\u7f6e\u7684max()\u548cmin()\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u6bd4\u8f83\u591a\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [5, 12, 7, 1, 9]<\/p>\n<p>max_value = max(numbers)<\/p>\n<p>min_value = min(numbers)<\/p>\n<p>print(f&quot;\u6700\u5927\u503c\u4e3a: {max_value}, \u6700\u5c0f\u503c\u4e3a: {min_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<strong>max()\u51fd\u6570<\/strong>\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u6700\u5927\u503c\uff0c\u800c<strong>min()\u51fd\u6570<\/strong>\u8fd4\u56de\u6700\u5c0f\u503c\u3002\u8fd9\u4e24\u4e2a\u51fd\u6570\u90fd\u53ef\u4ee5\u63a5\u53d7\u591a\u4e2a\u53c2\u6570\u6216\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u7b49\uff09\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u7528\u4e8e\u5728\u4e0d\u77e5\u9053\u5177\u4f53\u6570\u5b57\u6570\u91cf\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528sorted()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5bf9\u4e00\u7ec4\u6570\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684sorted()\u51fd\u6570\u3002\u8fd9\u4e0d\u4ec5\u53ef\u4ee5\u5e2e\u52a9\u627e\u5230\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\uff0c\u8fd8\u53ef\u4ee5\u5e2e\u52a9\u786e\u5b9a\u6570\u503c\u7684\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [5, 12, 7, 1, 9]<\/p>\n<p>sorted_numbers = sorted(numbers)<\/p>\n<p>print(f&quot;\u6392\u5e8f\u540e\u7684\u5217\u8868: {sorted_numbers}&quot;)<\/p>\n<p>print(f&quot;\u6700\u5c0f\u503c\u4e3a: {sorted_numbers[0]}, \u6700\u5927\u503c\u4e3a: {sorted_numbers[-1]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>sorted()\u51fd\u6570<\/strong>\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6392\u5e8f\u5217\u8868\uff0c\u539f\u5217\u8868\u4e0d\u53d8\u3002\u901a\u8fc7\u8bbf\u95ee\u6392\u5e8f\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u548c\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u5feb\u901f\u627e\u5230\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u5faa\u73af\u6bd4\u8f83<\/h2>\n<\/p>\n<p><h3>1. \u4f7f\u7528for\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u5728\u4e0d\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u904d\u5386\u5217\u8868\u6765\u6bd4\u8f83\u6570\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u7075\u6d3b\u6027\u8f83\u9ad8\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6bd4\u8f83\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [5, 12, 7, 1, 9]<\/p>\n<p>max_value = numbers[0]<\/p>\n<p>min_value = numbers[0]<\/p>\n<p>for number in numbers:<\/p>\n<p>    if number &gt; max_value:<\/p>\n<p>        max_value = number<\/p>\n<p>    if number &lt; min_value:<\/p>\n<p>        min_value = number<\/p>\n<p>print(f&quot;\u6700\u5927\u503c\u4e3a: {max_value}, \u6700\u5c0f\u503c\u4e3a: {min_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521d\u59cb\u5316\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u4e3a\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u7136\u540e\u901a\u8fc7\u5faa\u73af\u5c06\u6bcf\u4e2a\u6570\u4e0e\u5f53\u524d\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u66f4\u65b0\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u3002<\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528while\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u867d\u7136for\u5faa\u73af\u66f4\u5e38\u7528\uff0c\u4f46\u6709\u65f6\u4e5f\u53ef\u4ee5\u4f7f\u7528while\u5faa\u73af\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u903b\u8f91\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u7075\u6d3b\u63a7\u5236\u5faa\u73af\u6761\u4ef6\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [5, 12, 7, 1, 9]<\/p>\n<p>max_value = numbers[0]<\/p>\n<p>min_value = numbers[0]<\/p>\n<p>index = 1<\/p>\n<p>while index &lt; len(numbers):<\/p>\n<p>    if numbers[index] &gt; max_value:<\/p>\n<p>        max_value = numbers[index]<\/p>\n<p>    if numbers[index] &lt; min_value:<\/p>\n<p>        min_value = numbers[index]<\/p>\n<p>    index += 1<\/p>\n<p>print(f&quot;\u6700\u5927\u503c\u4e3a: {max_value}, \u6700\u5c0f\u503c\u4e3a: {min_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5bf9\u4e8e\u7406\u89e3\u5faa\u73af\u63a7\u5236\u548c\u6761\u4ef6\u5224\u65ad\u5f88\u6709\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/h2>\n<\/p>\n<p><h3>1. \u627e\u51fa\u6700\u5927\u548c\u6700\u5c0f\u503c<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u5bf9n\u4e2a\u6570\u7684\u6bd4\u8f83\u3002\u8fd9\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u590d\u7528\u6027\uff0c\u4e5f\u8ba9\u903b\u8f91\u66f4\u52a0\u6e05\u6670\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_max_min(numbers):<\/p>\n<p>    max_value = numbers[0]<\/p>\n<p>    min_value = numbers[0]<\/p>\n<p>    for number in numbers:<\/p>\n<p>        if number &gt; max_value:<\/p>\n<p>            max_value = number<\/p>\n<p>        if number &lt; min_value:<\/p>\n<p>            min_value = number<\/p>\n<p>    return max_value, min_value<\/p>\n<p>numbers = [5, 12, 7, 1, 9]<\/p>\n<p>max_value, min_value = find_max_min(numbers)<\/p>\n<p>print(f&quot;\u6700\u5927\u503c\u4e3a: {max_value}, \u6700\u5c0f\u503c\u4e3a: {min_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9a\u4e49<strong>find_max_min()\u51fd\u6570<\/strong>\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e2d\u8c03\u7528\u5e76\u83b7\u53d6\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u3002<\/p>\n<\/p>\n<p><h3>2. \u591a\u6761\u4ef6\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u6839\u636e\u591a\u4e2a\u6761\u4ef6\u6765\u6bd4\u8f83\u6570\u503c\u3002\u4f8b\u5982\uff0c\u9664\u4e86\u6bd4\u8f83\u6570\u503c\u672c\u8eab\uff0c\u8fd8\u9700\u8981\u8003\u8651\u5176\u4ed6\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">students = [<\/p>\n<p>    {&quot;name&quot;: &quot;Alice&quot;, &quot;score&quot;: 85},<\/p>\n<p>    {&quot;name&quot;: &quot;Bob&quot;, &quot;score&quot;: 92},<\/p>\n<p>    {&quot;name&quot;: &quot;Charlie&quot;, &quot;score&quot;: 78}<\/p>\n<p>]<\/p>\n<p>def get_top_student(students):<\/p>\n<p>    top_student = students[0]<\/p>\n<p>    for student in students:<\/p>\n<p>        if student[&quot;score&quot;] &gt; top_student[&quot;score&quot;]:<\/p>\n<p>            top_student = student<\/p>\n<p>    return top_student<\/p>\n<p>top_student = get_top_student(students)<\/p>\n<p>print(f&quot;\u5206\u6570\u6700\u9ad8\u7684\u5b66\u751f\u662f: {top_student[&#39;name&#39;]}\uff0c\u5206\u6570\u4e3a: {top_student[&#39;score&#39;]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u6bd4\u8f83\u5b66\u751f\u7684\u5206\u6570\u6765\u627e\u51fa\u5f97\u5206\u6700\u9ad8\u7684\u5b66\u751f\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h2>\n<\/p>\n<p><h3>1. numpy\u5e93<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6d89\u53ca\u5927\u91cf\u6570\u636e\u7684\u590d\u6742\u8ba1\u7b97\uff0c\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982numpy\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u548c\u7b80\u5316\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>numbers = np.array([5, 12, 7, 1, 9])<\/p>\n<p>max_value = np.max(numbers)<\/p>\n<p>min_value = np.min(numbers)<\/p>\n<p>print(f&quot;\u6700\u5927\u503c\u4e3a: {max_value}, \u6700\u5c0f\u503c\u4e3a: {min_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>numpy\u5e93<\/strong>\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>2. pandas\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0cpandas\u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u5bf9\u6570\u636e\u8fdb\u884c\u590d\u6742\u7684\u6bd4\u8f83\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;Scores&#39;: [5, 12, 7, 1, 9]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>max_value = df[&#39;Scores&#39;].max()<\/p>\n<p>min_value = df[&#39;Scores&#39;].min()<\/p>\n<p>print(f&quot;\u6700\u5927\u503c\u4e3a: {max_value}, \u6700\u5c0f\u503c\u4e3a: {min_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5c06\u6570\u636e\u5b58\u50a8\u5728<strong>DataFrame<\/strong>\u4e2d\uff0c\u53ef\u4ee5\u5229\u7528pandas\u7684\u4e30\u5bcc\u51fd\u6570\u8fdb\u884c\u5404\u79cd\u6570\u636e\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u5b9e\u8df5\u5e94\u7528<\/h2>\n<\/p>\n<p><h3>1. \u5b9e\u9645\u573a\u666f\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6bd4\u8f83n\u4e2a\u6570\u53ef\u80fd\u51fa\u73b0\u5728\u591a\u4e2a\u573a\u666f\u4e2d\uff0c\u5982\u7edf\u8ba1\u5b66\u5206\u6790\u3001\u6570\u636e\u5904\u7406\u3001\u7b97\u6cd5\u4f18\u5316\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5728\u7535\u5546\u7f51\u7ad9\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u6bd4\u8f83\u4e0d\u540c\u5546\u54c1\u7684\u4ef7\u683c\u4ee5\u786e\u5b9a\u6700\u4fbf\u5b9c\u7684\u4ea7\u54c1\uff1b\u5728\u6570\u636e\u79d1\u5b66\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u6bd4\u8f83\u591a\u4e2a\u5b9e\u9a8c\u7ed3\u679c\u4ee5\u9009\u62e9\u6700\u4f73\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><h3>2. \u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u6bd4\u8f83\u65b9\u6cd5\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002\u5bf9\u4e8e\u5c0f\u89c4\u6a21\u6570\u636e\uff0c\u5185\u7f6e\u51fd\u6570\u5df2\u7ecf\u8db3\u591f\uff1b\u4f46\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\uff0c\u4f7f\u7528numpy\u6216pandas\u7b49\u5e93\u53ef\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u4ee3\u66ff\u5217\u8868\u53ef\u4ee5\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\uff0c\u5e76\u63d0\u9ad8\u5904\u7406\u901f\u5ea6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = (x for x in range(1000000))<\/p>\n<p>max_value = max(numbers)<\/p>\n<p>print(f&quot;\u6700\u5927\u503c\u4e3a: {max_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u65b9\u6cd5\u901a\u8fc7\u751f\u6210\u5668\u52a8\u6001\u751f\u6210\u6570\u636e\uff0c\u907f\u514d\u4e86\u521b\u5efa\u5b8c\u6574\u5217\u8868\u5e26\u6765\u7684\u5185\u5b58\u5f00\u9500\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u6bd4\u8f83n\u4e2a\u6570\u5e76\u4f18\u5316\u4ee3\u7801\u6027\u80fd\u3002\u65e0\u8bba\u662f\u5728\u5b66\u4e60\u4e2d\u8fd8\u662f\u5de5\u4f5c\u4e2d\uff0c\u7406\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u6280\u672f\u90fd\u80fd\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u5904\u7406\u6570\u636e\u548c\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6bd4\u8f83\u591a\u4e2a\u6570\u503c\u7684\u5927\u5c0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>max()<\/code>\u548c<code>min()<\/code>\u51fd\u6570\u6765\u6bd4\u8f83\u591a\u4e2a\u6570\u503c\u3002<code>max()<\/code>\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u4e00\u7ec4\u6570\u4e2d\u7684\u6700\u5927\u503c\uff0c\u800c<code>min()<\/code>\u5219\u8fd4\u56de\u6700\u5c0f\u503c\u3002\u4f8b\u5982\uff0c\u82e5\u8981\u6bd4\u8f83\u4e00\u7ec4\u6570\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = [3, 5, 1, 7, 2]\nmaximum = max(numbers)\nminimum = min(numbers)\nprint(&quot;\u6700\u5927\u503c:&quot;, maximum)\nprint(&quot;\u6700\u5c0f\u503c:&quot;, minimum)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u7b80\u6d01\u4e14\u6613\u4e8e\u7406\u89e3\uff0c\u9002\u5408\u5904\u7406\u4efb\u610f\u6570\u91cf\u7684\u6570\u5b57\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u627e\u5230\u4e00\u7ec4\u6570\u7684\u4e2d\u4f4d\u6570\uff1f<\/strong><br \/>\u4e2d\u4f4d\u6570\u662f\u6570\u503c\u6392\u5e8f\u540e\u4f4d\u4e8e\u4e2d\u95f4\u4f4d\u7f6e\u7684\u503c\u3002\u82e5\u8981\u8ba1\u7b97\u4e00\u7ec4\u6570\u7684\u4e2d\u4f4d\u6570\uff0c\u53ef\u4ee5\u5148\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u6839\u636e\u6570\u636e\u7684\u4e2a\u6570\u5224\u65ad\u4f4d\u7f6e\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">import statistics\n\nnumbers = [3, 5, 1, 7, 2]\nmedian = statistics.median(numbers)\nprint(&quot;\u4e2d\u4f4d\u6570:&quot;, median)\n<\/code><\/pre>\n<p><code>statistics<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u8ba1\u7b97\u4e2d\u4f4d\u6570\uff0c\u9002\u5408\u5904\u7406\u8f83\u5927\u7684\u6570\u636e\u96c6\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u6bd4\u8f83\u591a\u4e2a\u6570\u7684\u76f8\u5bf9\u5927\u5c0f\u5173\u7cfb\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u6bd4\u8f83\u591a\u4e2a\u6570\u7684\u76f8\u5bf9\u5927\u5c0f\u5173\u7cfb\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u627e\u51fa\u6bd4\u67d0\u4e2a\u7279\u5b9a\u6570\u5b57\u5927\u7684\u6240\u6709\u6570\u5b57\uff1a  <\/p>\n<pre><code class=\"language-python\">target = 4\nnumbers = [3, 5, 1, 7, 2]\ngreater_than_target = [num for num in numbers if num &gt; target]\nprint(&quot;\u5927\u4e8e{}\u7684\u6570\u6709: {}&quot;.format(target, greater_than_target))\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u7075\u6d3b\u591a\u53d8\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u8fdb\u884c\u8c03\u6574\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u6bd4\u8f83\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u6bd4\u8f83n\u4e2a\u6570\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u5faa\u73af\u6bd4\u8f83\u3001\u5185\u7f6e\u51fd\u6570\u3001\u6392\u5e8f\u7b97\u6cd5\u7b49\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5185\u7f6e\u7684m [&hellip;]","protected":false},"author":3,"featured_media":970742,"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\/970734"}],"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=970734"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/970734\/revisions"}],"predecessor-version":[{"id":970744,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/970734\/revisions\/970744"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/970742"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=970734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=970734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=970734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}