{"id":1087151,"date":"2025-01-08T13:32:25","date_gmt":"2025-01-08T05:32:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1087151.html"},"modified":"2025-01-08T13:32:27","modified_gmt":"2025-01-08T05:32:27","slug":"python%e5%88%97%e8%a1%a8%e4%b8%ad%e5%a6%82%e4%bd%95%e8%ae%a1%e7%ae%97%e6%9c%80%e5%a4%a7%e5%80%bc-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1087151.html","title":{"rendered":"python\u5217\u8868\u4e2d\u5982\u4f55\u8ba1\u7b97\u6700\u5927\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200226\/bf7a5203-a6e1-4948-afe2-995d42f6e28a.webp\" alt=\"python\u5217\u8868\u4e2d\u5982\u4f55\u8ba1\u7b97\u6700\u5927\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u5217\u8868\u4e2d\u8ba1\u7b97\u6700\u5927\u503c\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570max()\u3001\u624b\u52a8\u8fed\u4ee3\u5217\u8868\u3001\u4f7f\u7528\u5217\u8868\u65b9\u6cd5\u6216\u5229\u7528\u7b2c\u4e09\u65b9\u5e93\u3002<\/strong> \u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5185\u7f6e\u51fd\u6570max()\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u4e14\u9ad8\u6548\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\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\u5185\u7f6e\u51fd\u6570 max()\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u8ba1\u7b97\u5217\u8868\u4e2d\u7684\u6700\u5927\u503c\u3002\u8fd9\u4e2a\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u4e00\u884c\u4ee3\u7801\u5373\u53ef\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]<\/p>\n<p>max_value = max(numbers)<\/p>\n<p>print(&quot;\u5217\u8868\u4e2d\u7684\u6700\u5927\u503c\u662f:&quot;, max_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>max() \u51fd\u6570\u7684\u4f18\u70b9\u662f\u5b83\u975e\u5e38\u76f4\u89c2\u3001\u7b80\u5355\uff0c\u5e76\u4e14\u5728\u5904\u7406\u5927\u578b\u5217\u8868\u65f6\u4e5f\u8868\u73b0\u51fa\u8272\uff0c\u56e0\u4e3a\u5b83\u662f\u7528 C \u8bed\u8a00\u5b9e\u73b0\u7684\uff0c\u5177\u6709\u5f88\u9ad8\u7684\u6548\u7387\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u624b\u52a8\u8fed\u4ee3\u5217\u8868<\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u60f3\u6df1\u5165\u4e86\u89e3\u8ba1\u7b97\u6700\u5927\u503c\u7684\u8fc7\u7a0b\uff0c\u6216\u8005\u5728\u67d0\u4e9b\u7279\u6b8a\u60c5\u51b5\u4e0b\u4e0d\u80fd\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 max()\uff0c\u4f60\u53ef\u4ee5\u624b\u52a8\u8fed\u4ee3\u5217\u8868\u6765\u627e\u5230\u6700\u5927\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u4e0d\u5982\u76f4\u63a5\u4f7f\u7528 max() \u7b80\u5355\uff0c\u4f46\u5b83\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]<\/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\u4e2d\u7684\u6700\u5927\u503c\u662f:&quot;, max_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u624b\u52a8\u8fed\u4ee3\u5217\u8868\u7684\u65b9\u6cd5\u867d\u7136\u6bd4\u76f4\u63a5\u4f7f\u7528 max() \u8981\u7e41\u7410\u4e00\u4e9b\uff0c\u4f46\u5b83\u53ef\u4ee5\u8ba9\u4f60\u66f4\u7075\u6d3b\u5730\u5904\u7406\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5728\u627e\u5230\u6700\u5927\u503c\u7684\u540c\u65f6\u8fdb\u884c\u5176\u4ed6\u64cd\u4f5c\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5bf9\u5217\u8868\u8fdb\u884c\u4e00\u4e9b\u9884\u5904\u7406\uff0c\u7136\u540e\u518d\u8ba1\u7b97\u6700\u5927\u503c\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5148\u8fc7\u6ee4\u6389\u67d0\u4e9b\u5143\u7d20\uff0c\u6216\u8005\u5bf9\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u67d0\u79cd\u8f6c\u6362\u3002\u6b64\u65f6\uff0c\u4f60\u53ef\u4ee5\u7ed3\u5408\u5217\u8868\u65b9\u6cd5\u548c max() \u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]<\/p>\n<p>filtered_numbers = [num for num in numbers if num % 2 == 0]  # \u4ec5\u4fdd\u7559\u5076\u6570<\/p>\n<p>max_value = max(filtered_numbers)<\/p>\n<p>print(&quot;\u5217\u8868\u4e2d\u7684\u5076\u6570\u6700\u5927\u503c\u662f:&quot;, max_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u5b83\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u6839\u636e\u4f60\u7684\u9700\u6c42\u5bf9\u5217\u8868\u8fdb\u884c\u5404\u79cd\u9884\u5904\u7406\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u56db\u3001\u5229\u7528\u7b2c\u4e09\u65b9\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5904\u7406\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\uff0c\u6216\u8005\u9700\u8981\u66f4\u590d\u6742\u7684\u8ba1\u7b97\u3002\u6b64\u65f6\uff0c\u4f60\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982 NumPy\u3002NumPy \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\uff0c\u5305\u62ec\u8ba1\u7b97\u6700\u5927\u503c\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>import numpy as np<\/p>\n<p>numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]<\/p>\n<p>np_numbers = np.array(numbers)<\/p>\n<p>max_value = np.max(np_numbers)<\/p>\n<p>print(&quot;\u5217\u8868\u4e2d\u7684\u6700\u5927\u503c\u662f:&quot;, max_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528 NumPy \u7684\u4f18\u70b9\u662f\u5b83\u5728\u5904\u7406\u5927\u578b\u6570\u7ec4\u65f6\u5177\u6709\u975e\u5e38\u9ad8\u7684\u6548\u7387\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u8bb8\u591a\u5176\u4ed6\u6709\u7528\u7684\u6570\u503c\u8ba1\u7b97\u529f\u80fd\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u5806\u6570\u636e\u7ed3\u6784<\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u5728\u52a8\u6001\u6570\u636e\u6d41\u4e2d\u4e0d\u65ad\u83b7\u53d6\u6700\u5927\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 heapq \u6a21\u5757\u4e2d\u7684\u5806\u6570\u636e\u7ed3\u6784\u3002\u867d\u7136 heapq \u9ed8\u8ba4\u662f\u6700\u5c0f\u5806\uff0c\u4f46\u4f60\u53ef\u4ee5\u901a\u8fc7\u5b58\u50a8\u8d1f\u503c\u6765\u6a21\u62df\u6700\u5927\u5806\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import heapq<\/p>\n<p>numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]<\/p>\n<p>max_heap = [-num for num in numbers]<\/p>\n<p>heapq.heapify(max_heap)<\/p>\n<p>max_value = -heapq.heappop(max_heap)<\/p>\n<p>print(&quot;\u5217\u8868\u4e2d\u7684\u6700\u5927\u503c\u662f:&quot;, max_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5806\u6570\u636e\u7ed3\u6784\u7684\u4f18\u70b9\u662f\u5b83\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u52a8\u6001\u6570\u636e\u6d41\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728 O(log n) \u65f6\u95f4\u5185\u83b7\u53d6\u6700\u5927\u503c\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u6bd4\u8f83\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u9ad8\u7ea7\u5e94\u7528\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u6839\u636e\u81ea\u5b9a\u4e49\u7684\u6bd4\u8f83\u903b\u8f91\u6765\u8ba1\u7b97\u6700\u5927\u503c\u3002\u6b64\u65f6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 max() \u51fd\u6570\u7684 key \u53c2\u6570\uff0c\u4f20\u9012\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u6bd4\u8f83\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>people = [Person(&quot;Alice&quot;, 30), Person(&quot;Bob&quot;, 25), Person(&quot;Charlie&quot;, 35)]<\/p>\n<h2><strong>\u81ea\u5b9a\u4e49\u6bd4\u8f83\u51fd\u6570<\/strong><\/h2>\n<p>def get_age(person):<\/p>\n<p>    return person.age<\/p>\n<p>oldest_person = max(people, key=get_age)<\/p>\n<p>print(&quot;\u5e74\u9f84\u6700\u5927\u7684\u4eba\u662f:&quot;, oldest_person.name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u81ea\u5b9a\u4e49\u6bd4\u8f83\u51fd\u6570\u7684\u4f18\u70b9\u662f\u5b83\u53ef\u4ee5\u6839\u636e\u4efb\u610f\u590d\u6742\u7684\u903b\u8f91\u6765\u8ba1\u7b97\u6700\u5927\u503c\uff0c\u975e\u5e38\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728 Python \u5217\u8868\u4e2d\u8ba1\u7b97\u6700\u5927\u503c\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u7b80\u5355\u60c5\u51b5\uff0c\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 max() \u662f\u6700\u7b80\u5355\u548c\u9ad8\u6548\u7684\u9009\u62e9\u3002\u5982\u679c\u4f60\u9700\u8981\u66f4\u590d\u6742\u7684\u5904\u7406\uff0c\u624b\u52a8\u8fed\u4ee3\u5217\u8868\u3001\u4f7f\u7528\u5217\u8868\u65b9\u6cd5\u3001\u7b2c\u4e09\u65b9\u5e93\u3001\u5806\u6570\u636e\u7ed3\u6784\u6216\u81ea\u5b9a\u4e49\u6bd4\u8f83\u51fd\u6570\u90fd\u662f\u4e0d\u9519\u7684\u9009\u62e9\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u5217\u8868\u4e2d\u627e\u5230\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\u8be5\u51fd\u6570\u3002\u4f8b\u5982\uff0c<code>max_value = max(my_list)<\/code>\uff0c\u8fd9\u6837\u5c31\u80fd\u5f97\u5230<code>my_list<\/code>\u4e2d\u7684\u6700\u5927\u5143\u7d20\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6761\u4ef6\u6765\u8ba1\u7b97\u5217\u8868\u4e2d\u7684\u6700\u5927\u503c\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\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\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5305\u542b\u5b57\u5178\u7684\u5217\u8868\uff0c\u5e76\u60f3\u6839\u636e\u67d0\u4e2a\u952e\u7684\u503c\u627e\u5230\u6700\u5927\u503c\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<code>max_value = max(my_list, key=lambda x: x[&#39;key_name&#39;])<\/code>\u3002\u8fd9\u5c06\u8fd4\u56de\u5177\u6709\u6700\u5927\u952e\u503c\u7684\u5b57\u5178\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u5305\u542b\u975e\u6570\u5b57\u5143\u7d20\u7684\u5217\u8868\u65f6\uff0c\u5982\u4f55\u627e\u5230\u6700\u5927\u503c\uff1f<\/strong><br \/>\u5982\u679c\u5217\u8868\u4e2d\u5305\u542b\u975e\u6570\u5b57\u7c7b\u578b\u7684\u5143\u7d20\uff0c<code>max()<\/code>\u51fd\u6570\u4f1a\u6839\u636e\u5143\u7d20\u7684\u81ea\u7136\u987a\u5e8f\u8fdb\u884c\u6bd4\u8f83\u3002\u5982\u679c\u4f60\u60f3\u627e\u5230\u5b57\u7b26\u4e32\u5217\u8868\u4e2d\u7684\u6700\u5927\u503c\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>max()<\/code>\uff0c\u5b83\u4f1a\u8fd4\u56de\u5b57\u5178\u5e8f\u6392\u5217\u4e2d\u6700\u5927\u7684\u5b57\u7b26\u4e32\u3002\u5982\u679c\u6709\u7279\u6b8a\u9700\u6c42\uff0c\u6bd4\u5982\u5ffd\u7565\u5927\u5c0f\u5199\uff0c\u53ef\u4ee5\u4f7f\u7528<code>key<\/code>\u53c2\u6570\uff1a<code>max_value = max(my_list, key=str.lower)<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6bd4\u8f83\u65f6\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u5217\u8868\u4e2d\u8ba1\u7b97\u6700\u5927\u503c\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570max()\u3001\u624b\u52a8\u8fed\u4ee3\u5217\u8868\u3001\u4f7f\u7528\u5217\u8868\u65b9\u6cd5\u6216\u5229\u7528\u7b2c\u4e09\u65b9\u5e93\u3002 \u6700 [&hellip;]","protected":false},"author":3,"featured_media":1087159,"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\/1087151"}],"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=1087151"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1087151\/revisions"}],"predecessor-version":[{"id":1087160,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1087151\/revisions\/1087160"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1087159"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1087151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1087151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1087151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}