{"id":1029092,"date":"2024-12-31T11:05:09","date_gmt":"2024-12-31T03:05:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1029092.html"},"modified":"2024-12-31T11:05:11","modified_gmt":"2024-12-31T03:05:11","slug":"python%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e6%95%b0%e7%bb%84%e6%9c%80%e5%a4%a7%e5%80%bc%e4%b8%8b%e6%a0%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1029092.html","title":{"rendered":"python\u5982\u4f55\u83b7\u53d6\u6570\u7ec4\u6700\u5927\u503c\u4e0b\u6807"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/15a9aa49-9d05-457a-bb3b-173aa2e7fdfd.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u83b7\u53d6\u6570\u7ec4\u6700\u5927\u503c\u4e0b\u6807\" \/><\/p>\n<p><p> <strong>Python\u83b7\u53d6\u6570\u7ec4\u6700\u5927\u503c\u4e0b\u6807\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001NumPy\u5e93\u7b49\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528\u5185\u7f6e\u7684max\u548cindex\u65b9\u6cd5\u3001\u4f7f\u7528enumerate\u51fd\u6570\u3001\u4ee5\u53ca\u4f7f\u7528NumPy\u5e93\u7684argmax\u65b9\u6cd5\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u5c55\u5f00\u8bf4\u660e\u5176\u4e2d\u7684\u4e00\u79cd\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u7684max\u548cindex\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684<code>max<\/code>\u548c<code>index<\/code>\u65b9\u6cd5\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u9996\u5148\u4f7f\u7528<code>max<\/code>\u65b9\u6cd5\u627e\u5230\u6570\u7ec4\u4e2d\u7684\u6700\u5927\u503c\uff0c\u7136\u540e\u4f7f\u7528<code>index<\/code>\u65b9\u6cd5\u627e\u5230\u8be5\u6700\u5927\u503c\u7684\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array = [1, 3, 7, 9, 7, 5]<\/p>\n<p>max_value = max(array)<\/p>\n<p>max_index = array.index(max_value)<\/p>\n<p>print(f&quot;\u6700\u5927\u503c\uff1a{max_value}, \u4e0b\u6807\uff1a{max_index}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5c55\u5f00\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u6838\u5fc3\u5728\u4e8e\u4e24\u4e2a\u6b65\u9aa4\uff1a<strong>\u627e\u5230\u6700\u5927\u503c\u548c\u627e\u5230\u7d22\u5f15<\/strong>\u3002\u5728Python\u4e2d\uff0c<code>max<\/code>\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de\u6570\u7ec4\u4e2d\u7684\u6700\u5927\u503c\uff0c\u800c<code>index<\/code>\u65b9\u6cd5\u5219\u53ef\u4ee5\u8fd4\u56de\u6570\u7ec4\u4e2d\u8be5\u503c\u7684\u7b2c\u4e00\u4e2a\u7d22\u5f15\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u597d\u5904\u662f\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u5c0f\u578b\u6570\u7ec4\u3002\u7136\u800c\uff0c\u5bf9\u4e8e\u5927\u578b\u6570\u7ec4\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7684\u6548\u7387\u53ef\u80fd\u4e0d\u591f\u9ad8\uff0c\u56e0\u4e3a<code>index<\/code>\u65b9\u6cd5\u5728\u6700\u574f\u60c5\u51b5\u4e0b\u9700\u8981\u7ebf\u6027\u65f6\u95f4\u590d\u6742\u5ea6\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528enumerate\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>enumerate<\/code>\u51fd\u6570\u53ef\u4ee5\u540c\u65f6\u83b7\u53d6\u6570\u7ec4\u7684\u5143\u7d20\u53ca\u5176\u5bf9\u5e94\u7684\u4e0b\u6807\u3002\u901a\u8fc7\u904d\u5386\u6570\u7ec4\u5e76\u8bb0\u5f55\u6700\u5927\u503c\u548c\u5176\u4e0b\u6807\uff0c\u53ef\u4ee5\u8fbe\u5230\u83b7\u53d6\u6700\u5927\u503c\u4e0b\u6807\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array = [1, 3, 7, 9, 7, 5]<\/p>\n<p>max_index, max_value = max(enumerate(array), key=lambda x: x[1])<\/p>\n<p>print(f&quot;\u6700\u5927\u503c\uff1a{max_value}, \u4e0b\u6807\uff1a{max_index}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>enumerate<\/code>\u51fd\u6570\u5c06\u6570\u7ec4\u8f6c\u5316\u4e3a\u4e00\u4e2a\u5305\u542b\uff08\u7d22\u5f15\uff0c\u503c\uff09\u5bf9\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002\u7136\u540e\u4f7f\u7528<code>max<\/code>\u51fd\u6570\u548c\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u952e\u51fd\u6570<code>lambda x: x[1]<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u627e\u5230\u503c\u6700\u5927\u7684\u90a3\u4e2a\u5bf9\u3002\u6700\u540e\uff0c<code>max<\/code>\u51fd\u6570\u8fd4\u56de\u7684\u5c31\u662f\u5305\u542b\u6700\u5927\u503c\u548c\u5176\u4e0b\u6807\u7684\u5143\u7ec4\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u53ea\u904d\u5386\u4e00\u6b21\u6570\u7ec4\uff0c\u56e0\u6b64\u5728\u6548\u7387\u4e0a\u6bd4\u524d\u4e00\u79cd\u65b9\u6cd5\u8981\u597d\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528NumPy\u5e93\u7684argmax\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5176\u4e2d\u7684<code>argmax<\/code>\u65b9\u6cd5\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de\u6570\u7ec4\u4e2d\u6700\u5927\u503c\u7684\u4e0b\u6807\uff0c\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array = np.array([1, 3, 7, 9, 7, 5])<\/p>\n<p>max_index = np.argmax(array)<\/p>\n<p>print(f&quot;\u6700\u5927\u503c\u4e0b\u6807\uff1a{max_index}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>NumPy\u5e93\u7684<code>argmax<\/code>\u65b9\u6cd5\u4e13\u95e8\u7528\u4e8e\u627e\u5230\u6570\u7ec4\u6700\u5927\u503c\u7684\u4e0b\u6807\u3002\u5176\u5185\u90e8\u5b9e\u73b0\u662f\u9ad8\u5ea6\u4f18\u5316\u7684\uff0c\u53ef\u4ee5\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\uff0c\u5e76\u4e14\u6548\u7387\u6781\u9ad8\u3002\u5bf9\u4e8e\u9700\u8981\u9891\u7e41\u8fdb\u884c\u6570\u7ec4\u64cd\u4f5c\u7684\u5e94\u7528\u573a\u666f\uff0c\u4f7f\u7528NumPy\u5e93\u662f\u975e\u5e38\u63a8\u8350\u7684\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5176\u4ed6\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u5e38\u7528\u65b9\u6cd5\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u503c\u4e0b\u6807\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><p>1\u3001\u624b\u52a8\u904d\u5386\u6570\u7ec4<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u624b\u52a8\u904d\u5386\u6570\u7ec4\uff0c\u53ef\u4ee5\u5728\u4e00\u6b21\u904d\u5386\u4e2d\u540c\u65f6\u627e\u5230\u6700\u5927\u503c\u53ca\u5176\u4e0b\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array = [1, 3, 7, 9, 7, 5]<\/p>\n<p>max_value = array[0]<\/p>\n<p>max_index = 0<\/p>\n<p>for i in range(1, len(array)):<\/p>\n<p>    if array[i] &gt; max_value:<\/p>\n<p>        max_value = array[i]<\/p>\n<p>        max_index = i<\/p>\n<p>print(f&quot;\u6700\u5927\u503c\uff1a{max_value}, \u4e0b\u6807\uff1a{max_index}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u63a7\u5236\u7075\u6d3b\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u4f46\u4ee3\u7801\u76f8\u5bf9\u8f83\u957f\uff0c\u4e0d\u5982\u524d\u51e0\u79cd\u65b9\u6cd5\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><p>2\u3001\u4f7f\u7528pandas\u5e93<\/p>\n<\/p>\n<p><p>\u5982\u679c\u6570\u636e\u5b58\u50a8\u5728pandas\u7684DataFrame\u6216\u8005Series\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u63d0\u4f9b\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>series = pd.Series([1, 3, 7, 9, 7, 5])<\/p>\n<p>max_index = series.idxmax()<\/p>\n<p>print(f&quot;\u6700\u5927\u503c\u4e0b\u6807\uff1a{max_index}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>pandas\u5e93\u7684<code>idxmax<\/code>\u65b9\u6cd5\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56deSeries\u4e2d\u6700\u5927\u503c\u7684\u7d22\u5f15\uff0c\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u83b7\u53d6\u6570\u7ec4\u6700\u5927\u503c\u4e0b\u6807\u7684\u65b9\u6cd5\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u6570\u636e\u89c4\u6a21\uff0c\u53ef\u4ee5\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\u3002\u5bf9\u4e8e\u4e00\u822c\u7684\u64cd\u4f5c\uff0c\u4f7f\u7528\u5185\u7f6e\u7684<code>max<\/code>\u548c<code>index<\/code>\u65b9\u6cd5\u5373\u53ef\uff1b\u5bf9\u4e8e\u8f83\u5927\u7684\u6570\u7ec4\u548c\u66f4\u9ad8\u7684\u6548\u7387\u8981\u6c42\uff0c\u63a8\u8350\u4f7f\u7528NumPy\u5e93\u7684<code>argmax<\/code>\u65b9\u6cd5\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u6765\u83b7\u53d6\u6570\u7ec4\u7684\u6700\u5927\u503c\u4e0b\u6807\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u627e\u5230\u6570\u7ec4\u4e2d\u6700\u5927\u503c\u7684\u7d22\u5f15\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u5e93\u7684<code>argmax<\/code>\u51fd\u6570\u6765\u8f7b\u677e\u627e\u5230\u6570\u7ec4\u4e2d\u6700\u5927\u503c\u7684\u7d22\u5f15\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>numpy.argmax(array)<\/code>\uff0c\u5b83\u5c06\u8fd4\u56de\u6700\u5927\u503c\u7684\u7d22\u5f15\u3002\u5982\u679c\u4e0d\u60f3\u4f7f\u7528\u5916\u90e8\u5e93\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u7684<code>max<\/code>\u51fd\u6570\u7ed3\u5408<code>list.index<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728\u6ca1\u6709numpy\u7684\u60c5\u51b5\u4e0b\u83b7\u53d6\u5217\u8868\u6700\u5927\u503c\u7684\u7d22\u5f15\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684\u529f\u80fd\u6765\u83b7\u53d6\u5217\u8868\u7684\u6700\u5927\u503c\u7d22\u5f15\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>max_value = max(my_list)<\/code>\u6765\u627e\u5230\u6700\u5927\u503c\uff0c\u7136\u540e\u4f7f\u7528<code>index = my_list.index(max_value)<\/code>\u6765\u83b7\u53d6\u8be5\u503c\u7684\u7d22\u5f15\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u7b80\u5355\u7684\u5217\u8868\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u591a\u4e2a\u6700\u5927\u503c\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u5982\u679c\u6570\u7ec4\u4e2d\u5b58\u5728\u591a\u4e2a\u76f8\u540c\u7684\u6700\u5927\u503c\uff0c\u4f7f\u7528<code>list.index()<\/code>\u65b9\u6cd5\u53ea\u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684\u6700\u5927\u503c\u7684\u7d22\u5f15\u3002\u5982\u679c\u9700\u8981\u83b7\u53d6\u6240\u6709\u6700\u5927\u503c\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u4f8b\u5982<code>[index for index, value in enumerate(my_list) if value == max(my_list)]<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de\u6240\u6709\u6700\u5927\u503c\u7684\u7d22\u5f15\u5217\u8868\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u83b7\u53d6\u6570\u7ec4\u6700\u5927\u503c\u4e0b\u6807\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001NumPy\u5e93\u7b49\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528\u5185\u7f6e [&hellip;]","protected":false},"author":3,"featured_media":1029099,"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\/1029092"}],"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=1029092"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1029092\/revisions"}],"predecessor-version":[{"id":1029104,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1029092\/revisions\/1029104"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1029099"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1029092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1029092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1029092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}