{"id":1045015,"date":"2024-12-31T13:19:33","date_gmt":"2024-12-31T05:19:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1045015.html"},"modified":"2024-12-31T13:19:35","modified_gmt":"2024-12-31T05:19:35","slug":"python-%e5%a6%82%e4%bd%95%e6%89%be%e5%87%ba%e6%9c%80%e5%a4%a7%e5%80%bc%e7%9a%84%e4%bd%8d%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1045015.html","title":{"rendered":"python \u5982\u4f55\u627e\u51fa\u6700\u5927\u503c\u7684\u4f4d\u7f6e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/61e80ec0-146d-4d72-ba4d-2dfa18a5da46.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python \u5982\u4f55\u627e\u51fa\u6700\u5927\u503c\u7684\u4f4d\u7f6e\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u627e\u51fa\u6700\u5927\u503c\u7684\u4f4d\u7f6e\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 <code>max()<\/code> \u548c <code>enumerate()<\/code> \u51fd\u6570\u3001\u4f7f\u7528 <code>numpy<\/code> \u5e93\u3001\u6216\u8005\u4f7f\u7528 <code>pandas<\/code> \u5e93\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 <code>max()<\/code> \u548c <code>enumerate()<\/code> \u65b9\u6cd5\u662f\u6700\u5e38\u89c1\u4e14\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 <code>max()<\/code> \u548c <code>enumerate()<\/code><\/h2>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u64cd\u4f5c\u5217\u8868\u3002\u8981\u627e\u5230\u6700\u5927\u503c\u53ca\u5176\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528 <code>max()<\/code> \u548c <code>enumerate()<\/code> \u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>data = [10, 20, 30, 40, 50]<\/p>\n<p>max_value = max(data)<\/p>\n<p>max_index = data.index(max_value)<\/p>\n<p>print(f&quot;\u6700\u5927\u503c\u662f {max_value}, \u4f4d\u4e8e\u7d22\u5f15 {max_index}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528 <code>max()<\/code> \u51fd\u6570\u627e\u5230\u5217\u8868\u4e2d\u7684\u6700\u5927\u503c\uff0c\u7136\u540e\u4f7f\u7528 <code>index()<\/code> \u51fd\u6570\u627e\u5230\u8be5\u6700\u5927\u503c\u5728\u5217\u8868\u4e2d\u7684\u7d22\u5f15\u3002\u8fd9\u6837\u65e2\u80fd\u627e\u5230\u6700\u5927\u503c\uff0c\u4e5f\u80fd\u627e\u5230\u5176\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h3>\n<\/p>\n<ol>\n<li><strong>max() \u51fd\u6570<\/strong>\uff1a\u7528\u4e8e\u627e\u5230\u5217\u8868\u4e2d\u7684\u6700\u5927\u503c\u3002\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5e76\u8fd4\u56de\u5176\u4e2d\u7684\u6700\u5927\u503c\u3002<\/li>\n<li><strong>index() \u51fd\u6570<\/strong>\uff1a\u7528\u4e8e\u627e\u5230\u67d0\u4e2a\u503c\u5728\u5217\u8868\u4e2d\u7684\u7d22\u5f15\u3002\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u503c\uff0c\u5e76\u8fd4\u56de\u8be5\u503c\u5728\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\u7684\u7d22\u5f15\u3002<\/li>\n<\/ol>\n<p><h2>\u4e8c\u3001\u4f7f\u7528 <code>numpy<\/code> \u5e93<\/h2>\n<\/p>\n<p><p><code>numpy<\/code> \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u51fd\u6570\u6765\u64cd\u4f5c\u6570\u7ec4\u3002\u4f7f\u7528 <code>numpy<\/code> \u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>data = np.array([10, 20, 30, 40, 50])<\/p>\n<p>max_index = np.argmax(data)<\/p>\n<p>max_value = data[max_index]<\/p>\n<p>print(f&quot;\u6700\u5927\u503c\u662f {max_value}, \u4f4d\u4e8e\u7d22\u5f15 {max_index}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h3>\n<\/p>\n<ol>\n<li><strong>np.array()<\/strong>\uff1a\u5c06\u5217\u8868\u8f6c\u6362\u4e3a <code>numpy<\/code> \u6570\u7ec4\u3002<\/li>\n<li><strong>np.argmax()<\/strong>\uff1a\u8fd4\u56de\u6570\u7ec4\u4e2d\u6700\u5927\u503c\u7684\u7d22\u5f15\u3002\u8fd9\u4e2a\u51fd\u6570\u6bd4\u5185\u7f6e\u7684 <code>index()<\/code> \u51fd\u6570\u66f4\u9ad8\u6548\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u578b\u6570\u7ec4\u65f6\u3002<\/li>\n<\/ol>\n<p><h2>\u4e09\u3001\u4f7f\u7528 <code>pandas<\/code> \u5e93<\/h2>\n<\/p>\n<p><p><code>pandas<\/code> \u662f\u4e00\u4e2a\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u8868\u683c\u6570\u636e\u3002\u4f7f\u7528 <code>pandas<\/code> \u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = pd.Series([10, 20, 30, 40, 50])<\/p>\n<p>max_index = data.idxmax()<\/p>\n<p>max_value = data[max_index]<\/p>\n<p>print(f&quot;\u6700\u5927\u503c\u662f {max_value}, \u4f4d\u4e8e\u7d22\u5f15 {max_index}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h3>\n<\/p>\n<ol>\n<li><strong>pd.Series()<\/strong>\uff1a\u5c06\u5217\u8868\u8f6c\u6362\u4e3a <code>pandas<\/code> \u7cfb\u5217\uff08\u7c7b\u4f3c\u4e8e\u4e00\u7ef4\u6570\u7ec4\uff09\u3002<\/li>\n<li><strong>idxmax()<\/strong>\uff1a\u8fd4\u56de\u7cfb\u5217\u4e2d\u6700\u5927\u503c\u7684\u7d22\u5f15\u3002<\/li>\n<\/ol>\n<p><h2>\u56db\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u627e\u5230Python\u5217\u8868\u6216\u6570\u7ec4\u4e2d\u7684\u6700\u5927\u503c\u53ca\u5176\u4f4d\u7f6e\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u5982\u679c\u6570\u636e\u91cf\u8f83\u5c0f\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 <code>max()<\/code> \u548c <code>enumerate()<\/code><\/strong>\u3002<\/li>\n<li><strong>\u5982\u679c\u9700\u8981\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\uff0c\u63a8\u8350\u4f7f\u7528 <code>numpy<\/code><\/strong>\u3002<\/li>\n<li><strong>\u5982\u679c\u6570\u636e\u662f\u8868\u683c\u5f62\u5f0f\uff0c\u4e14\u9700\u8981\u8fdb\u884c\u590d\u6742\u7684\u6570\u636e\u5206\u6790\uff0c\u63a8\u8350\u4f7f\u7528 <code>pandas<\/code><\/strong>\u3002<\/li>\n<\/ul>\n<p><p>\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\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\u5185\u7f6e\u7684<code>max()<\/code>\u51fd\u6570\u7ed3\u5408<code>list.index()<\/code>\u65b9\u6cd5\u627e\u5230\u5217\u8868\u4e2d\u6700\u5927\u503c\u7684\u4f4d\u7f6e\u3002\u9996\u5148\u4f7f\u7528<code>max()<\/code>\u627e\u5230\u6700\u5927\u503c\uff0c\u7136\u540e\u4f7f\u7528<code>index()<\/code>\u83b7\u53d6\u8be5\u503c\u7684\u7d22\u5f15\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">my_list = [1, 3, 7, 2, 5]\nmax_value = max(my_list)\nmax_index = my_list.index(max_value)\nprint(&quot;\u6700\u5927\u503c\u662f:&quot;, max_value, &quot;\u5b83\u7684\u4f4d\u7f6e\u662f:&quot;, max_index)\n<\/code><\/pre>\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\u5e93\u6765\u5904\u7406\u3002\u4f7f\u7528<code>numpy.argmax()<\/code>\u51fd\u6570\u80fd\u591f\u8fd4\u56de\u6700\u5927\u503c\u7684\u6241\u5e73\u5316\u7d22\u5f15\uff0c\u518d\u901a\u8fc7<code>numpy.unravel_index()<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u591a\u7ef4\u7d22\u5f15\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\narray = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\nflat_index = np.argmax(array)\nmulti_index = np.unravel_index(flat_index, array.shape)\nprint(&quot;\u6700\u5927\u503c\u7684\u4f4d\u7f6e\u662f:&quot;, multi_index)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5904\u7406\u542b\u6709\u591a\u4e2a\u6700\u5927\u503c\u7684\u5217\u8868\uff1f<\/strong><br \/>\u5982\u679c\u5217\u8868\u4e2d\u5b58\u5728\u591a\u4e2a\u6700\u5927\u503c\uff0c\u4f7f\u7528<code>list.index()<\/code>\u65b9\u6cd5\u53ea\u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u6700\u5927\u503c\u7684\u7d22\u5f15\u3002\u8981\u627e\u5230\u6240\u6709\u6700\u5927\u503c\u7684\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">my_list = [1, 3, 7, 2, 7, 5]\nmax_value = max(my_list)\nindices = [index for index, value in enumerate(my_list) if value == max_value]\nprint(&quot;\u6700\u5927\u503c\u662f:&quot;, max_value, &quot;\u5b83\u7684\u4f4d\u7f6e\u6709:&quot;, indices)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u627e\u51fa\u6700\u5927\u503c\u7684\u4f4d\u7f6e\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 max() \u548c enumerate() \u51fd\u6570\u3001\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1045020,"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\/1045015"}],"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=1045015"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1045015\/revisions"}],"predecessor-version":[{"id":1045023,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1045015\/revisions\/1045023"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1045020"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1045015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1045015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1045015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}