{"id":1075799,"date":"2025-01-08T11:47:31","date_gmt":"2025-01-08T03:47:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1075799.html"},"modified":"2025-01-08T11:47:34","modified_gmt":"2025-01-08T03:47:34","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%88%a4%e6%96%ad%e5%88%97%e8%a1%a8%e4%b8%ad%e5%ad%98%e5%9c%a8-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1075799.html","title":{"rendered":"python\u5982\u4f55\u5c06\u5224\u65ad\u5217\u8868\u4e2d\u5b58\u5728"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24180707\/2aa3d976-d922-4249-9f04-a3f87abcc9bb.webp\" alt=\"python\u5982\u4f55\u5c06\u5224\u65ad\u5217\u8868\u4e2d\u5b58\u5728\" \/><\/p>\n<p><p> <strong>\u6838\u5fc3\u89c2\u70b9\uff1a<\/strong> \u4f7f\u7528 <code>in<\/code> \u8fd0\u7b97\u7b26\u3001\u4f7f\u7528 <code>any()<\/code> \u51fd\u6570\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3002<strong>\u5176\u4e2d\uff0c\u4f7f\u7528 <code>in<\/code> \u8fd0\u7b97\u7b26 \u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5<\/strong>\u3002\u901a\u8fc7 <code>in<\/code> \u8fd0\u7b97\u7b26\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5224\u65ad\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\uff0c\u4f8b\u5982 <code>element in list<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u8868\u660e\u5143\u7d20\u662f\u5426\u5728\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5224\u65ad\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u5728\u5217\u8868\u4e2d\u662f\u4e00\u9879\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u5c31\u662f\u4f7f\u7528 <code>in<\/code> \u8fd0\u7b97\u7b26\uff0c\u8fd9\u4e0d\u4ec5\u6613\u4e8e\u7406\u89e3\uff0c\u800c\u4e14\u6027\u80fd\u4e5f\u8f83\u597d\u3002<code>in<\/code> \u8fd0\u7b97\u7b26\u4f1a\u904d\u5386\u6574\u4e2a\u5217\u8868\u4ee5\u68c0\u67e5\u5143\u7d20\u662f\u5426\u5b58\u5728\uff0c\u4f46\u5bf9\u4e8e\u5927\u591a\u6570\u5b9e\u9645\u5e94\u7528\u573a\u666f\u6765\u8bf4\uff0c\u8fd9\u79cd\u6027\u80fd\u5f00\u9500\u662f\u53ef\u4ee5\u63a5\u53d7\u7684\u3002\u6b64\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u8fbe\u5230\u540c\u6837\u7684\u6548\u679c\uff0c\u4f8b\u5982\u4f7f\u7528 <code>any()<\/code> \u51fd\u6570\u6216\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5728\u7279\u5b9a\u7684\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u66f4\u6709\u6548\u6216\u66f4\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528 <code>in<\/code> \u8fd0\u7b97\u7b26<\/p>\n<\/p>\n<p><p><code>in<\/code> \u8fd0\u7b97\u7b26\u662f\u5224\u65ad\u5217\u8868\u4e2d\u662f\u5426\u5b58\u5728\u67d0\u4e2a\u5143\u7d20\u7684\u6700\u76f4\u63a5\u65b9\u5f0f\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u8868\u793a\u5143\u7d20\u662f\u5426\u5728\u5217\u8868\u4e2d\u3002<\/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} exists in the list.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{element} does not exist in the list.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>in<\/code> \u8fd0\u7b97\u7b26\u904d\u5386\u5217\u8868 <code>my_list<\/code>\uff0c\u5e76\u68c0\u67e5 <code>element<\/code> \u662f\u5426\u5b58\u5728\u3002\u5982\u679c\u5b58\u5728\uff0c\u5b83\u8fd4\u56de <code>True<\/code>\uff0c\u5426\u5219\u8fd4\u56de <code>False<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528 <code>any()<\/code> \u51fd\u6570<\/p>\n<\/p>\n<p><p>\u53e6\u4e00\u4e2a\u65b9\u6cd5\u662f\u4f7f\u7528 <code>any()<\/code> \u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u4e0e\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u4e00\u8d77\u4f7f\u7528\u6765\u5224\u65ad\u5217\u8868\u4e2d\u662f\u5426\u5b58\u5728\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002<\/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} exists in the list.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{element} does not exist in the list.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>any()<\/code> \u51fd\u6570\u4f1a\u904d\u5386\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u76f4\u5230\u627e\u5230\u4e00\u4e2a\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\u4e3a\u6b62\u3002\u5982\u679c\u627e\u5230\uff0c\u8fd4\u56de <code>True<\/code>\uff0c\u5426\u5219\u8fd4\u56de <code>False<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u5217\u8868\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5224\u65ad\u5217\u8868\u4e2d\u662f\u5426\u5b58\u5728\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002\u7ed3\u5408 <code>bool()<\/code> \u51fd\u6570\uff0c\u53ef\u4ee5\u8fbe\u5230\u4e0e <code>in<\/code> \u8fd0\u7b97\u7b26\u548c <code>any()<\/code> \u51fd\u6570\u7c7b\u4f3c\u7684\u6548\u679c\u3002<\/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>exists = bool([x for x in my_list if x == element])<\/p>\n<p>if exists:<\/p>\n<p>    print(f&quot;{element} exists in the list.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;{element} does not exist in the list.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\u7684\u5217\u8868\u3002<code>bool()<\/code> \u51fd\u6570\u5c06\u8fd9\u4e2a\u5217\u8868\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\uff0c\u5982\u679c\u5217\u8868\u4e0d\u4e3a\u7a7a\uff0c\u5219\u8fd4\u56de <code>True<\/code>\uff0c\u5426\u5219\u8fd4\u56de <code>False<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u6027\u80fd\u6bd4\u8f83<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u60c5\u51b5\u548c\u504f\u597d\u3002\u5728\u6027\u80fd\u65b9\u9762\uff0c<code>in<\/code> \u8fd0\u7b97\u7b26\u901a\u5e38\u662f\u6700\u4f18\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u662fPython\u5185\u7f6e\u7684\uff0c\u5e76\u4e14\u76f4\u63a5\u5728C\u8bed\u8a00\u5c42\u9762\u5b9e\u73b0\uff0c\u56e0\u6b64\u6548\u7387\u8f83\u9ad8\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6027\u80fd\u6bd4\u8f83\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>my_list = list(range(1000000))<\/p>\n<p>element = 999999<\/p>\n<h2><strong>Using `in` operator<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>_ = element in my_list<\/p>\n<p>print(f&quot;`in` operator: {time.time() - start_time} seconds&quot;)<\/p>\n<h2><strong>Using `any()` function<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>_ = any(x == element for x in my_list)<\/p>\n<p>print(f&quot;`any()` function: {time.time() - start_time} seconds&quot;)<\/p>\n<h2><strong>Using list comprehension<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>_ = bool([x for x in my_list if x == element])<\/p>\n<p>print(f&quot;List comprehension: {time.time() - start_time} seconds&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c<code>in<\/code> \u8fd0\u7b97\u7b26\u7684\u6027\u80fd\u660e\u663e\u4f18\u4e8e <code>any()<\/code> \u51fd\u6570\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u5c24\u5176\u662f\u5728\u5217\u8868\u8f83\u5927\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5b9e\u7528\u573a\u666f\u548c\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5224\u65ad\u5217\u8868\u4e2d\u662f\u5426\u5b58\u5728\u67d0\u4e2a\u5143\u7d20\u662f\u975e\u5e38\u5e38\u89c1\u7684\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u68c0\u67e5\u7528\u6237\u8f93\u5165\u662f\u5426\u5728\u9884\u5b9a\u4e49\u7684\u9009\u9879\u5217\u8868\u4e2d\uff1b\u5728\u6570\u636e\u5904\u7406\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5224\u65ad\u67d0\u4e2a\u6570\u636e\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u4e8e\u96c6\u5408\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># Example of user input validation<\/p>\n<p>valid_choices = [&quot;yes&quot;, &quot;no&quot;, &quot;maybe&quot;]<\/p>\n<p>user_input = input(&quot;Enter your choice: &quot;)<\/p>\n<p>if user_input in valid_choices:<\/p>\n<p>    print(&quot;Valid choice!&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Invalid choice!&quot;)<\/p>\n<h2><strong>Example of data processing<\/strong><\/h2>\n<p>data_set = [1001, 1002, 1003, 1004, 1005]<\/p>\n<p>new_data = 1003<\/p>\n<p>if new_data in data_set:<\/p>\n<p>    print(&quot;Data already exists.&quot;)<\/p>\n<p>else:<\/p>\n<p>    data_set.append(new_data)<\/p>\n<p>    print(&quot;Data added.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e9b\u573a\u666f\u4e2d\uff0c\u4f7f\u7528 <code>in<\/code> \u8fd0\u7b97\u7b26\u53ef\u4ee5\u4f7f\u4ee3\u7801\u7b80\u6d01\u6613\u8bfb\uff0c\u5e76\u4e14\u6027\u80fd\u8868\u73b0\u4e5f\u8db3\u591f\u4f18\u79c0\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\u6765\u8bf4\uff0c\u5224\u65ad\u5217\u8868\u4e2d\u662f\u5426\u5b58\u5728\u67d0\u4e2a\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u6700\u5e38\u7528\u548c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\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\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c<code>in<\/code> \u8fd0\u7b97\u7b26\u662f\u6700\u4f73\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u65e2\u7b80\u5355\u53c8\u9ad8\u6548\u3002\u7406\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u66f4\u52a0\u7075\u6d3b\u5730\u5904\u7406\u5404\u79cd\u6570\u636e\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u5217\u8868\u4e2d\u662f\u5426\u5b58\u5728\u67d0\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u6765\u5224\u65ad\u67d0\u4e2a\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5217\u8868<code>my_list = [1, 2, 3, 4, 5]<\/code>\uff0c\u4f60\u53ef\u4ee5\u7528<code>if 3 in my_list:<\/code>\u6765\u5224\u65ad3\u662f\u5426\u5728\u5217\u8868\u4e2d\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7b80\u5355\u4e14\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<p><strong>Python\u4e2d\u5224\u65ad\u5217\u8868\u662f\u5426\u4e3a\u7a7a\u7684\u65b9\u6cd5\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5224\u65ad\u5217\u8868\u662f\u5426\u4e3a\u7a7a\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u3002\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u662f\u76f4\u63a5\u4f7f\u7528<code>if not my_list:<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de<code>True<\/code>\uff0c\u5982\u679c\u5217\u8868\u4e3a\u7a7a\u3002\u6b64\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>len(my_list) == 0<\/code>\u6765\u68c0\u67e5\u5217\u8868\u7684\u957f\u5ea6\u662f\u5426\u4e3a0\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u662f\u6709\u6548\u7684\u3002<\/p>\n<p><strong>\u5982\u4f55\u627e\u5230\u5217\u8868\u4e2d\u6240\u6709\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u627e\u51fa\u5217\u8868\u4e2d\u6240\u6709\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216<code>filter()<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u60f3\u4ece\u5217\u8868<code>my_list = [1, 2, 3, 4, 5]<\/code>\u4e2d\u627e\u5230\u6240\u6709\u5076\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\uff1a<code>even_numbers = [x for x in my_list if x % 2 == 0]<\/code>\uff0c\u8fd9\u6837\u4f60\u5c06\u5f97\u5230\u4e00\u4e2a\u65b0\u7684\u5305\u542b\u6240\u6709\u5076\u6570\u7684\u5217\u8868\u3002\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u5b9a\u4e49\u4efb\u4f55\u6761\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u6838\u5fc3\u89c2\u70b9\uff1a \u4f7f\u7528 in \u8fd0\u7b97\u7b26\u3001\u4f7f\u7528 any() \u51fd\u6570\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u5176\u4e2d\uff0c\u4f7f\u7528 in \u8fd0\u7b97\u7b26 \u662f\u6700\u7b80\u5355\u76f4\u63a5 [&hellip;]","protected":false},"author":3,"featured_media":1075807,"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\/1075799"}],"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=1075799"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1075799\/revisions"}],"predecessor-version":[{"id":1075809,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1075799\/revisions\/1075809"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1075807"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1075799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1075799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1075799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}