{"id":1127913,"date":"2025-01-08T20:15:00","date_gmt":"2025-01-08T12:15:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1127913.html"},"modified":"2025-01-08T20:15:03","modified_gmt":"2025-01-08T12:15:03","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%98%af%e5%90%a6%e5%9c%a8%e5%ad%97%e5%85%b8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1127913.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u5728\u5b57\u5178"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094756\/03ee0cef-c2dd-4e19-8ef2-d6bb22e1dfbe.webp\" alt=\"python\u4e2d\u5982\u4f55\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u5728\u5b57\u5178\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u5728\u5b57\u5178\u7684\u5173\u952e\u5728\u4e8e\u4f7f\u7528\u5b57\u5178\u7684\u952e\u8fdb\u884c\u67e5\u627e<\/strong>\u3001<strong>\u5229\u7528<code>in<\/code>\u64cd\u4f5c\u7b26<\/strong>\u3001<strong>\u7ed3\u5408\u6761\u4ef6\u5224\u65ad<\/strong>\u3002\u5176\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u5229\u7528<code>in<\/code>\u64cd\u4f5c\u7b26\u6765\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u5b57\u5178\u7684\u952e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>in<\/code>\u64cd\u4f5c\u7b26\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u5728\u5b57\u5178\u7684\u952e\u4e2d<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>in<\/code>\u64cd\u4f5c\u7b26\u662f\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u5728\u5b57\u5178\u4e2d\u7684\u6700\u7b80\u4fbf\u548c\u76f4\u89c2\u7684\u65b9\u6cd5\u3002\u5b57\u5178\u7684\u952e\u96c6\u5408\u652f\u6301\u5feb\u901f\u67e5\u627e\uff0c\u56e0\u6b64\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9ad8\u6548\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>key_to_check = &#39;name&#39;<\/p>\n<p>if key_to_check in my_dict:<\/p>\n<p>    print(f&quot;&#39;{key_to_check}&#39; is a key in the dictionary&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;&#39;{key_to_check}&#39; is not a key in the dictionary&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>&#39;name&#39;<\/code>\u662f\u5b57\u5178\u7684\u4e00\u4e2a\u952e\uff0c\u56e0\u6b64\u8f93\u51fa\u5c06\u662f<code>&#39;name&#39; is a key in the dictionary<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5b57\u5178\u7684<code>keys()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u76f4\u63a5\u4f7f\u7528<code>in<\/code>\u64cd\u4f5c\u7b26\u5df2\u7ecf\u5f88\u7b80\u4fbf\uff0c\u4f46\u6709\u65f6\u5019\u4f60\u53ef\u80fd\u60f3\u66f4\u660e\u786e\u5730\u4f7f\u7528\u5b57\u5178\u7684<code>keys()<\/code>\u65b9\u6cd5\u6765\u68c0\u67e5\u952e\u3002\u8fd9\u4e5f\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>key_to_check = &#39;name&#39;<\/p>\n<p>if key_to_check in my_dict.keys():<\/p>\n<p>    print(f&quot;&#39;{key_to_check}&#39; is a key in the dictionary&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;&#39;{key_to_check}&#39; is not a key in the dictionary&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>try-except<\/code>\u7ed3\u6784\u8fdb\u884c\u5224\u65ad<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u4f1a\u5e0c\u671b\u6355\u83b7\u5c1d\u8bd5\u8bbf\u95ee\u4e0d\u5b58\u5728\u952e\u65f6\u629b\u51fa\u7684\u5f02\u5e38\u3002\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u7ed3\u6784\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>key_to_check = &#39;name&#39;<\/p>\n<p>try:<\/p>\n<p>    value = my_dict[key_to_check]<\/p>\n<p>    print(f&quot;&#39;{key_to_check}&#39; is a key in the dictionary with value {value}&quot;)<\/p>\n<p>except KeyError:<\/p>\n<p>    print(f&quot;&#39;{key_to_check}&#39; is not a key in the dictionary&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u597d\u5904\u662f\u4f60\u53ef\u4ee5\u540c\u65f6\u83b7\u53d6\u952e\u5bf9\u5e94\u7684\u503c\uff0c\u800c\u4e0d\u9700\u8981\u8fdb\u884c\u4e24\u6b21\u67e5\u627e\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6027\u80fd\u8003\u8651<\/h3>\n<\/p>\n<p><p>\u5728\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\uff0c\u76f4\u63a5\u4f7f\u7528<code>in<\/code>\u64cd\u4f5c\u7b26\u662f\u6700\u597d\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u6027\u80fd\u6700\u4f73\u3002\u5b57\u5178\u5728Python\u4e2d\u662f\u901a\u8fc7\u54c8\u5e0c\u8868\u5b9e\u73b0\u7684\uff0c\u8fd9\u610f\u5473\u7740\u952e\u7684\u67e5\u627e\u64cd\u4f5c\u5728\u5e73\u5747\u60c5\u51b5\u4e0b\u662fO(1)\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u4f7f\u7528<code>keys()<\/code>\u65b9\u6cd5\u867d\u7136\u53ef\u8bfb\u6027\u9ad8\uff0c\u4f46\u7531\u4e8e\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u952e\u7684\u89c6\u56fe\u5bf9\u8c61\uff0c\u53ef\u80fd\u4f1a\u7a0d\u5fae\u5f71\u54cd\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5b9e\u6218\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1. \u68c0\u67e5\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u952e<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u4ece\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bfb\u53d6\u67d0\u4e9b\u8bbe\u7f6e\u5e76\u8fdb\u884c\u68c0\u67e5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">config = {<\/p>\n<p>    &#39;database&#39;: &#39;mysql&#39;,<\/p>\n<p>    &#39;host&#39;: &#39;localhost&#39;,<\/p>\n<p>    &#39;port&#39;: 3306<\/p>\n<p>}<\/p>\n<p>required_keys = [&#39;database&#39;, &#39;host&#39;, &#39;port&#39;, &#39;user&#39;]<\/p>\n<p>for key in required_keys:<\/p>\n<p>    if key not in config:<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se KeyError(f&quot;Missing required config key: {key}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6570\u636e\u6e05\u6d17\u548c\u9884\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u6e05\u6d17\u548c\u9884\u5904\u7406\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u68c0\u67e5\u5b57\u5178\u4e2d\u662f\u5426\u5305\u542b\u67d0\u4e9b\u5fc5\u8981\u5b57\u6bb5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data_records = [<\/p>\n<p>    {&#39;id&#39;: 1, &#39;name&#39;: &#39;Alice&#39;, &#39;email&#39;: &#39;alice@example.com&#39;},<\/p>\n<p>    {&#39;id&#39;: 2, &#39;name&#39;: &#39;Bob&#39;},  # missing email<\/p>\n<p>    {&#39;id&#39;: 3, &#39;name&#39;: &#39;Charlie&#39;, &#39;email&#39;: &#39;charlie@example.com&#39;}<\/p>\n<p>]<\/p>\n<p>for record in data_records:<\/p>\n<p>    if &#39;email&#39; not in record:<\/p>\n<p>        record[&#39;email&#39;] = &#39;N\/A&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u52a8\u6001\u5904\u7406\u7528\u6237\u8f93\u5165<\/h4>\n<\/p>\n<p><p>\u5728\u7528\u6237\u8f93\u5165\u9700\u8981\u52a8\u6001\u5904\u7406\u7684\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u5224\u65ad\u7528\u6237\u8f93\u5165\u7684\u952e\u662f\u5426\u5728\u5b57\u5178\u4e2d\u4e5f\u662f\u5e38\u89c1\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">user_profile = {<\/p>\n<p>    &#39;username&#39;: &#39;john_doe&#39;,<\/p>\n<p>    &#39;email&#39;: &#39;john@example.com&#39;<\/p>\n<p>}<\/p>\n<p>user_input = &#39;phone_number&#39;<\/p>\n<p>if user_input in user_profile:<\/p>\n<p>    print(f&quot;User profile contains {user_input}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;User profile does not contain {user_input}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>in<\/code>\u64cd\u4f5c\u7b26\u76f4\u63a5\u5224\u65ad<\/strong>\u3001<strong>\u5229\u7528<code>keys()<\/code>\u65b9\u6cd5\u8fdb\u884c\u53ef\u8bfb\u6027\u66f4\u9ad8\u7684\u5224\u65ad<\/strong>\u3001<strong>\u901a\u8fc7<code>try-except<\/code>\u7ed3\u6784\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/strong>\u662fPython\u4e2d\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u5728\u5b57\u5178\u4e2d\u7684\u4e3b\u8981\u65b9\u6cd5\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u573a\u666f\u548c\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7efc\u5408\u8003\u8651\u6027\u80fd\u548c\u4ee3\u7801\u53ef\u8bfb\u6027\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728\u4e0d\u540c\u573a\u666f\u4e2d\u7075\u6d3b\u5730\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u5728\u5b57\u5178\u4e2d\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u5b57\u5178\u7684\u952e\uff1f<\/strong><br \/>\u8981\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u662f\u5b57\u5178\u7684\u952e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u8fd0\u7b97\u7b26\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u5b57\u5178<code>my_dict<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>if my_string in my_dict:<\/code>\u6765\u5224\u65ad<code>my_string<\/code>\u662f\u5426\u4e3a\u5b57\u5178\u4e2d\u7684\u952e\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7b80\u5355\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u6240\u6709\u7c7b\u578b\u7684\u5b57\u5178\u3002<\/p>\n<p><strong>\u5728Python\u5b57\u5178\u4e2d\u67e5\u627e\u5b57\u7b26\u4e32\u65f6\uff0c\u5927\u5c0f\u5199\u654f\u611f\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u5b57\u5178\u7684\u952e\u662f\u5927\u5c0f\u5199\u654f\u611f\u7684\u3002\u8fd9\u610f\u5473\u7740\u5b57\u7b26\u4e32<code>&quot;Key&quot;<\/code>\u548c<code>&quot;key&quot;<\/code>\u88ab\u89c6\u4e3a\u4e0d\u540c\u7684\u952e\u3002\u5982\u679c\u9700\u8981\u8fdb\u884c\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u67e5\u627e\uff0c\u53ef\u4ee5\u5c06\u952e\u548c\u8981\u67e5\u627e\u7684\u5b57\u7b26\u4e32\u90fd\u8f6c\u6362\u4e3a\u76f8\u540c\u7684\u5927\u5c0f\u5199\uff0c\u4f8b\u5982\u4f7f\u7528<code>lower()<\/code>\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5b57\u5178\u4e2d\u4e0d\u5b58\u5728\u7684\u5b57\u7b26\u4e32\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u5c1d\u8bd5\u67e5\u627e\u4e00\u4e2a\u4e0d\u5728\u5b57\u5178\u4e2d\u7684\u5b57\u7b26\u4e32\uff0cPython\u4e0d\u4f1a\u5f15\u53d1\u9519\u8bef\uff0c\u800c\u662f\u8fd4\u56de<code>False<\/code>\uff0c\u5982\u679c\u4f7f\u7528<code>in<\/code>\u8fd0\u7b97\u7b26\u3002\u4e3a\u4e86\u907f\u514d\u5bf9\u4e0d\u5b58\u5728\u7684\u952e\u8fdb\u884c\u64cd\u4f5c\uff0c\u63a8\u8350\u4f7f\u7528<code>get()<\/code>\u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8\u4f60\u6307\u5b9a\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002\u4f8b\u5982\uff0c<code>value = my_dict.get(my_string, &quot;\u9ed8\u8ba4\u503c&quot;)<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de\u5b57\u5178\u4e2d\u5bf9\u5e94\u952e\u7684\u503c\uff0c\u5982\u679c\u952e\u4e0d\u5b58\u5728\u5219\u8fd4\u56de&quot;\u9ed8\u8ba4\u503c&quot;\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u5728\u5b57\u5178\u7684\u5173\u952e\u5728\u4e8e\u4f7f\u7528\u5b57\u5178\u7684\u952e\u8fdb\u884c\u67e5\u627e\u3001\u5229\u7528in\u64cd\u4f5c\u7b26\u3001\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u3002\u5176\u4e2d\u6700\u5e38\u7528\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1127919,"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\/1127913"}],"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=1127913"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1127913\/revisions"}],"predecessor-version":[{"id":1127920,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1127913\/revisions\/1127920"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1127919"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1127913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1127913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1127913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}