{"id":1138173,"date":"2025-01-08T21:56:53","date_gmt":"2025-01-08T13:56:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1138173.html"},"modified":"2025-01-08T21:56:55","modified_gmt":"2025-01-08T13:56:55","slug":"python-%e5%a6%82%e4%bd%95%e6%89%be%e4%b8%80%e4%b8%aa%e5%ba%8f%e5%88%97%e4%b8%ad%e6%9c%80%e5%a4%a7%e7%9a%84%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1138173.html","title":{"rendered":"python \u5982\u4f55\u627e\u4e00\u4e2a\u5e8f\u5217\u4e2d\u6700\u5927\u7684\u5143\u7d20"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101836\/1b922882-8285-43c8-b99d-d1f529ace429.webp\" alt=\"python \u5982\u4f55\u627e\u4e00\u4e2a\u5e8f\u5217\u4e2d\u6700\u5927\u7684\u5143\u7d20\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u627e\u51fa\u4e00\u4e2a\u5e8f\u5217\u4e2d\u7684\u6700\u5927\u5143\u7d20\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u624b\u52a8\u8fed\u4ee3\u3001\u4ee5\u53ca\u9ad8\u7ea7\u7684\u5e93\u51fd\u6570\u3002<\/strong>\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5185\u7f6e\u7684 <code>max()<\/code> \u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u662f\u6700\u7b80\u6d01\u548c\u9ad8\u6548\u7684\u3002\u4f60\u53ea\u9700\u5c06\u5e8f\u5217\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9 <code>max()<\/code> \u51fd\u6570\uff0c\u5b83\u5c06\u8fd4\u56de\u5e8f\u5217\u4e2d\u7684\u6700\u5927\u5143\u7d20\u3002<strong>\u7136\u800c\uff0c\u8fd9\u53ea\u662f\u4f17\u591a\u65b9\u6cd5\u4e4b\u4e00<\/strong>\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4f7f\u7528\u573a\u666f\u3001\u4f18\u7f3a\u70b9\u4ee5\u53ca\u4e00\u4e9b\u9ad8\u7ea7\u6280\u5de7\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 <code>max()<\/code><\/h3>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u5185\u7f6e\u51fd\u6570 <code>max()<\/code>\uff0c\u5b83\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u627e\u51fa\u5e8f\u5217\u4e2d\u7684\u6700\u5927\u5143\u7d20\u3002\u8fd9\u4e2a\u51fd\u6570\u4e0d\u4ec5\u9002\u7528\u4e8e\u5217\u8868\uff0c\u8fd8\u9002\u7528\u4e8e\u5143\u7ec4\u3001\u96c6\u5408\u7b49\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>sequence = [3, 1, 4, 1, 5, 9, 2, 6, 5]<\/p>\n<p>max_element = max(sequence)<\/p>\n<p>print(f&quot;\u5e8f\u5217\u4e2d\u7684\u6700\u5927\u5143\u7d20\u662f: {max_element}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li><strong>\u7b80\u6d01\u660e\u4e86<\/strong>\uff1a\u53ea\u9700\u4e00\u884c\u4ee3\u7801\u3002<\/li>\n<li><strong>\u6027\u80fd\u4f18\u8d8a<\/strong>\uff1a\u5185\u90e8\u5b9e\u73b0\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u901a\u5e38\u6bd4\u624b\u52a8\u8fed\u4ee3\u66f4\u5feb\u3002<\/li>\n<li><strong>\u9002\u7528\u8303\u56f4\u5e7f<\/strong>\uff1a\u9002\u7528\u4e8e\u6240\u6709\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<p><h4>\u7f3a\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li><strong>\u9ed1\u76d2\u64cd\u4f5c<\/strong>\uff1a\u6211\u4eec\u65e0\u6cd5\u4e86\u89e3\u5176\u5185\u90e8\u5de5\u4f5c\u673a\u5236\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u624b\u52a8\u8fed\u4ee3<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u60f3\u4e86\u89e3\u5e8f\u5217\u4e2d\u6700\u5927\u5143\u7d20\u7684\u67e5\u627e\u8fc7\u7a0b\uff0c\u6216\u8005\u5728\u5b66\u4e60\u7b97\u6cd5\u65f6\uff0c\u8fd9\u79cd\u624b\u52a8\u8fed\u4ee3\u7684\u65b9\u6cd5\u662f\u975e\u5e38\u6709\u7528\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>sequence = [3, 1, 4, 1, 5, 9, 2, 6, 5]<\/p>\n<p>max_element = sequence[0]<\/p>\n<p>for num in sequence:<\/p>\n<p>    if num &gt; max_element:<\/p>\n<p>        max_element = num<\/p>\n<p>print(f&quot;\u5e8f\u5217\u4e2d\u7684\u6700\u5927\u5143\u7d20\u662f: {max_element}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li><strong>\u900f\u660e\u5ea6\u9ad8<\/strong>\uff1a\u4f60\u53ef\u4ee5\u5b8c\u5168\u63a7\u5236\u67e5\u627e\u8fc7\u7a0b\uff0c\u9002\u5408\u5b66\u4e60\u548c\u6559\u5b66\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027\u5f3a<\/strong>\uff1a\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u6dfb\u52a0\u989d\u5916\u7684\u903b\u8f91\uff0c\u6bd4\u5982\u540c\u65f6\u67e5\u627e\u6700\u5c0f\u503c\u3002<\/li>\n<\/ul>\n<p><h4>\u7f3a\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li><strong>\u4ee3\u7801\u5197\u957f<\/strong>\uff1a\u76f8\u6bd4 <code>max()<\/code> \u51fd\u6570\uff0c\u624b\u52a8\u8fed\u4ee3\u9700\u8981\u66f4\u591a\u4ee3\u7801\u3002<\/li>\n<li><strong>\u5bb9\u6613\u51fa\u9519<\/strong>\uff1a\u624b\u52a8\u8fed\u4ee3\u53ef\u80fd\u5f15\u5165\u9519\u8bef\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u590d\u6742\u903b\u8f91\u65f6\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528 NumPy \u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\uff0c\u7279\u522b\u662f\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u9886\u57df\uff0cNumPy \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\u3002NumPy \u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\uff0c\u5176\u4e2d <code>numpy.max()<\/code> \u53ef\u4ee5\u7528\u6765\u67e5\u627e\u6570\u7ec4\u4e2d\u7684\u6700\u5927\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>sequence = np.array([3, 1, 4, 1, 5, 9, 2, 6, 5])<\/p>\n<p>max_element = np.max(sequence)<\/p>\n<p>print(f&quot;\u5e8f\u5217\u4e2d\u7684\u6700\u5927\u5143\u7d20\u662f: {max_element}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li><strong>\u9ad8\u6548<\/strong>\uff1aNumPy \u9488\u5bf9\u5927\u6570\u636e\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u5904\u7406\u901f\u5ea6\u975e\u5e38\u5feb\u3002<\/li>\n<li><strong>\u529f\u80fd\u4e30\u5bcc<\/strong>\uff1a\u9664\u4e86\u67e5\u627e\u6700\u5927\u5143\u7d20\u5916\uff0cNumPy \u63d0\u4f9b\u4e86\u8bb8\u591a\u5176\u4ed6\u6709\u7528\u7684\u51fd\u6570\u3002<\/li>\n<\/ul>\n<p><h4>\u7f3a\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li><strong>\u4f9d\u8d56\u6027<\/strong>\uff1a\u9700\u8981\u5b89\u88c5\u989d\u5916\u7684\u5e93\uff0c\u8fd9\u5bf9\u4e00\u4e9b\u8f7b\u91cf\u7ea7\u5e94\u7528\u53ef\u80fd\u4e0d\u5408\u9002\u3002<\/li>\n<li><strong>\u5b66\u4e60\u66f2\u7ebf<\/strong>\uff1a\u9700\u8981\u4e00\u5b9a\u7684\u5b66\u4e60\u6210\u672c\u6765\u638c\u63e1 NumPy \u7684\u7528\u6cd5\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u4f7f\u7528 pandas \u5e93<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5904\u7406\u7684\u662f\u6570\u636e\u6846\uff08DataFrame\uff09\u6216\u8005\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0cpandas \u5e93\u63d0\u4f9b\u4e86\u8bb8\u591a\u5f3a\u5927\u7684\u6570\u636e\u64cd\u4f5c\u529f\u80fd\u3002\u4f7f\u7528 <code>pandas.DataFrame.max()<\/code> \u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u627e\u5230\u5e8f\u5217\u4e2d\u7684\u6700\u5927\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>sequence = pd.Series([3, 1, 4, 1, 5, 9, 2, 6, 5])<\/p>\n<p>max_element = sequence.max()<\/p>\n<p>print(f&quot;\u5e8f\u5217\u4e2d\u7684\u6700\u5927\u5143\u7d20\u662f: {max_element}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li><strong>\u529f\u80fd\u5f3a\u5927<\/strong>\uff1a\u9002\u7528\u4e8e\u590d\u6742\u7684\u6570\u636e\u64cd\u4f5c\u548c\u6570\u636e\u5206\u6790\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027\u9ad8<\/strong>\uff1a\u53ef\u4ee5\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u7ed3\u6784\u3002<\/li>\n<\/ul>\n<p><h4>\u7f3a\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li><strong>\u4f9d\u8d56\u6027<\/strong>\uff1a\u9700\u8981\u5b89\u88c5 pandas \u5e93\u3002<\/li>\n<li><strong>\u6027\u80fd<\/strong>\uff1a\u5728\u5904\u7406\u7b80\u5355\u4efb\u52a1\u65f6\uff0c\u6027\u80fd\u53ef\u80fd\u4e0d\u5982 NumPy\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u4f7f\u7528 functools.reduce<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u559c\u6b22\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u4eba\u6765\u8bf4\uff0c<code>functools.reduce<\/code> \u63d0\u4f9b\u4e86\u4e00\u79cd\u4f18\u96c5\u7684\u65b9\u6cd5\u6765\u67e5\u627e\u5e8f\u5217\u4e2d\u7684\u6700\u5927\u5143\u7d20\u3002<code>reduce<\/code> \u51fd\u6570\u901a\u8fc7\u5c06\u4e00\u4e2a\u4e8c\u5143\u64cd\u4f5c\u53cd\u590d\u5e94\u7528\u4e8e\u5e8f\u5217\u7684\u5143\u7d20\uff0c\u6700\u7ec8\u5c06\u5e8f\u5217\u7f29\u51cf\u4e3a\u4e00\u4e2a\u5355\u4e00\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import reduce<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>sequence = [3, 1, 4, 1, 5, 9, 2, 6, 5]<\/p>\n<p>max_element = reduce(lambda x, y: x if x &gt; y else y, sequence)<\/p>\n<p>print(f&quot;\u5e8f\u5217\u4e2d\u7684\u6700\u5927\u5143\u7d20\u662f: {max_element}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li><strong>\u51fd\u6570\u5f0f\u7f16\u7a0b<\/strong>\uff1a\u9002\u5408\u559c\u6b22\u51fd\u6570\u5f0f\u7f16\u7a0b\u98ce\u683c\u7684\u5f00\u53d1\u8005\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u4e8c\u5143\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><h4>\u7f3a\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li><strong>\u53ef\u8bfb\u6027<\/strong>\uff1a\u5bf9\u4e8e\u4e0d\u719f\u6089\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u4eba\u6765\u8bf4\uff0c\u4ee3\u7801\u53ef\u80fd\u4e0d\u592a\u76f4\u89c2\u3002<\/li>\n<li><strong>\u6027\u80fd<\/strong>\uff1a\u5728\u5904\u7406\u975e\u5e38\u5927\u7684\u5e8f\u5217\u65f6\uff0c\u6027\u80fd\u53ef\u80fd\u4e0d\u5982\u4e13\u95e8\u4f18\u5316\u7684\u5e93\u51fd\u6570\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u4f7f\u7528 heapq \u5e93<\/h3>\n<\/p>\n<p><p><code>heapq<\/code> \u662f Python \u7684\u4e00\u4e2a\u5185\u7f6e\u5e93\uff0c\u4e13\u95e8\u7528\u4e8e\u5806\u64cd\u4f5c\u3002\u867d\u7136\u5806\u901a\u5e38\u7528\u4e8e\u67e5\u627e\u6700\u5c0f\u5143\u7d20\uff0c\u4f46\u6211\u4eec\u4e5f\u53ef\u4ee5\u7528\u5b83\u6765\u67e5\u627e\u6700\u5927\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import heapq<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>sequence = [3, 1, 4, 1, 5, 9, 2, 6, 5]<\/p>\n<p>max_element = heapq.nlargest(1, sequence)[0]<\/p>\n<p>print(f&quot;\u5e8f\u5217\u4e2d\u7684\u6700\u5927\u5143\u7d20\u662f: {max_element}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li><strong>\u9002\u7528\u4e8e\u5806\u64cd\u4f5c<\/strong>\uff1a\u5982\u679c\u4f60\u7684\u5e94\u7528\u573a\u666f\u9700\u8981\u9891\u7e41\u7684\u5806\u64cd\u4f5c\uff0c\u8fd9\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027<\/strong>\uff1a\u53ef\u4ee5\u65b9\u4fbf\u5730\u67e5\u627e\u524d n \u4e2a\u6700\u5927\u5143\u7d20\u3002<\/li>\n<\/ul>\n<p><h4>\u7f3a\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li><strong>\u590d\u6742\u5ea6<\/strong>\uff1a\u5bf9\u4e8e\u7b80\u5355\u7684\u6700\u5927\u5143\u7d20\u67e5\u627e\uff0c\u4f7f\u7528 <code>heapq<\/code> \u663e\u5f97\u6709\u4e9b\u8fc7\u4e8e\u590d\u6742\u3002<\/li>\n<li><strong>\u6027\u80fd<\/strong>\uff1a\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6027\u80fd\u53ef\u80fd\u4e0d\u5982 <code>max()<\/code> \u51fd\u6570\u3002<\/li>\n<\/ul>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u6bd4\u8f83\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u7279\u6b8a\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u6bd4\u8f83\u51fd\u6570\u6765\u67e5\u627e\u5e8f\u5217\u4e2d\u7684\u6700\u5927\u5143\u7d20\u3002Python \u7684 <code>max()<\/code> \u51fd\u6570\u652f\u6301\u4e00\u4e2a <code>key<\/code> \u53c2\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u6307\u5b9a\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>sequence = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;, &quot;date&quot;]<\/p>\n<p>max_element = max(sequence, key=len)<\/p>\n<p>print(f&quot;\u5e8f\u5217\u4e2d\u6700\u957f\u7684\u5143\u7d20\u662f: {max_element}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li><strong>\u7075\u6d3b\u6027<\/strong>\uff1a\u53ef\u4ee5\u6839\u636e\u81ea\u5b9a\u4e49\u7684\u6bd4\u8f83\u903b\u8f91\u67e5\u627e\u6700\u5927\u5143\u7d20\u3002<\/li>\n<li><strong>\u9002\u7528\u6027\u5e7f<\/strong>\uff1a\u9002\u7528\u4e8e\u5404\u79cd\u590d\u6742\u7684\u5e94\u7528\u573a\u666f\u3002<\/li>\n<\/ul>\n<p><h4>\u7f3a\u70b9\uff1a<\/h4>\n<\/p>\n<ul>\n<li><strong>\u590d\u6742\u5ea6<\/strong>\uff1a\u9700\u8981\u7f16\u5199\u548c\u8c03\u8bd5\u81ea\u5b9a\u4e49\u6bd4\u8f83\u51fd\u6570\u3002<\/li>\n<li><strong>\u6027\u80fd<\/strong>\uff1a\u81ea\u5b9a\u4e49\u6bd4\u8f83\u51fd\u6570\u53ef\u80fd\u5f71\u54cd\u6027\u80fd\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\u3002<\/li>\n<\/ul>\n<p><h3>\u516b\u3001\u6027\u80fd\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u67e5\u627e\u6700\u5927\u5143\u7d20\u7684\u65b9\u6cd5\u65f6\uff0c\u6027\u80fd\u5f80\u5f80\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u89c1\u65b9\u6cd5\u7684\u6027\u80fd\u6bd4\u8f83\uff1a<\/p>\n<\/p>\n<table>\n<thead>\n<tr>\n<p><th>\u65b9\u6cd5<\/th>\n<\/p>\n<p><th>\u65f6\u95f4\u590d\u6742\u5ea6<\/th>\n<\/p>\n<p><th>\u9002\u7528\u573a\u666f<\/th>\n<\/p>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<p><td><code>max()<\/code><\/td>\n<\/p>\n<p><td>O(n)<\/td>\n<\/p>\n<p><td>\u5e38\u89c4\u4f7f\u7528<\/td>\n<\/p>\n<\/tr>\n<tr>\n<p><td>\u624b\u52a8\u8fed\u4ee3<\/td>\n<\/p>\n<p><td>O(n)<\/td>\n<\/p>\n<p><td>\u5b66\u4e60\u548c\u6559\u5b66<\/td>\n<\/p>\n<\/tr>\n<tr>\n<p><td>NumPy<\/td>\n<\/p>\n<p><td>O(n)<\/td>\n<\/p>\n<p><td>\u5927\u6570\u636e\u548c\u79d1\u5b66\u8ba1\u7b97<\/td>\n<\/p>\n<\/tr>\n<tr>\n<p><td>pandas<\/td>\n<\/p>\n<p><td>O(n)<\/td>\n<\/p>\n<p><td>\u6570\u636e\u5206\u6790<\/td>\n<\/p>\n<\/tr>\n<tr>\n<p><td><code>functools.reduce<\/code><\/td>\n<\/p>\n<p><td>O(n)<\/td>\n<\/p>\n<p><td>\u51fd\u6570\u5f0f\u7f16\u7a0b<\/td>\n<\/p>\n<\/tr>\n<tr>\n<p><td><code>heapq<\/code><\/td>\n<\/p>\n<p><td>O(n log n)<\/td>\n<\/p>\n<p><td>\u5806\u64cd\u4f5c\u548c\u67e5\u627e\u524d n \u4e2a\u6700\u5927\u5143\u7d20<\/td>\n<\/p>\n<\/tr>\n<tr>\n<p><td>\u81ea\u5b9a\u4e49\u6bd4\u8f83\u51fd\u6570<\/td>\n<\/p>\n<p><td>O(n)<\/td>\n<\/p>\n<p><td>\u7279\u6b8a\u5e94\u7528\u573a\u666f<\/td>\n<\/p>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><h3>\u4e5d\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>\u6570\u636e\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u67e5\u627e\u6570\u636e\u96c6\u4e2d\u7684\u6700\u5927\u503c\u3002\u4f8b\u5982\uff0c\u5728\u80a1\u7968\u5e02\u573a\u5206\u6790\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u627e\u5230\u67d0\u53ea\u80a1\u7968\u5728\u4e00\u6bb5\u65f6\u95f4\u5185\u7684\u6700\u9ad8\u4ef7\u683c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>data = {&#39;date&#39;: [&#39;2023-01-01&#39;, &#39;2023-01-02&#39;, &#39;2023-01-03&#39;],<\/p>\n<p>        &#39;price&#39;: [150, 200, 180]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>max_price = df[&#39;price&#39;].max()<\/p>\n<p>print(f&quot;\u80a1\u7968\u7684\u6700\u9ad8\u4ef7\u683c\u662f: {max_price}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u56fe\u50cf\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4e2d\uff0c\u67e5\u627e\u56fe\u50cf\u50cf\u7d20\u503c\u7684\u6700\u5927\u503c\u662f\u4e00\u4e2a\u5e38\u89c1\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u5728\u56fe\u50cf\u5f52\u4e00\u5316\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u627e\u5230\u56fe\u50cf\u4e2d\u6700\u5927\u7684\u50cf\u7d20\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>image = np.array([[0, 128, 255], [64, 192, 128]])<\/p>\n<p>max_pixel = np.max(image)<\/p>\n<p>print(f&quot;\u56fe\u50cf\u4e2d\u7684\u6700\u5927\u50cf\u7d20\u503c\u662f: {max_pixel}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4><a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a><\/h4>\n<\/p>\n<p><p>\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u67e5\u627e\u6570\u636e\u96c6\u4e2d\u67d0\u4e2a\u7279\u5f81\u7684\u6700\u5927\u503c\u662f\u6570\u636e\u9884\u5904\u7406\u7684\u4e00\u90e8\u5206\u3002\u4f8b\u5982\uff0c\u5728\u7279\u5f81\u7f29\u653e\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u627e\u5230\u7279\u5f81\u503c\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>features = np.array([1.2, 3.4, 2.2, 5.6])<\/p>\n<p>max_feature = np.max(features)<\/p>\n<p>print(f&quot;\u7279\u5f81\u503c\u4e2d\u7684\u6700\u5927\u503c\u662f: {max_feature}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u627e\u51fa\u5e8f\u5217\u4e2d\u7684\u6700\u5927\u5143\u7d20\u662f\u4e00\u4e2a\u57fa\u7840\u4e14\u5e38\u89c1\u7684\u64cd\u4f5c\uff0cPython \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<strong>\u5185\u7f6e\u51fd\u6570 <code>max()<\/code> \u662f\u6700\u7b80\u6d01\u548c\u9ad8\u6548\u7684\u9009\u62e9\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u5b9a\u573a\u666f\u4e0b\uff0c\u5176\u4ed6\u65b9\u6cd5\u53ef\u80fd\u66f4\u4e3a\u9002\u7528<\/strong>\u3002\u4e86\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u505a\u51fa\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u4f60\u5df2\u7ecf\u5bf9\u5982\u4f55\u5728 Python \u4e2d\u627e\u51fa\u5e8f\u5217\u4e2d\u7684\u6700\u5927\u5143\u7d20\u6709\u4e86\u5168\u9762\u7684\u4e86\u89e3\u3002\u5e0c\u671b\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6280\u5de7\u80fd\u5728\u4f60\u7684\u65e5\u5e38\u5f00\u53d1\u4e2d\u6d3e\u4e0a\u7528\u573a\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\u4e2d\u7684\u6700\u5927\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>max()<\/code>\u51fd\u6570\u6765\u8f7b\u677e\u627e\u5230\u5217\u8868\u4e2d\u7684\u6700\u5927\u503c\u3002\u53ea\u9700\u5c06\u5e8f\u5217\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u8be5\u51fd\u6570\u3002\u4f8b\u5982\uff0c<code>max_value = max(my_list)<\/code>\u5c06\u8fd4\u56de\u5217\u8868<code>my_list<\/code>\u4e2d\u7684\u6700\u5927\u5143\u7d20\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u624b\u52a8\u6bd4\u8f83\u5143\u7d20\u6765\u627e\u51fa\u6700\u5927\u503c\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\uff0c\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u4f18\u5316\u67e5\u627e\u6700\u5927\u5143\u7d20\u7684\u6548\u7387\uff1f<\/strong><br \/>\u5f53\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u8fed\u4ee3\u7b97\u6cd5\u800c\u4e0d\u662f\u5c06\u6240\u6709\u6570\u636e\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5728\u904d\u5386\u6570\u636e\u7684\u540c\u65f6\u627e\u5230\u6700\u5927\u503c\uff0c\u4ece\u800c\u8282\u7701\u5185\u5b58\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>max()<\/code>\u7ed3\u5408\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7684\u65b9\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u3002<\/p>\n<p><strong>\u9664\u4e86\u6700\u5927\u503c\uff0c\u5982\u4f55\u627e\u51fa\u5e8f\u5217\u4e2d\u7684\u6700\u5c0f\u503c\u548c\u5176\u4ed6\u7edf\u8ba1\u4fe1\u606f\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86<code>min()<\/code>\u51fd\u6570\u6765\u627e\u5230\u5e8f\u5217\u4e2d\u7684\u6700\u5c0f\u503c\u3002\u8981\u83b7\u53d6\u5176\u4ed6\u7edf\u8ba1\u4fe1\u606f\uff0c\u4f8b\u5982\u5e73\u5747\u503c\u548c\u4e2d\u4f4d\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>statistics<\/code>\u6a21\u5757\u4e2d\u7684<code>mean()<\/code>\u548c<code>median()<\/code>\u51fd\u6570\u3002\u8fd9\u6837\uff0c\u4f60\u4e0d\u4ec5\u53ef\u4ee5\u627e\u5230\u6700\u5927\u503c\uff0c\u8fd8\u53ef\u4ee5\u5168\u9762\u5206\u6790\u6570\u636e\u96c6\u7684\u5176\u4ed6\u91cd\u8981\u7279\u5f81\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u627e\u51fa\u4e00\u4e2a\u5e8f\u5217\u4e2d\u7684\u6700\u5927\u5143\u7d20\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u624b\u52a8\u8fed\u4ee3\u3001\u4ee5\u53ca\u9ad8\u7ea7\u7684\u5e93\u51fd\u6570\u3002\u6700\u5e38\u89c1 [&hellip;]","protected":false},"author":3,"featured_media":1138180,"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\/1138173"}],"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=1138173"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1138173\/revisions"}],"predecessor-version":[{"id":1138181,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1138173\/revisions\/1138181"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1138180"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1138173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1138173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1138173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}