{"id":1060540,"date":"2024-12-31T15:34:52","date_gmt":"2024-12-31T07:34:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1060540.html"},"modified":"2024-12-31T15:34:55","modified_gmt":"2024-12-31T07:34:55","slug":"%e5%a6%82%e4%bd%95%e8%be%93%e5%87%ba%e5%88%97%e8%a1%a8%e6%9c%80%e5%a4%a7%e5%80%bcpython","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1060540.html","title":{"rendered":"\u5982\u4f55\u8f93\u51fa\u5217\u8868\u6700\u5927\u503cpython"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/8e37ebb9-5c35-4252-bbf8-3ece5a5b0e08.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u8f93\u51fa\u5217\u8868\u6700\u5927\u503cpython\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570max()\u3001\u4f7f\u7528\u5faa\u73af\u6bd4\u8f83\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong>\u3002\u8981\u5728Python\u4e2d\u627e\u5230\u5217\u8868\u7684\u6700\u5927\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>max()<\/code>\u51fd\u6570\uff0c\u8fd9\u662f\u4e00\u79cd\u7b80\u5355\u800c\u6709\u6548\u7684\u65b9\u6cd5\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u6bd4\u8f83\u6bcf\u4e2a\u5143\u7d20\u6765\u627e\u5230\u6700\u5927\u503c\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u901a\u8fc7\u8fd9\u51e0\u79cd\u65b9\u6cd5\u6765\u8f93\u51fa\u5217\u8868\u7684\u6700\u5927\u503c\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570max()<\/p>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u5185\u7f6e\u51fd\u6570 <code>max()<\/code>\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u67e5\u627e\u5217\u8868\u4e2d\u7684\u6700\u5927\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u5355\u6613\u7528\uff0c\u4ee3\u7801\u7b80\u6d01\uff0c\u800c\u4e14\u6267\u884c\u901f\u5ea6\u975e\u5e38\u5feb\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [3, 5, 7, 2, 8, 6]<\/p>\n<p>max_value = max(numbers)<\/p>\n<p>print(&quot;\u5217\u8868\u7684\u6700\u5927\u503c\u662f:&quot;, max_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>max()<\/code> \u51fd\u6570\u76f4\u63a5\u8fd4\u56de\u5217\u8868 <code>numbers<\/code> \u4e2d\u7684\u6700\u5927\u503c\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u53d8\u91cf <code>max_value<\/code> \u4e2d\uff0c\u6700\u540e\u6253\u5370\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5faa\u73af\u6bd4\u8f83<\/p>\n<\/p>\n<p><p>\u5982\u679c\u60f3\u8981\u66f4\u6df1\u5165\u5730\u7406\u89e3\u67e5\u627e\u5217\u8868\u6700\u5927\u503c\u7684\u8fc7\u7a0b\uff0c\u6216\u8005\u5728\u5b66\u4e60\u7b97\u6cd5\u65f6\uff0c\u4f7f\u7528\u5faa\u73af\u6bd4\u8f83\u6bcf\u4e2a\u5143\u7d20\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u67e5\u627e\u6700\u5927\u503c\u7684\u5e95\u5c42\u903b\u8f91\uff0c\u4f46\u4ee3\u7801\u76f8\u5bf9\u590d\u6742\u4e00\u4e9b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [3, 5, 7, 2, 8, 6]<\/p>\n<p>max_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>print(&quot;\u5217\u8868\u7684\u6700\u5927\u503c\u662f:&quot;, max_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5c06\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u8bbe\u4e3a <code>max_value<\/code>\uff0c\u7136\u540e\u901a\u8fc7\u5faa\u73af\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5982\u679c\u53d1\u73b0\u67d0\u4e2a\u5143\u7d20\u5927\u4e8e <code>max_value<\/code>\uff0c\u5219\u66f4\u65b0 <code>max_value<\/code>\u3002\u6700\u7ec8 <code>max_value<\/code> \u4e2d\u5b58\u50a8\u7684\u5c31\u662f\u5217\u8868\u7684\u6700\u5927\u503c\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u4e24\u79cd\u65b9\u6cd5\uff0c\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u67e5\u627e\u5217\u8868\u7684\u6700\u5927\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u5c06\u67e5\u627e\u6700\u5927\u503c\u7684\u903b\u8f91\u5c01\u88c5\u8d77\u6765\uff0c\u4fbf\u4e8e\u5728\u591a\u4e2a\u5730\u65b9\u91cd\u590d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_max(numbers):<\/p>\n<p>    max_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>    return max_value<\/p>\n<p>numbers = [3, 5, 7, 2, 8, 6]<\/p>\n<p>max_value = find_max(numbers)<\/p>\n<p>print(&quot;\u5217\u8868\u7684\u6700\u5927\u503c\u662f:&quot;, max_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>find_max<\/code> \u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u8be5\u5217\u8868\u7684\u6700\u5927\u503c\u3002\u8fd9\u6837\u67e5\u627e\u6700\u5927\u503c\u7684\u903b\u8f91\u5c31\u88ab\u5c01\u88c5\u5728 <code>find_max<\/code> \u51fd\u6570\u4e2d\uff0c\u4ee3\u7801\u663e\u5f97\u66f4\u52a0\u6574\u6d01\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5904\u7406\u7a7a\u5217\u8868\u7684\u60c5\u51b5<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5217\u8868\u4e3a\u7a7a\u7684\u60c5\u51b5\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u5728\u67e5\u627e\u6700\u5927\u503c\u4e4b\u524d\u5148\u68c0\u67e5\u5217\u8868\u662f\u5426\u4e3a\u7a7a\u3002\u5982\u679c\u5217\u8868\u4e3a\u7a7a\uff0c\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u7279\u5b9a\u7684\u503c\u6216\u8005\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_max(numbers):<\/p>\n<p>    if not numbers:<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;\u5217\u8868\u4e3a\u7a7a\uff0c\u65e0\u6cd5\u67e5\u627e\u6700\u5927\u503c&quot;)<\/p>\n<p>    max_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>    return max_value<\/p>\n<p>try:<\/p>\n<p>    numbers = []<\/p>\n<p>    max_value = find_max(numbers)<\/p>\n<p>    print(&quot;\u5217\u8868\u7684\u6700\u5927\u503c\u662f:&quot;, max_value)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u68c0\u67e5\u5217\u8868 <code>numbers<\/code> \u662f\u5426\u4e3a\u7a7a\uff0c\u5982\u679c\u4e3a\u7a7a\u5219\u629b\u51fa\u4e00\u4e2a <code>ValueError<\/code> \u5f02\u5e38\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5728\u5904\u7406\u7a7a\u5217\u8868\u65f6\u4e0d\u4f1a\u51fa\u73b0\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u6392\u5e8f\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u67e5\u627e\u5217\u8868\u6700\u5927\u503c\u7684\u65b9\u6cd5\u662f\u5148\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u83b7\u53d6\u6392\u5e8f\u540e\u7684\u5217\u8868\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u5982\u76f4\u63a5\u4f7f\u7528 <code>max()<\/code> \u51fd\u6570\u6216\u5faa\u73af\u6bd4\u8f83\u9ad8\u6548\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [3, 5, 7, 2, 8, 6]<\/p>\n<p>numbers.sort()<\/p>\n<p>max_value = numbers[-1]<\/p>\n<p>print(&quot;\u5217\u8868\u7684\u6700\u5927\u503c\u662f:&quot;, max_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5bf9\u5217\u8868 <code>numbers<\/code> \u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u83b7\u53d6\u6392\u5e8f\u540e\u7684\u5217\u8868\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u8be5\u5143\u7d20\u5373\u4e3a\u5217\u8868\u7684\u6700\u5927\u503c\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u67e5\u627e\u591a\u7ef4\u5217\u8868\u7684\u6700\u5927\u503c<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u591a\u7ef4\u5217\u8868\uff08\u4f8b\u5982\u77e9\u9635\uff09\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u5faa\u73af\u6765\u67e5\u627e\u6700\u5927\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = [<\/p>\n<p>    [3, 5, 7],<\/p>\n<p>    [2, 8, 6],<\/p>\n<p>    [1, 4, 9]<\/p>\n<p>]<\/p>\n<p>max_value = matrix[0][0]<\/p>\n<p>for row in matrix:<\/p>\n<p>    for number in row:<\/p>\n<p>        if number &gt; max_value:<\/p>\n<p>            max_value = number<\/p>\n<p>print(&quot;\u77e9\u9635\u7684\u6700\u5927\u503c\u662f:&quot;, max_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u5d4c\u5957\u5faa\u73af\u904d\u5386\u77e9\u9635\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u67e5\u627e\u5e76\u66f4\u65b0\u6700\u5927\u503c <code>max_value<\/code>\uff0c\u6700\u7ec8 <code>max_value<\/code> \u4e2d\u5b58\u50a8\u7684\u5c31\u662f\u77e9\u9635\u7684\u6700\u5927\u503c\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u4f7f\u7528NumPy\u5e93<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5904\u7406\uff0cNumPy \u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528 NumPy \u7684 <code>max()<\/code> \u51fd\u6570\u6765\u67e5\u627e\u6570\u7ec4\u7684\u6700\u5927\u503c\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u6d01\u4e14\u6267\u884c\u6548\u7387\u9ad8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>numbers = np.array([3, 5, 7, 2, 8, 6])<\/p>\n<p>max_value = np.max(numbers)<\/p>\n<p>print(&quot;\u5217\u8868\u7684\u6700\u5927\u503c\u662f:&quot;, max_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u4f7f\u7528 <code>np.array<\/code> \u5c06\u5217\u8868\u8f6c\u6362\u4e3a NumPy \u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528 <code>np.max()<\/code> \u51fd\u6570\u67e5\u627e\u6570\u7ec4\u7684\u6700\u5927\u503c\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u67e5\u627e\u5b57\u5178\u4e2d\u503c\u7684\u6700\u5927\u503c<\/p>\n<\/p>\n<p><p>\u5982\u679c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u662f\u5b57\u5178\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b57\u5178\u4e2d\u7684\u67d0\u4e2a\u952e\u6765\u67e5\u627e\u6700\u5927\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">students = [<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;score&#39;: 85},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;score&#39;: 92},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;score&#39;: 87}<\/p>\n<p>]<\/p>\n<p>max_score = max(students, key=lambda x: x[&#39;score&#39;])<\/p>\n<p>print(&quot;\u6700\u9ad8\u5206\u7684\u5b66\u751f\u662f:&quot;, max_score[&#39;name&#39;], &quot;\u5206\u6570\u662f:&quot;, max_score[&#39;score&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 <code>max()<\/code> \u51fd\u6570\u548c <code>key<\/code> \u53c2\u6570\u6765\u67e5\u627e\u5b57\u5178\u5217\u8868\u4e2d\u5206\u6570\u6700\u9ad8\u7684\u5b66\u751f\u3002<\/p>\n<\/p>\n<p><p>\u4e5d\u3001\u67e5\u627e\u590d\u6742\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u6700\u5927\u503c<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u53ef\u80fd\u662f\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u5d4c\u5957\u7684\u5217\u8868\u6216\u5b57\u5178\u3002\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u7684\u65b9\u5f0f\u6765\u67e5\u627e\u6700\u5927\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_max(data):<\/p>\n<p>    if isinstance(data, list):<\/p>\n<p>        max_value = find_max(data[0])<\/p>\n<p>        for item in data:<\/p>\n<p>            item_max = find_max(item)<\/p>\n<p>            if item_max &gt; max_value:<\/p>\n<p>                max_value = item_max<\/p>\n<p>        return max_value<\/p>\n<p>    elif isinstance(data, dict):<\/p>\n<p>        return find_max(list(data.values()))<\/p>\n<p>    else:<\/p>\n<p>        return data<\/p>\n<p>data = [3, [5, 7], {&#39;a&#39;: 2, &#39;b&#39;: 8}, 6]<\/p>\n<p>max_value = find_max(data)<\/p>\n<p>print(&quot;\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u6700\u5927\u503c\u662f:&quot;, max_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u9012\u5f52\u7684\u65b9\u5f0f\u904d\u5386\u590d\u6742\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u67e5\u627e\u5e76\u8fd4\u56de\u6700\u5927\u503c\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u662f\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 <code>max()<\/code>\u3001\u5faa\u73af\u6bd4\u8f83\u3001\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u8fd8\u662f\u5904\u7406\u7a7a\u5217\u8868\u3001\u6392\u5e8f\u65b9\u6cd5\u3001\u67e5\u627e\u591a\u7ef4\u5217\u8868\u7684\u6700\u5927\u503c\u3001\u4f7f\u7528 NumPy \u5e93\u3001\u67e5\u627e\u5b57\u5178\u4e2d\u503c\u7684\u6700\u5927\u503c\uff0c\u751a\u81f3\u662f\u67e5\u627e\u590d\u6742\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u6700\u5927\u503c\uff0cPython \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u627e\u5230\u5217\u8868\u7684\u6700\u5927\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>max()<\/code>\u51fd\u6570\u8f7b\u677e\u627e\u5230\u5217\u8868\u4e2d\u7684\u6700\u5927\u503c\u3002\u53ea\u9700\u5c06\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9<code>max()<\/code>\u51fd\u6570\uff0c\u5b83\u5c06\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u6700\u5927\u5143\u7d20\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = [3, 5, 1, 8, 2]\nmax_value = max(numbers)\nprint(max_value)  # \u8f93\u51fa: 8\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u5355\uff0c\u800c\u4e14\u6548\u7387\u9ad8\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u7c7b\u578b\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6761\u4ef6\u6765\u627e\u5230\u5217\u8868\u7684\u6700\u5927\u503c\u5417\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u5728\u4f7f\u7528<code>max()<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>key<\/code>\u53c2\u6570\u81ea\u5b9a\u4e49\u6bd4\u8f83\u6761\u4ef6\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u5305\u542b\u5b57\u5178\u7684\u5217\u8868\uff0c\u5e76\u5e0c\u671b\u627e\u5230\u57fa\u4e8e\u7279\u5b9a\u952e\u7684\u6700\u5927\u503c\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">people = [{&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30}, {&#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 25}, {&#39;name&#39;: &#39;Charlie&#39;, &#39;age&#39;: 35}]\noldest_person = max(people, key=lambda x: x[&#39;age&#39;])\nprint(oldest_person)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Charlie&#39;, &#39;age&#39;: 35}\n<\/code><\/pre>\n<p>\u4f7f\u7528<code>key<\/code>\u53c2\u6570\u53ef\u4ee5\u7075\u6d3b\u5730\u6307\u5b9a\u6bd4\u8f83\u6807\u51c6\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<p><strong>\u5982\u679c\u5217\u8868\u4e3a\u7a7a\uff0c\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5f53\u5217\u8868\u4e3a\u7a7a\u65f6\uff0c\u8c03\u7528<code>max()<\/code>\u51fd\u6570\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u5728\u5bfb\u627e\u6700\u5927\u503c\u4e4b\u524d\u68c0\u67e5\u5217\u8868\u662f\u5426\u4e3a\u7a7a\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = []\nif numbers:\n    max_value = max(numbers)\nelse:\n    max_value = None  # \u6216\u8005\u53ef\u4ee5\u8bbe\u7f6e\u4e3a\u5176\u4ed6\u9ed8\u8ba4\u503c\nprint(max_value)  # \u8f93\u51fa: None\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u505a\u6cd5\u53ef\u4ee5\u786e\u4fdd\u4ee3\u7801\u5728\u5904\u7406\u7a7a\u5217\u8868\u65f6\u4e0d\u4f1a\u5d29\u6e83\uff0c\u5e76\u4e14\u80fd\u591f\u4f18\u96c5\u5730\u5904\u7406\u5f02\u5e38\u60c5\u51b5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528\u5185\u7f6e\u51fd\u6570max()\u3001\u4f7f\u7528\u5faa\u73af\u6bd4\u8f83\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u3002\u8981\u5728Python\u4e2d\u627e\u5230\u5217\u8868\u7684\u6700\u5927\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684max [&hellip;]","protected":false},"author":3,"featured_media":1060547,"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\/1060540"}],"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=1060540"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1060540\/revisions"}],"predecessor-version":[{"id":1060548,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1060540\/revisions\/1060548"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1060547"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1060540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1060540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1060540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}