{"id":1138237,"date":"2025-01-08T21:57:35","date_gmt":"2025-01-08T13:57:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1138237.html"},"modified":"2025-01-08T21:57:50","modified_gmt":"2025-01-08T13:57:50","slug":"python%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e4%b8%80%e4%b8%aa%e5%80%bc%e6%98%af%e5%90%a6%e5%80%bc%e5%88%97%e8%a1%a8%e9%87%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1138237.html","title":{"rendered":"python\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u503c\u662f\u5426\u503c\u5217\u8868\u91cc"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101903\/9cf5316c-b87f-45ef-86ab-12362450949b.webp\" alt=\"python\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u503c\u662f\u5426\u503c\u5217\u8868\u91cc\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u5224\u65ad\u4e00\u4e2a\u503c\u662f\u5426\u5728\u5217\u8868\u91cc\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u8fd0\u7b97\u7b26\u3001<code>any()<\/code>\u51fd\u6570\u3001\u5faa\u73af\u7b49\u65b9\u6cd5\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>in<\/code>\u8fd0\u7b97\u7b26\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u4e14\u9ad8\u6548\u3002\u4e0b\u9762\u8be6\u7ec6\u89e3\u91ca\u5982\u4f55\u4f7f\u7528<code>in<\/code>\u8fd0\u7b97\u7b26\u6765\u5224\u65ad\u4e00\u4e2a\u503c\u662f\u5426\u5728\u5217\u8868\u91cc\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>in<\/code>\u8fd0\u7b97\u7b26\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u5f53\u4f60\u4f7f\u7528<code>in<\/code>\u65f6\uff0cPython\u4f1a\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u68c0\u67e5\u662f\u5426\u6709\u4e00\u4e2a\u5143\u7d20\u4e0e\u7ed9\u5b9a\u503c\u76f8\u7b49\u3002\u5982\u679c\u627e\u5230\u76f8\u7b49\u7684\u5143\u7d20\uff0c\u8868\u8fbe\u5f0f\u8fd4\u56de<code>True<\/code>\uff0c\u5426\u5219\u8fd4\u56de<code>False<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u5355\u6613\u61c2\uff0c\u800c\u4e14\u6027\u80fd\u4e5f\u76f8\u5bf9\u8f83\u597d\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u7684\u5217\u8868\u67e5\u627e\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528<code>in<\/code>\u8fd0\u7b97\u7b26<\/h2>\n<\/p>\n<p><p><code>in<\/code>\u8fd0\u7b97\u7b26\u662fPython\u5185\u7f6e\u7684\u8fd0\u7b97\u7b26\u4e4b\u4e00\uff0c\u7528\u4e8e\u68c0\u67e5\u67d0\u4e2a\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u4e00\u4e2a\u5e8f\u5217\uff08\u5982\u5217\u8868\u3001\u5b57\u7b26\u4e32\u3001\u5143\u7ec4\uff09\u4e2d\u3002\u5176\u8bed\u6cd5\u975e\u5e38\u7b80\u5355\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value in list<\/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_list = [1, 2, 3, 4, 5]<\/p>\n<p>value = 3<\/p>\n<p>if value in my_list:<\/p>\n<p>    print(f&quot;{value} exists in the list.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{value} does not exist in the list.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>value<\/code>\uff08\u53733\uff09\u786e\u5b9e\u5b58\u5728\u4e8e<code>my_list<\/code>\u4e2d\uff0c\u6240\u4ee5\u8f93\u51fa\u7ed3\u679c\u5c06\u662f &quot;3 exists in the list.&quot;<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528<code>any()<\/code>\u51fd\u6570<\/h2>\n<\/p>\n<p><p><code>any()<\/code>\u51fd\u6570\u53ef\u4ee5\u4e0e\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u63d0\u4f9b\u4e00\u79cd\u7075\u6d3b\u7684\u89e3\u51b3\u65b9\u6848\u3002<code>any()<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u5982\u679c\u8fed\u4ee3\u5668\u4e2d\u7684\u4efb\u4f55\u5143\u7d20\u4e3a\u771f\uff0c\u5219\u8fd4\u56de<code>True<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>value = 3<\/p>\n<p>if any(x == value for x in my_list):<\/p>\n<p>    print(f&quot;{value} exists in the list.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{value} does not exist in the list.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u5faa\u73af<\/h2>\n<\/p>\n<p><p>\u867d\u7136\u4e0d\u5982\u524d\u9762\u7684\u65b9\u6cd5\u7b80\u6d01\uff0c\u4f46\u4f7f\u7528\u5faa\u73af\u4e5f\u662f\u4e00\u79cd\u53ef\u4ee5\u8003\u8651\u7684\u65b9\u6cd5\uff0c\u5c24\u5176\u662f\u5f53\u4f60\u9700\u8981\u5bf9\u5143\u7d20\u8fdb\u884c\u4e00\u4e9b\u590d\u6742\u7684\u68c0\u67e5\u65f6\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>value = 3<\/p>\n<p>found = False<\/p>\n<p>for x in my_list:<\/p>\n<p>    if x == value:<\/p>\n<p>        found = True<\/p>\n<p>        break<\/p>\n<p>if found:<\/p>\n<p>    print(f&quot;{value} exists in the list.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{value} does not exist in the list.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u6027\u80fd\u6bd4\u8f83<\/h2>\n<\/p>\n<p><p>\u5728\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528<code>in<\/code>\u8fd0\u7b97\u7b26\u7684\u6027\u80fd\u662f\u6700\u4f73\u7684\u3002<code>any()<\/code>\u51fd\u6570\u548c\u5faa\u73af\u7684\u65b9\u6cd5\u5728\u67d0\u4e9b\u7279\u5b9a\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u66f4\u7075\u6d3b\uff0c\u4f46\u901a\u5e38\u4e0d\u4f1a\u6bd4<code>in<\/code>\u8fd0\u7b97\u7b26\u66f4\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u6027\u80fd\u6d4b\u8bd5\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>my_list = list(range(1000000))<\/p>\n<p>value = 999999<\/p>\n<p>start_time = time.time()<\/p>\n<p>value in my_list<\/p>\n<p>print(&quot;Using &#39;in&#39;:&quot;, time.time() - start_time)<\/p>\n<p>start_time = time.time()<\/p>\n<p>any(x == value for x in my_list)<\/p>\n<p>print(&quot;Using &#39;any&#39;:&quot;, time.time() - start_time)<\/p>\n<p>start_time = time.time()<\/p>\n<p>found = False<\/p>\n<p>for x in my_list:<\/p>\n<p>    if x == value:<\/p>\n<p>        found = True<\/p>\n<p>        break<\/p>\n<p>print(&quot;Using loop:&quot;, time.time() - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u6027\u80fd\u6d4b\u8bd5\u4e2d\uff0c\u4f60\u4f1a\u53d1\u73b0<code>in<\/code>\u8fd0\u7b97\u7b26\u901a\u5e38\u662f\u6700\u5feb\u7684\uff0c\u5c24\u5176\u662f\u5f53\u5217\u8868\u975e\u5e38\u5927\u65f6\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5224\u65ad\u4e00\u4e2a\u503c\u662f\u5426\u5728\u5217\u8868\u4e2d\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u68c0\u67e5\u7528\u6237\u8f93\u5165\u7684\u503c\u662f\u5426\u5728\u9884\u5b9a\u4e49\u7684\u9009\u9879\u5217\u8868\u4e2d\uff0c\u6216\u8005\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u786e\u4fdd\u67d0\u4e2a\u5173\u952e\u503c\u5b58\u5728\u4e8e\u6570\u636e\u96c6\u5185\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u68c0\u67e5\u7528\u6237\u8f93\u5165\u662f\u5426\u5728\u9009\u9879\u5217\u8868\u4e2d<\/p>\n<p>options = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>user_input = input(&quot;Enter a fruit: &quot;)<\/p>\n<p>if user_input in options:<\/p>\n<p>    print(&quot;Valid option.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Invalid option.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7528\u6237\u8f93\u5165\u7684\u503c\u5c06\u88ab\u68c0\u67e5\u662f\u5426\u5728\u9009\u9879\u5217\u8868\u4e2d\uff0c\u5982\u679c\u662f\u6709\u6548\u9009\u9879\uff0c\u5219\u8f93\u51fa &quot;Valid option.&quot;\uff0c\u5426\u5219\u8f93\u51fa &quot;Invalid option.&quot;<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5224\u65ad\u4e00\u4e2a\u503c\u662f\u5426\u5728\u5217\u8868\u91cc\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5176\u4e2d<code>in<\/code>\u8fd0\u7b97\u7b26\u662f\u6700\u5e38\u7528\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002<code>any()<\/code>\u51fd\u6570\u548c\u5faa\u73af\u4e5f\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u68c0\u67e5\uff0c\u4f46\u901a\u5e38\u4e0d\u4f1a\u6bd4<code>in<\/code>\u8fd0\u7b97\u7b26\u66f4\u9ad8\u6548\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u6ee1\u8db3\u4f60\u7684\u7f16\u7a0b\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u4e00\u4e2a\u503c\u662f\u5426\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u6765\u5224\u65ad\u4e00\u4e2a\u503c\u662f\u5426\u5728\u5217\u8868\u4e2d\u3002\u4f8b\u5982\uff0c<code>if value in my_list:<\/code>\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de\u5e03\u5c14\u503c\uff0c\u8868\u793a\u8be5\u503c\u662f\u5426\u5b58\u5728\u4e8e<code>my_list<\/code>\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u660e\u4e86\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u6570\u636e\u7c7b\u578b\u7684\u5217\u8868\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u7684\u5176\u4ed6\u65b9\u6cd5\u6765\u5224\u65ad\u503c\u662f\u5426\u5728\u5217\u8868\u4e2d\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>list.count()<\/code>\u65b9\u6cd5\u6765\u5224\u65ad\u4e00\u4e2a\u503c\u7684\u51fa\u73b0\u6b21\u6570\u3002\u5982\u679c\u8fd4\u56de\u503c\u5927\u4e8e0\uff0c\u5219\u8868\u793a\u8be5\u503c\u5728\u5217\u8868\u4e2d\u5b58\u5728\u3002\u53ef\u4ee5\u901a\u8fc7<code>my_list.count(value)<\/code>\u6765\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>any()<\/code>\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408\u4e5f\u662f\u4e00\u79cd\u7075\u6d3b\u7684\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u63d0\u9ad8\u67e5\u627e\u6548\u7387\uff1f<\/strong><br \/>\u5f53\u5904\u7406\u5927\u578b\u5217\u8868\u65f6\uff0c\u4f7f\u7528<code>in<\/code>\u8fdb\u884c\u67e5\u627e\u7684\u6548\u7387\u53ef\u80fd\u8f83\u4f4e\u3002\u4e3a\u4e86\u63d0\u9ad8\u6548\u7387\uff0c\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff08set\uff09\uff0c\u56e0\u4e3a\u96c6\u5408\u7684\u67e5\u627e\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(1)\u3002\u901a\u8fc7<code>my_set = set(my_list)<\/code>\uff0c\u4e4b\u540e\u53ef\u4ee5\u4f7f\u7528<code>value in my_set<\/code>\u5feb\u901f\u5224\u65ad\u503c\u662f\u5426\u5b58\u5728\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u5224\u65ad\u4e00\u4e2a\u503c\u662f\u5426\u5728\u5217\u8868\u91cc\u53ef\u4ee5\u4f7f\u7528in\u8fd0\u7b97\u7b26\u3001any()\u51fd\u6570\u3001\u5faa\u73af\u7b49\u65b9\u6cd5\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528i [&hellip;]","protected":false},"author":3,"featured_media":1138256,"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\/1138237"}],"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=1138237"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1138237\/revisions"}],"predecessor-version":[{"id":1138261,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1138237\/revisions\/1138261"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1138256"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1138237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1138237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1138237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}