{"id":1071613,"date":"2025-01-08T11:08:48","date_gmt":"2025-01-08T03:08:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1071613.html"},"modified":"2025-01-08T11:08:50","modified_gmt":"2025-01-08T03:08:50","slug":"python%e5%a6%82%e4%bd%95%e6%b1%82%e6%9c%80%e5%a4%a7%e5%80%bc%e7%9a%84%e4%bd%8d%e7%bd%ae-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1071613.html","title":{"rendered":"python\u5982\u4f55\u6c42\u6700\u5927\u503c\u7684\u4f4d\u7f6e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102102\/359aff88-6c0f-41d9-a03e-5ffb35219f1b.webp\" alt=\"python\u5982\u4f55\u6c42\u6700\u5927\u503c\u7684\u4f4d\u7f6e\" \/><\/p>\n<p><p> <strong>Python\u6c42\u6700\u5927\u503c\u7684\u4f4d\u7f6e\u53ef\u4ee5\u4f7f\u7528max()\u51fd\u6570\u3001enumerate()\u51fd\u6570\u3001numpy\u5e93\u3001pandas\u5e93\u7b49\u65b9\u6cd5\u3002<\/strong><br \/>\u5176\u4e2d\uff0c\u4f7f\u7528enumerate()\u51fd\u6570\u53ef\u4ee5\u5728\u904d\u5386\u65f6\u540c\u65f6\u83b7\u53d6\u7d22\u5f15\u548c\u5143\u7d20\u503c\uff0c\u65b9\u4fbf\u627e\u5230\u6700\u5927\u503c\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570max()\u548cindex()<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u51fd\u6570\uff0cmax()\u548cindex()\u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u5730\u627e\u5230\u5217\u8868\u4e2d\u6700\u5927\u503c\u53ca\u5176\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u4f7f\u7528max()\u548cindex()\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [10, 20, 30, 40, 50]<\/p>\n<p>max_value = max(numbers)<\/p>\n<p>max_index = numbers.index(max_value)<\/p>\n<p>print(f&quot;\u6700\u5927\u503c\u662f {max_value}\uff0c\u4f4d\u7f6e\u5728 {max_index}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528enumerate()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>enumerate()\u51fd\u6570\u53ef\u4ee5\u5728\u904d\u5386\u5217\u8868\u65f6\u540c\u65f6\u83b7\u53d6\u7d22\u5f15\u548c\u503c\uff0c\u8fd9\u4f7f\u5f97\u5bfb\u627e\u6700\u5927\u503c\u7684\u4f4d\u7f6e\u53d8\u5f97\u66f4\u52a0\u7b80\u4fbf\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u4f7f\u7528enumerate()\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [10, 20, 30, 40, 50]<\/p>\n<p>max_index, max_value = max(enumerate(numbers), key=lambda x: x[1])<\/p>\n<p>print(f&quot;\u6700\u5927\u503c\u662f {max_value}\uff0c\u4f4d\u7f6e\u5728 {max_index}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528enumerate()\u548cmax()\u51fd\u6570\uff0c\u6211\u4eec\u80fd\u591f\u5728\u4e00\u6b21\u904d\u5386\u4e2d\u83b7\u53d6\u6700\u5927\u503c\u53ca\u5176\u7d22\u5f15\uff0c\u4ee3\u7801\u7b80\u6d01\u4e14\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528numpy\u5e93<\/h3>\n<\/p>\n<p><p>numpy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u4fbf\u6377\u7684\u64cd\u4f5c\u65b9\u6cd5\u3002\u4f7f\u7528numpy\u53ef\u4ee5\u66f4\u52a0\u9ad8\u6548\u5730\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u4f7f\u7528numpy\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>numbers = np.array([10, 20, 30, 40, 50])<\/p>\n<p>max_index = np.argmax(numbers)<\/p>\n<p>max_value = numbers[max_index]<\/p>\n<p>print(f&quot;\u6700\u5927\u503c\u662f {max_value}\uff0c\u4f4d\u7f6e\u5728 {max_index}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>np.argmax()\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de\u6700\u5927\u503c\u7684\u7d22\u5f15\uff0c\u7ed3\u5408numpy\u7684\u9ad8\u6548\u8ba1\u7b97\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528pandas\u5e93<\/h3>\n<\/p>\n<p><p>pandas\u662f\u4e00\u4e2a\u6570\u636e\u5206\u6790\u5e93\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\u3002\u4f7f\u7528pandas\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u6570\u636e\u6846\u548c\u5e8f\u5217\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u4f7f\u7528pandas\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>numbers = pd.Series([10, 20, 30, 40, 50])<\/p>\n<p>max_index = numbers.idxmax()<\/p>\n<p>max_value = numbers[max_index]<\/p>\n<p>print(f&quot;\u6700\u5927\u503c\u662f {max_value}\uff0c\u4f4d\u7f6e\u5728 {max_index}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>pandas\u7684idxmax()\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de\u6700\u5927\u503c\u7684\u7d22\u5f15\uff0c\u7ed3\u5408pandas\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u9002\u5408\u6570\u636e\u5206\u6790\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5b9e\u73b0\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u73b0\u6709\u7684\u5e93\u548c\u51fd\u6570\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5b9e\u73b0\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5bfb\u627e\u6700\u5927\u503c\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u5b9e\u73b0\u81ea\u5b9a\u4e49\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def find_max_position(numbers):<\/p>\n<p>    max_index = 0<\/p>\n<p>    max_value = numbers[0]<\/p>\n<p>    for i in range(1, len(numbers)):<\/p>\n<p>        if numbers[i] &gt; max_value:<\/p>\n<p>            max_value = numbers[i]<\/p>\n<p>            max_index = i<\/p>\n<p>    return max_index, max_value<\/p>\n<p>numbers = [10, 20, 30, 40, 50]<\/p>\n<p>max_index, max_value = find_max_position(numbers)<\/p>\n<p>print(f&quot;\u6700\u5927\u503c\u662f {max_value}\uff0c\u4f4d\u7f6e\u5728 {max_index}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u4f18\u5316\u548c\u6269\u5c55\uff0c\u9002\u5e94\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u4ecb\u7ecd\u4e86\u591a\u79cd\u5728Python\u4e2d\u5bfb\u627e\u6700\u5927\u503c\u53ca\u5176\u4f4d\u7f6e\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001enumerate()\u51fd\u6570\u3001numpy\u5e93\u3001pandas\u5e93\u4ee5\u53ca\u81ea\u5b9a\u4e49\u51fd\u6570\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5185\u7f6e\u51fd\u6570max()\u548cindex()<\/strong>\uff1a\u7b80\u5355\u76f4\u63a5\uff0c\u9002\u5408\u5c0f\u89c4\u6a21\u6570\u636e\u3002<\/li>\n<li><strong>enumerate()\u51fd\u6570<\/strong>\uff1a\u7b80\u6d01\u9ad8\u6548\uff0c\u9002\u5408\u4e2d\u7b49\u89c4\u6a21\u6570\u636e\u3002<\/li>\n<li><strong>numpy\u5e93<\/strong>\uff1a\u9ad8\u6548\u8ba1\u7b97\uff0c\u9002\u5408\u5927\u89c4\u6a21\u6570\u636e\u3002<\/li>\n<li><strong>pandas\u5e93<\/strong>\uff1a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u9002\u5408\u6570\u636e\u5206\u6790\u3002<\/li>\n<li><strong>\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong>\uff1a\u7075\u6d3b\u53ef\u6269\u5c55\uff0c\u9002\u5e94\u4e0d\u540c\u9700\u6c42\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u6570\u636e\uff0c\u627e\u5230\u6700\u5927\u503c\u53ca\u5176\u4f4d\u7f6e\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\u6700\u5927\u503c\u7684\u7d22\u5f15\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>max()<\/code>\u51fd\u6570\u627e\u5230\u6700\u5927\u503c\uff0c\u7136\u540e\u7ed3\u5408<code>index()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5176\u7d22\u5f15\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u5217\u8868<code>my_list<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">max_value = max(my_list)\nmax_index = my_list.index(max_value)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8fd4\u56de\u6700\u5927\u503c\u53ca\u5176\u5728\u5217\u8868\u4e2d\u7684\u4f4d\u7f6e\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u627e\u5230\u6700\u5927\u503c\u53ca\u5176\u4f4d\u7f6e\uff1f<\/strong><br \/>\u5f53\u7136\uff0c\u9664\u4e86\u4f7f\u7528<code>max()<\/code>\u548c<code>index()<\/code>\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u4e2d\u7684<code>np.argmax()<\/code>\u51fd\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u66f4\u9002\u5408\u5904\u7406\u5927\u578b\u6570\u7ec4\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\nmy_array = np.array([1, 3, 2, 5, 4])\nmax_index = np.argmax(my_array)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8fd4\u56de\u6570\u7ec4\u4e2d\u6700\u5927\u503c\u7684\u7d22\u5f15\u3002<\/p>\n<p><strong>\u5728\u591a\u7ef4\u6570\u7ec4\u4e2d\u5982\u4f55\u627e\u5230\u6700\u5927\u503c\u7684\u4f4d\u7f6e\uff1f<\/strong><br \/>\u5bf9\u4e8e\u591a\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u7684<code>np.unravel_index()<\/code>\u4e0e<code>np.argmax()<\/code>\u7ed3\u5408\uff0c\u6765\u83b7\u53d6\u6700\u5927\u503c\u7684\u591a\u7ef4\u7d22\u5f15\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\nmy_array = np.array([[1, 2, 3], [4, 5, 6]])\nmax_index = np.argmax(my_array)\nmulti_index = np.unravel_index(max_index, my_array.shape)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u83b7\u53d6\u5230\u5728\u591a\u7ef4\u6570\u7ec4\u4e2d\u7684\u6700\u5927\u503c\u7684\u5177\u4f53\u4f4d\u7f6e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u6c42\u6700\u5927\u503c\u7684\u4f4d\u7f6e\u53ef\u4ee5\u4f7f\u7528max()\u51fd\u6570\u3001enumerate()\u51fd\u6570\u3001numpy\u5e93\u3001pandas\u5e93\u7b49\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":1071621,"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\/1071613"}],"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=1071613"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1071613\/revisions"}],"predecessor-version":[{"id":1071624,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1071613\/revisions\/1071624"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1071621"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1071613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1071613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1071613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}