{"id":1117078,"date":"2025-01-08T18:22:39","date_gmt":"2025-01-08T10:22:39","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1117078.html"},"modified":"2025-01-08T18:22:41","modified_gmt":"2025-01-08T10:22:41","slug":"python%e5%ad%97%e5%85%b8%e4%b8%ad%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e5%80%bc%e6%9c%80%e5%b0%8f%e7%9a%84%e9%94%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1117078.html","title":{"rendered":"python\u5b57\u5178\u4e2d\u5982\u4f55\u83b7\u53d6\u503c\u6700\u5c0f\u7684\u952e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081142\/ffe1d2d1-6fce-48ae-a5b0-2b08dcceff29.webp\" alt=\"python\u5b57\u5178\u4e2d\u5982\u4f55\u83b7\u53d6\u503c\u6700\u5c0f\u7684\u952e\" \/><\/p>\n<p><p> <strong>\u5728Python\u5b57\u5178\u4e2d\u83b7\u53d6\u503c\u6700\u5c0f\u7684\u952e<\/strong>\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>min<\/code>\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u5229\u7528<code>operator.itemgetter<\/code>\u7b49\u3002<strong>\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5185\u7f6e\u51fd\u6570min()<\/strong>\uff0c\u5b83\u80fd\u591f\u76f4\u63a5\u627e\u5230\u5177\u6709\u6700\u5c0f\u503c\u7684\u952e\u3002\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>min()<\/code>\u662f\u6700\u7b80\u6d01\u4e14\u76f4\u89c2\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u4e00\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u89e3\u91ca\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u83b7\u53d6Python\u5b57\u5178\u4e2d\u503c\u6700\u5c0f\u7684\u952e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570 <code>min<\/code><\/h3>\n<\/p>\n<p><p><strong>\u5185\u7f6e\u51fd\u6570<code>min()<\/code>\u662f\u83b7\u53d6\u5b57\u5178\u4e2d\u503c\u6700\u5c0f\u952e\u7684\u6700\u5e38\u7528\u65b9\u6cd5<\/strong>\u3002\u901a\u8fc7\u63d0\u4f9b\u4e00\u4e2a\u5b57\u5178\uff0c\u5e76\u4f7f\u7528\u952e\u503c\u5bf9\u7684\u503c\u4f5c\u4e3a\u6bd4\u8f83\u6807\u51c6\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u627e\u5230\u5bf9\u5e94\u7684\u952e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sample_dict = {&#39;a&#39;: 10, &#39;b&#39;: 5, &#39;c&#39;: 15}<\/p>\n<p>min_key = min(sample_dict, key=sample_dict.get)<\/p>\n<p>print(f&quot;Key with minimum value: {min_key}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>min()<\/code>\u51fd\u6570\u901a\u8fc7<code>key=sample_dict.get<\/code>\u53c2\u6570\u6765\u83b7\u53d6\u5b57\u5178\u4e2d\u7684\u503c\u5e76\u8fdb\u884c\u6bd4\u8f83\uff0c\u6700\u540e\u8fd4\u56de\u503c\u6700\u5c0f\u7684\u952e\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\uff0c\u5148\u5c06\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u7136\u540e\u8fdb\u884c\u6392\u5e8f\uff0c\u83b7\u53d6\u503c\u6700\u5c0f\u7684\u952e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sample_dict = {&#39;a&#39;: 10, &#39;b&#39;: 5, &#39;c&#39;: 15}<\/p>\n<p>min_key = sorted(sample_dict.items(), key=lambda item: item[1])[0][0]<\/p>\n<p>print(f&quot;Key with minimum value: {min_key}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u5bf9\u5b57\u5178\u7684\u952e\u503c\u5bf9\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u901a\u8fc7<code>key=lambda item: item[1]<\/code>\u6765\u6bd4\u8f83\u6bcf\u4e2a\u952e\u503c\u5bf9\u7684\u503c\uff0c\u6700\u540e\u53d6\u6392\u5e8f\u540e\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u952e\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 <code>operator.itemgetter<\/code><\/h3>\n<\/p>\n<p><p><code>operator<\/code>\u6a21\u5757\u4e2d\u7684<code>itemgetter<\/code>\u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u503c\u6700\u5c0f\u7684\u952e\u3002\u8be5\u65b9\u6cd5\u4e0e\u5217\u8868\u63a8\u5bfc\u5f0f\u7c7b\u4f3c\uff0c\u4f46\u66f4\u4e3a\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import operator<\/p>\n<p>sample_dict = {&#39;a&#39;: 10, &#39;b&#39;: 5, &#39;c&#39;: 15}<\/p>\n<p>min_key = min(sample_dict.items(), key=operator.itemgetter(1))[0]<\/p>\n<p>print(f&quot;Key with minimum value: {min_key}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>operator.itemgetter(1)<\/code>\u6765\u83b7\u53d6\u952e\u503c\u5bf9\u4e2d\u7684\u503c\uff0c<code>min()<\/code>\u51fd\u6570\u5219\u7528\u4e8e\u627e\u5230\u5177\u6709\u6700\u5c0f\u503c\u7684\u952e\u503c\u5bf9\uff0c\u6700\u540e\u8fd4\u56de\u5176\u952e\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u66f4\u7075\u6d3b\u7684\u65b9\u6cd5\u6765\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\uff0c\u4f8b\u5982\u5728\u5b57\u5178\u8f83\u5927\u6216\u9700\u8981\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\u65f6\u3002\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5b8c\u6210\u8fd9\u4e00\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_min_key(d):<\/p>\n<p>    min_key = None<\/p>\n<p>    min_value = float(&#39;inf&#39;)<\/p>\n<p>    for k, v in d.items():<\/p>\n<p>        if v &lt; min_value:<\/p>\n<p>            min_value = v<\/p>\n<p>            min_key = k<\/p>\n<p>    return min_key<\/p>\n<p>sample_dict = {&#39;a&#39;: 10, &#39;b&#39;: 5, &#39;c&#39;: 15}<\/p>\n<p>min_key = get_min_key(sample_dict)<\/p>\n<p>print(f&quot;Key with minimum value: {min_key}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u8fed\u4ee3\u5b57\u5178\u7684\u952e\u503c\u5bf9\u6765\u627e\u5230\u6700\u5c0f\u503c\uff0c\u5e76\u8bb0\u5f55\u76f8\u5e94\u7684\u952e\u3002\u8fd9\u4e2a\u65b9\u6cd5\u867d\u7136\u4e0d\u5982\u5185\u7f6e\u51fd\u6570\u7b80\u6d01\uff0c\u4f46\u5728\u4e00\u4e9b\u7279\u5b9a\u573a\u666f\u4e0b\u53ef\u80fd\u66f4\u4e3a\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5c06\u591a\u4e2a\u65b9\u6cd5\u8fdb\u884c\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u7684\u6027\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u5b83\u4eec\u8fdb\u884c\u7b80\u5355\u7684\u6bd4\u8f83\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u6bd4\u8f83\u8fd9\u4e9b\u65b9\u6cd5\u5728\u4e0d\u540c\u5b57\u5178\u89c4\u6a21\u4e0b\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<p>sample_dict = {chr(97+i): i for i in range(1000)}<\/p>\n<p>def min_func():<\/p>\n<p>    return min(sample_dict, key=sample_dict.get)<\/p>\n<p>def sorted_func():<\/p>\n<p>    return sorted(sample_dict.items(), key=lambda item: item[1])[0][0]<\/p>\n<p>def itemgetter_func():<\/p>\n<p>    return min(sample_dict.items(), key=operator.itemgetter(1))[0]<\/p>\n<p>def custom_func():<\/p>\n<p>    min_key = None<\/p>\n<p>    min_value = float(&#39;inf&#39;)<\/p>\n<p>    for k, v in sample_dict.items():<\/p>\n<p>        if v &lt; min_value:<\/p>\n<p>            min_value = v<\/p>\n<p>            min_key = k<\/p>\n<p>    return min_key<\/p>\n<p>print(timeit.timeit(min_func, number=1000))<\/p>\n<p>print(timeit.timeit(sorted_func, number=1000))<\/p>\n<p>print(timeit.timeit(itemgetter_func, number=1000))<\/p>\n<p>print(timeit.timeit(custom_func, number=1000))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd0\u884c\u4ee5\u4e0a\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4e0d\u540c\u65b9\u6cd5\u5728\u5904\u7406\u76f8\u540c\u4efb\u52a1\u65f6\u7684\u6027\u80fd\u5dee\u5f02\u3002\u901a\u5e38\uff0c<code>min()<\/code>\u51fd\u6570\u548c<code>operator.itemgetter<\/code>\u65b9\u6cd5\u4f1a\u6bd4\u5176\u4ed6\u65b9\u6cd5\u66f4\u5feb\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c<strong>\u5728Python\u5b57\u5178\u4e2d\u83b7\u53d6\u503c\u6700\u5c0f\u7684\u952e\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>min()<\/code><\/strong>\u3002\u5176\u4ed6\u65b9\u6cd5\u5982\u5217\u8868\u63a8\u5bfc\u5f0f\u3001<code>operator.itemgetter<\/code>\u548c\u81ea\u5b9a\u4e49\u51fd\u6570\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u540c\u6837\u7684\u529f\u80fd\uff0c\u4f46\u5728\u6027\u80fd\u548c\u7b80\u6d01\u6027\u4e0a\u53ef\u80fd\u6709\u6240\u4e0d\u540c\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u80fd\u591f\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u5b57\u5178\u4e2d\u627e\u5230\u5177\u6709\u6700\u5c0f\u503c\u7684\u952e\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>min()<\/code>\u51fd\u6570\u7ed3\u5408\u5b57\u5178\u7684<code>items()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u901a\u8fc7\u4f20\u9012\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u952e\u51fd\u6570\uff0c\u53ef\u4ee5\u627e\u5230\u5177\u6709\u6700\u5c0f\u503c\u7684\u952e\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 5, &#39;b&#39;: 2, &#39;c&#39;: 3}\nmin_key = min(my_dict, key=my_dict.get)\nprint(min_key)  # \u8f93\u51fa: &#39;b&#39;\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa\u5b57\u5178\u4e2d\u503c\u6700\u5c0f\u7684\u952e\u3002<\/p>\n<p><strong>\u5728\u5b57\u5178\u4e2d\uff0c\u5982\u679c\u6709\u591a\u4e2a\u952e\u5bf9\u5e94\u76f8\u540c\u7684\u6700\u5c0f\u503c\uff0c\u5982\u4f55\u83b7\u53d6\u6240\u6709\u8fd9\u4e9b\u952e\uff1f<\/strong><br \/>\u53ef\u4ee5\u904d\u5386\u5b57\u5178\u5e76\u68c0\u67e5\u6bcf\u4e2a\u952e\u7684\u503c\u662f\u5426\u7b49\u4e8e\u6700\u5c0f\u503c\u3002\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u6536\u96c6\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u952e\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 2, &#39;b&#39;: 2, &#39;c&#39;: 3}\nmin_value = min(my_dict.values())\nmin_keys = [k for k, v in my_dict.items() if v == min_value]\nprint(min_keys)  # \u8f93\u51fa: [&#39;a&#39;, &#39;b&#39;]\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u83b7\u5f97\u6240\u6709\u503c\u7b49\u4e8e\u6700\u5c0f\u503c\u7684\u952e\u3002<\/p>\n<p><strong>\u5728Python\u5b57\u5178\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u503c\u4e3aNone\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u5982\u679c\u5b57\u5178\u4e2d\u5b58\u5728\u503c\u4e3a<code>None<\/code>\u7684\u60c5\u51b5\uff0c\u4f7f\u7528<code>min()<\/code>\u51fd\u6570\u65f6\u53ef\u80fd\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u5728\u83b7\u53d6\u6700\u5c0f\u503c\u4e4b\u524d\u8fc7\u6ee4\u6389\u8fd9\u4e9b\u503c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 5, &#39;b&#39;: None, &#39;c&#39;: 3}\nfiltered_dict = {k: v for k, v in my_dict.items() if v is not None}\nmin_key = min(filtered_dict, key=filtered_dict.get)\nprint(min_key)  # \u8f93\u51fa: &#39;c&#39;\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fdd\u5728\u8ba1\u7b97\u6700\u5c0f\u503c\u65f6\u4e0d\u4f1a\u53d7\u5230<code>None<\/code>\u503c\u7684\u5f71\u54cd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u5b57\u5178\u4e2d\u83b7\u53d6\u503c\u6700\u5c0f\u7684\u952e\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570min\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u5229\u7528opera [&hellip;]","protected":false},"author":3,"featured_media":1117082,"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\/1117078"}],"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=1117078"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1117078\/revisions"}],"predecessor-version":[{"id":1117084,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1117078\/revisions\/1117084"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1117082"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1117078"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1117078"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1117078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}