{"id":1121769,"date":"2025-01-08T19:13:15","date_gmt":"2025-01-08T11:13:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1121769.html"},"modified":"2025-01-08T19:13:17","modified_gmt":"2025-01-08T11:13:17","slug":"python%e5%88%97%e8%a1%a8%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e6%98%af%e5%90%a6%e5%90%ab%e6%9c%89%e8%af%a5%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1121769.html","title":{"rendered":"python\u5217\u8868\u4e2d\u5982\u4f55\u5224\u65ad\u662f\u5426\u542b\u6709\u8be5\u5143\u7d20"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083909\/f70c5c48-cb40-4de2-bc8d-9c32909fc573.webp\" alt=\"python\u5217\u8868\u4e2d\u5982\u4f55\u5224\u65ad\u662f\u5426\u542b\u6709\u8be5\u5143\u7d20\" \/><\/p>\n<p><p> <strong>\u5728Python\u5217\u8868\u4e2d\u5224\u65ad\u662f\u5426\u542b\u6709\u67d0\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>in<\/code>\u8fd0\u7b97\u7b26\u3001<code>any()<\/code>\u51fd\u6570\u3001<code>filter()<\/code>\u51fd\u6570\u3001\u4ee5\u53ca<code>count()<\/code>\u65b9\u6cd5\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u5177\u4f53\u4f7f\u7528\u65f6\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8fdb\u884c\u9009\u62e9\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u4e14\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>in<\/code>\u8fd0\u7b97\u7b26\uff0c\u56e0\u4e3a\u5b83\u76f4\u89c2\u4e14\u4ee3\u7801\u7b80\u6d01\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u4ecb\u7ecd\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\u53ca\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001<code>in<\/code>\u8fd0\u7b97\u7b26<\/h3>\n<\/p>\n<p><p><code>in<\/code>\u8fd0\u7b97\u7b26\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u6765\u5224\u65ad\u5217\u8868\u4e2d\u662f\u5426\u5305\u542b\u67d0\u4e2a\u5143\u7d20\u3002\u5b83\u7b80\u5355\u76f4\u89c2\uff0c\u4ee3\u7801\u53ef\u8bfb\u6027\u5f3a\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>element = 3<\/p>\n<p>if element in my_list:<\/p>\n<p>    print(f&quot;{element} is in the list&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{element} is not in the list&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u7b80\u5355\u76f4\u89c2\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u4f7f\u7528\u3002<\/li>\n<li>\u53ef\u8bfb\u6027\u5f3a\uff0c\u9002\u5408\u5feb\u901f\u5224\u65ad\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5728\u5217\u8868\u8f83\u957f\u65f6\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)\uff0c\u6548\u7387\u53ef\u80fd\u4e0d\u9ad8\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001<code>any()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>any()<\/code>\u51fd\u6570\u53ef\u4ee5\u4e0e\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408\u4f7f\u7528\uff0c\u6765\u5224\u65ad\u5217\u8868\u4e2d\u662f\u5426\u5b58\u5728\u7b26\u5408\u67d0\u4e2a\u6761\u4ef6\u7684\u5143\u7d20\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>element = 3<\/p>\n<p>if any(x == element for x in my_list):<\/p>\n<p>    print(f&quot;{element} is in the list&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{element} is not in the list&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u53ef\u4ee5\u5224\u65ad\u7b26\u5408\u590d\u6742\u6761\u4ef6\u7684\u5143\u7d20\u662f\u5426\u5b58\u5728\u3002<\/li>\n<li>\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u60c5\u51b5\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u590d\u6742\u5ea6\u8f83\u9ad8\uff0c\u7406\u89e3\u548c\u4f7f\u7528\u4e0d\u5982<code>in<\/code>\u8fd0\u7b97\u7b26\u76f4\u89c2\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001<code>filter()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>filter()<\/code>\u51fd\u6570\u4e0e<code>lambda<\/code>\u8868\u8fbe\u5f0f\u7ed3\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u7b5b\u9009\u51fa\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u5e76\u5224\u65ad\u7b5b\u9009\u7ed3\u679c\u662f\u5426\u4e3a\u7a7a\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>element = 3<\/p>\n<p>filtered_list = list(filter(lambda x: x == element, my_list))<\/p>\n<p>if filtered_list:<\/p>\n<p>    print(f&quot;{element} is in the list&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{element} is not in the list&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9002\u7528\u4e8e\u590d\u6742\u6761\u4ef6\u7b5b\u9009\u3002<\/li>\n<li>\u53ef\u4ee5\u76f4\u63a5\u83b7\u5f97\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u5217\u8868\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u590d\u6742\u5ea6\u8f83\u9ad8\uff0c\u7406\u89e3\u548c\u4f7f\u7528\u4e0d\u5982<code>in<\/code>\u8fd0\u7b97\u7b26\u76f4\u89c2\u3002<\/li>\n<li>\u6027\u80fd\u53ef\u80fd\u4e0d\u5982<code>in<\/code>\u8fd0\u7b97\u7b26\u9ad8\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001<code>count()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>count()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7edf\u8ba1\u5217\u8868\u4e2d\u67d0\u4e2a\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u901a\u8fc7\u5224\u65ad\u5176\u6b21\u6570\u662f\u5426\u5927\u4e8e0\u6765\u786e\u5b9a\u5143\u7d20\u662f\u5426\u5b58\u5728\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>element = 3<\/p>\n<p>if my_list.count(element) &gt; 0:<\/p>\n<p>    print(f&quot;{element} is in the list&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{element} is not in the list&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u7b80\u5355\u76f4\u89c2\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u4f7f\u7528\u3002<\/li>\n<li>\u9002\u7528\u4e8e\u5217\u8868\u4e2d\u5143\u7d20\u91cd\u590d\u7684\u60c5\u51b5\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u904d\u5386\u6574\u4e2a\u5217\u8868\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)\uff0c\u6548\u7387\u53ef\u80fd\u4e0d\u9ad8\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u5217\u8868\u4e2d\u5224\u65ad\u662f\u5426\u542b\u6709\u67d0\u4e2a\u5143\u7d20\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u6700\u5e38\u7528\u4e14\u6700\u7b80\u5355\u7684\u662f\u4f7f\u7528<code>in<\/code>\u8fd0\u7b97\u7b26\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u6761\u4ef6\u5224\u65ad\uff0c\u53ef\u4ee5\u4f7f\u7528<code>any()<\/code>\u51fd\u6570\u6216<code>filter()<\/code>\u51fd\u6570\u3002<code>count()<\/code>\u65b9\u6cd5\u9002\u7528\u4e8e\u7edf\u8ba1\u5143\u7d20\u51fa\u73b0\u6b21\u6570\u7684\u60c5\u51b5\u3002\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u5217\u8868\u4e2d\u6709\u6548\u68c0\u67e5\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u5b58\u5728\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u6765\u68c0\u67e5\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\u3002\u4f8b\u5982\uff0c<code>if element in my_list:<\/code>\u5c06\u8fd4\u56deTrue\u6216False\uff0c\u6307\u793a\u5143\u7d20\u662f\u5426\u5728\u5217\u8868\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u975e\u5e38\u9002\u5408\u5927\u591a\u6570\u60c5\u51b5\u3002<\/p>\n<p><strong>\u5728Python\u5217\u8868\u4e2d\u67e5\u627e\u5143\u7d20\u65f6\uff0c\u6027\u80fd\u5982\u4f55\u5f71\u54cd\u6211\u7684\u9009\u62e9\uff1f<\/strong><br \/>\u5f53\u5217\u8868\u8f83\u5927\u65f6\uff0c\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u7684\u67e5\u627e\u6548\u7387\u53ef\u80fd\u4f1a\u53d7\u5230\u5f71\u54cd\uff0c\u5c24\u5176\u662f\u5728\u5217\u8868\u4e2d\u5143\u7d20\u7684\u6570\u91cf\u8fbe\u5230\u6570\u5343\u65f6\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u6765\u5b58\u50a8\u5143\u7d20\uff0c\u56e0\u4e3a\u96c6\u5408\u7684\u67e5\u627e\u901f\u5ea6\u901a\u5e38\u6bd4\u5217\u8868\u66f4\u5feb\u3002\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u540e\uff0c\u4f7f\u7528<code>element in my_set<\/code>\u5c06\u663e\u8457\u63d0\u9ad8\u67e5\u627e\u6548\u7387\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u6765\u68c0\u67e5Python\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u7684<code>.count()<\/code>\u65b9\u6cd5\u6765\u68c0\u67e5\u5143\u7d20\u662f\u5426\u5b58\u5728\u3002\u8c03\u7528<code>my_list.count(element)<\/code>\u5c06\u8fd4\u56de\u5143\u7d20\u5728\u5217\u8868\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u5982\u679c\u8fd4\u56de\u503c\u5927\u4e8e0\uff0c\u5219\u8bf4\u660e\u8be5\u5143\u7d20\u5b58\u5728\u3002\u4e0d\u8fc7\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5728\u6027\u80fd\u4e0a\u4e0d\u5982\u76f4\u63a5\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\uff0c\u56e0\u4e3a<code>.count()<\/code>\u4f1a\u904d\u5386\u6574\u4e2a\u5217\u8868\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u5217\u8868\u4e2d\u5224\u65ad\u662f\u5426\u542b\u6709\u67d0\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528in\u8fd0\u7b97\u7b26\u3001any()\u51fd\u6570\u3001filter()\u51fd\u6570\u3001\u4ee5\u53cac [&hellip;]","protected":false},"author":3,"featured_media":1121774,"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\/1121769"}],"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=1121769"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1121769\/revisions"}],"predecessor-version":[{"id":1121776,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1121769\/revisions\/1121776"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1121774"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1121769"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1121769"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1121769"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}