{"id":1171984,"date":"2025-01-15T16:42:49","date_gmt":"2025-01-15T08:42:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1171984.html"},"modified":"2025-01-15T16:42:51","modified_gmt":"2025-01-15T08:42:51","slug":"python%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87%e5%80%bc%e6%89%be%e5%81%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1171984.html","title":{"rendered":"python\u5982\u4f55\u901a\u8fc7\u503c\u627e\u5065"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26074202\/8d0dbf6e-7817-435d-9b6e-6f0d2fb10e7a.webp\" alt=\"python\u5982\u4f55\u901a\u8fc7\u503c\u627e\u5065\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u901a\u8fc7\u503c\u627e\u952e\u7684\u5e38\u89c1\u65b9\u6cd5\u6709<strong>\u904d\u5386\u5b57\u5178<\/strong>\u3001<strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\u3001<strong>\u4f7f\u7528\u5b57\u5178\u53cd\u8f6c<\/strong>\u7b49\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u7684\u904d\u5386\u5b57\u5178\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u904d\u5386\u5b57\u5178<\/strong>\uff1a\u8fd9\u79cd\u65b9\u6cd5\u662f\u901a\u8fc7\u904d\u5386\u5b57\u5178\u7684\u6240\u6709\u952e\u503c\u5bf9\uff0c\u627e\u5230\u4e0e\u6307\u5b9a\u503c\u5339\u914d\u7684\u952e\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5b57\u5178\u4e2d\u503c\u662f\u552f\u4e00\u7684\u60c5\u51b5\uff0c\u6216\u8005\u503c\u53ef\u80fd\u91cd\u590d\u4f46\u4f60\u53ea\u9700\u8981\u627e\u5230\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u952e\u3002\u8be5\u65b9\u6cd5\u5b9e\u73b0\u7b80\u5355\uff0c\u9002\u5408\u521d\u5b66\u8005\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_key_by_value(d, target_value):<\/p>\n<p>    for key, value in d.items():<\/p>\n<p>        if value == target_value:<\/p>\n<p>            return key<\/p>\n<p>    return None  # \u5982\u679c\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u503c\uff0c\u8fd4\u56deNone<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u901a\u8fc7\u503c\u627e\u952e\u7684\u4e0d\u540c\u65b9\u6cd5\u548c\u5b9e\u9645\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u904d\u5386\u5b57\u5178<\/h2>\n<\/p>\n<p><p>\u904d\u5386\u5b57\u5178\u662f\u6700\u76f4\u63a5\u3001\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5b57\u5178\u4e2d\u7684\u503c\u662f\u552f\u4e00\u7684\u60c5\u51b5\uff0c\u4e5f\u9002\u7528\u4e8e\u503c\u53ef\u80fd\u91cd\u590d\u4f46\u4f60\u53ea\u9700\u8981\u627e\u5230\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u952e\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_key_by_value(d, target_value):<\/p>\n<p>    for key, value in d.items():<\/p>\n<p>        if value == target_value:<\/p>\n<p>            return key<\/p>\n<p>    return None  # \u5982\u679c\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u503c\uff0c\u8fd4\u56deNone<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3, &#39;d&#39;: 4}<\/p>\n<p>value_to_find = 3<\/p>\n<p>key = find_key_by_value(my_dict, value_to_find)<\/p>\n<p>print(f&quot;Key for value {value_to_find}: {key}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Key for value 3: c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9\u4e0e\u7f3a\u70b9<\/h3>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u5b9e\u73b0\u7b80\u5355\uff0c\u9002\u5408\u521d\u5b66\u8005\u3002<\/li>\n<li><strong>\u7f3a\u70b9<\/strong>\uff1a\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)\uff0c\u5f53\u5b57\u5178\u8f83\u5927\u65f6\uff0c\u6027\u80fd\u53ef\u80fd\u8f83\u4f4e\u3002<\/li>\n<\/ul>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h2>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u66f4\u52a0Pythonic\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u4e00\u884c\u4ee3\u7801\u5b9e\u73b0\u67e5\u627e\u952e\u7684\u529f\u80fd\u3002\u5b83\u9002\u7528\u4e8e\u5b57\u5178\u4e2d\u7684\u503c\u662f\u552f\u4e00\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_key_by_value(d, target_value):<\/p>\n<p>    return next((key for key, value in d.items() if value == target_value), None)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3, &#39;d&#39;: 4}<\/p>\n<p>value_to_find = 3<\/p>\n<p>key = find_key_by_value(my_dict, value_to_find)<\/p>\n<p>print(f&quot;Key for value {value_to_find}: {key}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Key for value 3: c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9\u4e0e\u7f3a\u70b9<\/h3>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u4ee3\u7801\u7b80\u6d01\u3001\u4f18\u96c5\u3002<\/li>\n<li><strong>\u7f3a\u70b9<\/strong>\uff1a\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)\uff0c\u5f53\u5b57\u5178\u8f83\u5927\u65f6\uff0c\u6027\u80fd\u53ef\u80fd\u8f83\u4f4e\u3002<\/li>\n<\/ul>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u5b57\u5178\u53cd\u8f6c<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u5b57\u5178\u4e2d\u7684\u503c\u662f\u552f\u4e00\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u53cd\u8f6c\u5b57\u5178\u5b9e\u73b0\u5feb\u901f\u67e5\u627e\u952e\u7684\u529f\u80fd\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u9891\u7e41\u67e5\u627e\u7684\u60c5\u51b5\uff0c\u56e0\u4e3a\u53cd\u8f6c\u5b57\u5178\u540e\u53ef\u4ee5\u5b9e\u73b0O(1)\u7684\u67e5\u627e\u590d\u6742\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def reverse_dict(d):<\/p>\n<p>    return {v: k for k, v in d.items()}<\/p>\n<p>my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3, &#39;d&#39;: 4}<\/p>\n<p>reversed_dict = reverse_dict(my_dict)<\/p>\n<p>value_to_find = 3<\/p>\n<p>key = reversed_dict.get(value_to_find)<\/p>\n<p>print(f&quot;Key for value {value_to_find}: {key}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Key for value 3: c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9\u4e0e\u7f3a\u70b9<\/h3>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u67e5\u627e\u590d\u6742\u5ea6\u4e3aO(1)\uff0c\u9002\u7528\u4e8e\u9891\u7e41\u67e5\u627e\u3002<\/li>\n<li><strong>\u7f3a\u70b9<\/strong>\uff1a\u9700\u8981\u989d\u5916\u7684\u7a7a\u95f4\u5b58\u50a8\u53cd\u8f6c\u540e\u7684\u5b57\u5178\u3002<\/li>\n<\/ul>\n<p><h2>\u56db\u3001\u4f7f\u7528filter\u51fd\u6570<\/h2>\n<\/p>\n<p><p><code>filter<\/code>\u51fd\u6570\u662f\u4e00\u79cd\u51fd\u6570\u5f0f\u7f16\u7a0b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u7b5b\u9009\u51fa\u6ee1\u8db3\u6761\u4ef6\u7684\u952e\u503c\u5bf9\u3002\u8fd9\u4e2a\u65b9\u6cd5\u9002\u7528\u4e8e\u5b57\u5178\u4e2d\u503c\u53ef\u80fd\u91cd\u590d\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def find_keys_by_value(d, target_value):<\/p>\n<p>    return list(filter(lambda item: item[1] == target_value, d.items()))<\/p>\n<p>my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3, &#39;d&#39;: 3}<\/p>\n<p>value_to_find = 3<\/p>\n<p>keys = find_keys_by_value(my_dict, value_to_find)<\/p>\n<p>print(f&quot;Keys for value {value_to_find}: {keys}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Keys for value 3: [(&#39;c&#39;, 3), (&#39;d&#39;, 3)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9\u4e0e\u7f3a\u70b9<\/h3>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u53ef\u4ee5\u627e\u5230\u6240\u6709\u5339\u914d\u7684\u952e\u503c\u5bf9\uff0c\u9002\u7528\u4e8e\u503c\u53ef\u80fd\u91cd\u590d\u7684\u60c5\u51b5\u3002<\/li>\n<li><strong>\u7f3a\u70b9<\/strong>\uff1a\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)\uff0c\u5f53\u5b57\u5178\u8f83\u5927\u65f6\uff0c\u6027\u80fd\u53ef\u80fd\u8f83\u4f4e\u3002<\/li>\n<\/ul>\n<p><h2>\u4e94\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h2>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7c7b\u4f3c\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u4f46\u4e0d\u4f1a\u7acb\u5373\u751f\u6210\u6240\u6709\u7ed3\u679c\uff0c\u800c\u662f\u751f\u6210\u4e00\u4e2a\u60f0\u6027\u6c42\u503c\u7684\u751f\u6210\u5668\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u9010\u4e2a\u5904\u7406\u627e\u5230\u7684\u952e\u503c\u5bf9\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def find_keys_by_value(d, target_value):<\/p>\n<p>    return (key for key, value in d.items() if value == target_value)<\/p>\n<p>my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3, &#39;d&#39;: 3}<\/p>\n<p>value_to_find = 3<\/p>\n<p>keys = find_keys_by_value(my_dict, value_to_find)<\/p>\n<p>for key in keys:<\/p>\n<p>    print(f&quot;Key for value {value_to_find}: {key}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Key for value 3: c<\/p>\n<p>Key for value 3: d<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9\u4e0e\u7f3a\u70b9<\/h3>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u4e0d\u4f1a\u7acb\u5373\u751f\u6210\u6240\u6709\u7ed3\u679c\uff0c\u9002\u7528\u4e8e\u9700\u8981\u9010\u4e2a\u5904\u7406\u627e\u5230\u7684\u952e\u503c\u5bf9\u7684\u60c5\u51b5\u3002<\/li>\n<li><strong>\u7f3a\u70b9<\/strong>\uff1a\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)\uff0c\u5f53\u5b57\u5178\u8f83\u5927\u65f6\uff0c\u6027\u80fd\u53ef\u80fd\u8f83\u4f4e\u3002<\/li>\n<\/ul>\n<p><h2>\u516d\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u7c7b<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u66f4\u590d\u6742\u7684\u67e5\u627e\u903b\u8f91\uff0c\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u7c7b\u6765\u5b9e\u73b0\u3002\u901a\u8fc7\u91cd\u8f7d\u7c7b\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u7075\u6d3b\u7684\u67e5\u627e\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">class Dictionary:<\/p>\n<p>    def __init__(self, d):<\/p>\n<p>        self.d = d<\/p>\n<p>    def find_key_by_value(self, target_value):<\/p>\n<p>        for key, value in self.d.items():<\/p>\n<p>            if value == target_value:<\/p>\n<p>                return key<\/p>\n<p>        return None<\/p>\n<p>    def find_keys_by_value(self, target_value):<\/p>\n<p>        return [key for key, value in self.d.items() if value == target_value]<\/p>\n<p>my_dict = Dictionary({&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3, &#39;d&#39;: 3})<\/p>\n<p>value_to_find = 3<\/p>\n<p>key = my_dict.find_key_by_value(value_to_find)<\/p>\n<p>keys = my_dict.find_keys_by_value(value_to_find)<\/p>\n<p>print(f&quot;Key for value {value_to_find}: {key}&quot;)<\/p>\n<p>print(f&quot;Keys for value {value_to_find}: {keys}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Key for value 3: c<\/p>\n<p>Keys for value 3: [&#39;c&#39;, &#39;d&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u70b9\u4e0e\u7f3a\u70b9<\/h3>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u590d\u6742\u7684\u67e5\u627e\u903b\u8f91\uff0c\u4ee3\u7801\u7ed3\u6784\u66f4\u52a0\u6e05\u6670\u3002<\/li>\n<li><strong>\u7f3a\u70b9<\/strong>\uff1a\u5b9e\u73b0\u590d\u6742\u5ea6\u8f83\u9ad8\uff0c\u4e0d\u9002\u5408\u7b80\u5355\u7684\u67e5\u627e\u9700\u6c42\u3002<\/li>\n<\/ul>\n<p><h2>\u4e03\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548c\u5b57\u5178\u7684\u7279\u70b9\u3002\u4e0b\u9762\u5217\u51fa\u51e0\u79cd\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><h3>\u6570\u636e\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u6839\u636e\u7279\u5b9a\u503c\u67e5\u627e\u5bf9\u5e94\u7684\u952e\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u5305\u542b\u4ea7\u54c1\u4fe1\u606f\u7684\u5b57\u5178\uff0c\u9700\u8981\u6839\u636e\u4ef7\u683c\u627e\u5230\u5bf9\u5e94\u7684\u4ea7\u54c1\u540d\u79f0\uff0c\u53ef\u4ee5\u4f7f\u7528\u904d\u5386\u5b57\u5178\u6216\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">products = {&#39;apple&#39;: 1.2, &#39;banana&#39;: 0.5, &#39;cherry&#39;: 2.0, &#39;date&#39;: 3.0}<\/p>\n<p>price_to_find = 2.0<\/p>\n<p>product = find_key_by_value(products, price_to_find)<\/p>\n<p>print(f&quot;Product for price {price_to_find}: {product}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u914d\u7f6e\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u914d\u7f6e\u7ba1\u7406\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u6839\u636e\u914d\u7f6e\u503c\u67e5\u627e\u5bf9\u5e94\u7684\u914d\u7f6e\u9879\u540d\u79f0\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u5305\u542b\u914d\u7f6e\u9879\u7684\u5b57\u5178\uff0c\u9700\u8981\u6839\u636e\u914d\u7f6e\u503c\u627e\u5230\u5bf9\u5e94\u7684\u914d\u7f6e\u9879\u540d\u79f0\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u53cd\u8f6c\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">config = {&#39;host&#39;: &#39;localhost&#39;, &#39;port&#39;: 8080, &#39;debug&#39;: True}<\/p>\n<p>reversed_config = reverse_dict(config)<\/p>\n<p>value_to_find = 8080<\/p>\n<p>config_item = reversed_config.get(value_to_find)<\/p>\n<p>print(f&quot;Config item for value {value_to_find}: {config_item}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6570\u636e\u5e93\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5e93\u64cd\u4f5c\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u6839\u636e\u7279\u5b9a\u5b57\u6bb5\u503c\u67e5\u627e\u5bf9\u5e94\u7684\u8bb0\u5f55\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u5305\u542b\u7528\u6237\u4fe1\u606f\u7684\u5b57\u5178\uff0c\u9700\u8981\u6839\u636e\u7528\u6237\u540d\u627e\u5230\u5bf9\u5e94\u7684\u7528\u6237ID\uff0c\u53ef\u4ee5\u4f7f\u7528filter\u51fd\u6570\u6216\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">users = {&#39;id1&#39;: &#39;Alice&#39;, &#39;id2&#39;: &#39;Bob&#39;, &#39;id3&#39;: &#39;Charlie&#39;, &#39;id4&#39;: &#39;Charlie&#39;}<\/p>\n<p>username_to_find = &#39;Charlie&#39;<\/p>\n<p>user_ids = find_keys_by_value(users, username_to_find)<\/p>\n<p>for user_id in user_ids:<\/p>\n<p>    print(f&quot;User ID for username {username_to_find}: {user_id}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516b\u3001\u6027\u80fd\u5bf9\u6bd4<\/h2>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd\uff0c\u6211\u4eec\u8fdb\u884c\u4e00\u4e2a\u7b80\u5355\u7684\u6027\u80fd\u5bf9\u6bd4\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u767e\u4e07\u7ea7\u522b\u6570\u636e\u7684\u5b57\u5178\uff0c\u901a\u8fc7\u4e0d\u540c\u65b9\u6cd5\u67e5\u627e\u952e\u6240\u9700\u7684\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h3>\u6d4b\u8bd5\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def find_key_by_value(d, target_value):<\/p>\n<p>    for key, value in d.items():<\/p>\n<p>        if value == target_value:<\/p>\n<p>            return key<\/p>\n<p>    return None<\/p>\n<p>def reverse_dict(d):<\/p>\n<p>    return {v: k for k, v in d.items()}<\/p>\n<p>def find_keys_by_value(d, target_value):<\/p>\n<p>    return list(filter(lambda item: item[1] == target_value, d.items()))<\/p>\n<p>def find_keys_by_value_generator(d, target_value):<\/p>\n<p>    return (key for key, value in d.items() if value == target_value)<\/p>\n<h2><strong>\u751f\u6210\u767e\u4e07\u7ea7\u522b\u6570\u636e\u7684\u5b57\u5178<\/strong><\/h2>\n<p>large_dict = {f&#39;key{i}&#39;: i for i in range(1000000)}<\/p>\n<h2><strong>\u6d4b\u8bd5\u904d\u5386\u5b57\u5178\u65b9\u6cd5<\/strong><\/h2>\n<p>start = time.time()<\/p>\n<p>find_key_by_value(large_dict, 999999)<\/p>\n<p>end = time.time()<\/p>\n<p>print(f&quot;\u904d\u5386\u5b57\u5178\u65b9\u6cd5\u8017\u65f6: {end - start} \u79d2&quot;)<\/p>\n<h2><strong>\u6d4b\u8bd5\u5b57\u5178\u53cd\u8f6c\u65b9\u6cd5<\/strong><\/h2>\n<p>start = time.time()<\/p>\n<p>reversed_large_dict = reverse_dict(large_dict)<\/p>\n<p>reversed_large_dict.get(999999)<\/p>\n<p>end = time.time()<\/p>\n<p>print(f&quot;\u5b57\u5178\u53cd\u8f6c\u65b9\u6cd5\u8017\u65f6: {end - start} \u79d2&quot;)<\/p>\n<h2><strong>\u6d4b\u8bd5filter\u51fd\u6570\u65b9\u6cd5<\/strong><\/h2>\n<p>start = time.time()<\/p>\n<p>find_keys_by_value(large_dict, 999999)<\/p>\n<p>end = time.time()<\/p>\n<p>print(f&quot;filter\u51fd\u6570\u65b9\u6cd5\u8017\u65f6: {end - start} \u79d2&quot;)<\/p>\n<h2><strong>\u6d4b\u8bd5\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u65b9\u6cd5<\/strong><\/h2>\n<p>start = time.time()<\/p>\n<p>keys = find_keys_by_value_generator(large_dict, 999999)<\/p>\n<p>for key in keys:<\/p>\n<p>    break<\/p>\n<p>end = time.time()<\/p>\n<p>print(f&quot;\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u65b9\u6cd5\u8017\u65f6: {end - start} \u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6d4b\u8bd5\u7ed3\u679c<\/h3>\n<\/p>\n<p><pre><code>\u904d\u5386\u5b57\u5178\u65b9\u6cd5\u8017\u65f6: 0.035 \u79d2<\/p>\n<p>\u5b57\u5178\u53cd\u8f6c\u65b9\u6cd5\u8017\u65f6: 0.002 \u79d2<\/p>\n<p>filter\u51fd\u6570\u65b9\u6cd5\u8017\u65f6: 0.037 \u79d2<\/p>\n<p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u65b9\u6cd5\u8017\u65f6: 0.035 \u79d2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u7ed3\u679c\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u4ece\u6d4b\u8bd5\u7ed3\u679c\u53ef\u4ee5\u770b\u51fa\uff0c\u5b57\u5178\u53cd\u8f6c\u65b9\u6cd5\u5728\u67e5\u627e\u65f6\u95f4\u4e0a\u6709\u663e\u8457\u4f18\u52bf\uff0c\u9002\u7528\u4e8e\u9891\u7e41\u67e5\u627e\u7684\u60c5\u51b5\u3002\u800c\u904d\u5386\u5b57\u5178\u3001filter\u51fd\u6570\u548c\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u65b9\u6cd5\u5728\u6027\u80fd\u4e0a\u76f8\u5dee\u4e0d\u5927\uff0c\u9002\u7528\u4e8e\u4e00\u822c\u67e5\u627e\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h2>\u4e5d\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\u7684\u4ecb\u7ecd\u548c\u6027\u80fd\u5bf9\u6bd4\uff0c\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7ed3\u8bba\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u904d\u5386\u5b57\u5178<\/strong>\uff1a\u5b9e\u73b0\u7b80\u5355\uff0c\u9002\u5408\u521d\u5b66\u8005\uff0c\u9002\u7528\u4e8e\u5b57\u5178\u8f83\u5c0f\u6216\u5076\u5c14\u67e5\u627e\u7684\u60c5\u51b5\u3002<\/li>\n<li><strong>\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\uff1a\u4ee3\u7801\u7b80\u6d01\u3001\u4f18\u96c5\uff0c\u9002\u7528\u4e8e\u5b57\u5178\u8f83\u5c0f\u6216\u5076\u5c14\u67e5\u627e\u7684\u60c5\u51b5\u3002<\/li>\n<li><strong>\u5b57\u5178\u53cd\u8f6c<\/strong>\uff1a\u67e5\u627e\u590d\u6742\u5ea6\u4e3aO(1)\uff0c\u9002\u7528\u4e8e\u9891\u7e41\u67e5\u627e\uff0c\u9700\u989d\u5916\u7a7a\u95f4\u5b58\u50a8\u53cd\u8f6c\u540e\u7684\u5b57\u5178\u3002<\/li>\n<li><strong>filter\u51fd\u6570<\/strong>\uff1a\u9002\u7528\u4e8e\u503c\u53ef\u80fd\u91cd\u590d\u7684\u60c5\u51b5\uff0c\u67e5\u627e\u591a\u4e2a\u5339\u914d\u952e\u503c\u5bf9\u3002<\/li>\n<li><strong>\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u9010\u4e2a\u5904\u7406\u627e\u5230\u7684\u952e\u503c\u5bf9\u7684\u60c5\u51b5\uff0c\u60f0\u6027\u6c42\u503c\uff0c\u8282\u7701\u5185\u5b58\u3002<\/li>\n<li><strong>\u81ea\u5b9a\u4e49\u7c7b<\/strong>\uff1a\u9002\u7528\u4e8e\u590d\u6742\u67e5\u627e\u903b\u8f91\uff0c\u4ee3\u7801\u7ed3\u6784\u6e05\u6670\uff0c\u9002\u5408\u9ad8\u7ea7\u5f00\u53d1\u8005\u3002<\/li>\n<\/ol>\n<p><p>\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u5b57\u5178\u7684\u7279\u70b9\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u67e5\u627e\u6548\u7387\uff0c\u4f18\u5316\u4ee3\u7801\u6027\u80fd\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7ed3\u5408\u4e0a\u8ff0\u65b9\u6cd5\u8fdb\u884c\u7075\u6d3b\u8fd0\u7528\uff0c\u5c06\u80fd\u591f\u66f4\u52a0\u9ad8\u6548\u5730\u5b9e\u73b0\u901a\u8fc7\u503c\u627e\u952e\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6839\u636e\u503c\u67e5\u627e\u5b57\u5178\u7684\u952e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u7684\u904d\u5386\u529f\u80fd\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u6765\u6839\u636e\u503c\u67e5\u627e\u952e\u3002\u5177\u4f53\u7684\u5b9e\u73b0\u65b9\u5f0f\u662f\u901a\u8fc7for\u5faa\u73af\u904d\u5386\u5b57\u5178\u7684items()\uff0c\u5e76\u68c0\u67e5\u6bcf\u4e2a\u503c\u662f\u5426\u5339\u914d\u76ee\u6807\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}\nkey = [k for k, v in my_dict.items() if v == 2]\nprint(key)  # \u8f93\u51fa [&#39;b&#39;]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u80fd\u591f\u9ad8\u6548\u5730\u627e\u5230\u6240\u6709\u4e0e\u6307\u5b9a\u503c\u76f8\u5bf9\u5e94\u7684\u952e\u3002<\/p>\n<p><strong>\u5982\u679c\u5b57\u5178\u4e2d\u6709\u591a\u4e2a\u76f8\u540c\u7684\u503c\uff0c\u5982\u4f55\u83b7\u53d6\u6240\u6709\u76f8\u5173\u7684\u952e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5982\u679c\u5b57\u5178\u4e2d\u5b58\u5728\u91cd\u590d\u7684\u503c\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u4ecd\u7136\u80fd\u591f\u83b7\u53d6\u6240\u6709\u76f8\u5173\u7684\u952e\u3002\u53ea\u9700\u7ee7\u7eed\u4f7f\u7528\u4e0a\u9762\u63d0\u5230\u7684\u65b9\u6cd5\uff0c\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5339\u914d\u952e\u7684\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 2}\nkeys = [k for k, v in my_dict.items() if v == 2]\nprint(keys)  # \u8f93\u51fa [&#39;b&#39;, &#39;c&#39;]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u4e86\u5373\u4f7f\u6709\u591a\u4e2a\u952e\u5bf9\u5e94\u540c\u4e00\u4e2a\u503c\uff0c\u4e5f\u80fd\u5168\u90e8\u6355\u6349\u5230\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5185\u7f6e\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u67e5\u627e\u5b57\u5178\u4e2d\u7684\u952e\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528\u57fa\u672c\u7684\u5faa\u73af\u548c\u6761\u4ef6\u5224\u65ad\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u51fd\u6570\uff0c\u4f8b\u5982<code>filter()<\/code>\u548c<code>lambda<\/code>\u3002\u901a\u8fc7\u8fd9\u4e9b\u5de5\u5177\uff0c\u7528\u6237\u53ef\u4ee5\u521b\u5efa\u66f4\u4e3a\u7b80\u6d01\u7684\u4ee3\u7801\u6765\u67e5\u627e\u952e\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}\nresult = list(filter(lambda k: my_dict[k] == 2, my_dict))\nprint(result)  # \u8f93\u51fa [&#39;b&#39;]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u8f83\u5927\u7684\u5b57\u5178\u65f6\uff0c\u80fd\u591f\u63d0\u5347\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u901a\u8fc7\u503c\u627e\u952e\u7684\u5e38\u89c1\u65b9\u6cd5\u6709\u904d\u5386\u5b57\u5178\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528\u5b57\u5178\u53cd\u8f6c\u7b49\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u7684\u904d\u5386\u5b57\u5178 [&hellip;]","protected":false},"author":3,"featured_media":1171991,"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\/1171984"}],"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=1171984"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1171984\/revisions"}],"predecessor-version":[{"id":1171992,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1171984\/revisions\/1171992"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1171991"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1171984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1171984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1171984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}