{"id":2642,"date":"2025-05-28T17:20:01","date_gmt":"2025-06-04T09:58:01","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2642"},"modified":"2025-06-04T17:20:01","modified_gmt":"2025-06-04T09:58:01","slug":"python-list-in-and-not-in-operator","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/python-list-in-and-not-in-operator\/","title":{"rendered":"\u5229\u7528 Python \u4e32\u5217\u7684 `in` \u548c `not in` \u904b\u7b97\u5b50\u8f15\u9b06\u6aa2\u67e5\u5143\u7d20\u5b58\u5728\u6027"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, \u4e32\u5217, in, not in, \u904b\u7b97\u5b50, \u6aa2\u67e5\u5143\u7d20, Python \u6559\u5b78, Python \u8a9e\u6cd5\"><\/p>\n<p>\u5728 Python \u4e2d\uff0c\u4e32\u5217\uff08List\uff09\u662f\u4e00\u7a2e\u975e\u5e38\u5e38\u7528\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u53ef\u4ee5\u7528\u4f86\u5132\u5b58\u591a\u500b\u5143\u7d20\u3002\u900f\u904e `in` \u548c `not in` \u904b\u7b97\u5b50\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u6aa2\u67e5\u67d0\u500b\u5143\u7d20\u662f\u5426\u5b58\u5728\u65bc\u4e32\u5217\u4e2d\uff0c\u9019\u5c0d\u65bc\u8cc7\u6599\u9a57\u8b49\u548c\u689d\u4ef6\u908f\u8f2f\u975e\u5e38\u6709\u5e6b\u52a9\u3002\u4ee5\u4e0b\u662f 2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>1. Python \u4e32\u5217 `in` \u904b\u7b97\u5b50<\/h2>\n<p>`in` \u904b\u7b97\u5b50\u7528\u4f86\u6aa2\u67e5\u4e00\u500b\u5143\u7d20\u662f\u5426\u5b58\u5728\u65bc\u4e32\u5217\u4e2d\u3002\u5982\u679c\u5143\u7d20\u5b58\u5728\uff0c\u5247\u8fd4\u56de `True`\uff1b\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5247\u8fd4\u56de `False`\u3002\u9019\u662f\u5176\u57fa\u672c\u7528\u6cd5\uff1a<\/p>\n<p>&#8220;`python<br \/>\nlist1 = [1, 2, 3, 4, 5]\n<p># \u6aa2\u67e5\u5143\u7d203\u662f\u5426\u5b58\u5728\u65bclist1\u4e2d<br \/>\nprint(3 in list1)  # \u56de\u50b3 True<\/p>\n<p># \u6aa2\u67e5\u5143\u7d206\u662f\u5426\u5b58\u5728\u65bclist1\u4e2d<br \/>\nprint(6 in list1)  # \u56de\u50b3 False<br \/>\n&#8220;`<\/p>\n<h3>\u5be6\u4f5c\u7bc4\u4f8b<\/h3>\n<p>\u5047\u8a2d\u4f60\u6709\u4e00\u500b\u4e32\u5217\uff0c\u88e1\u9762\u5305\u542b\u4e00\u4e9b\u6c34\u679c\u540d\u7a31\uff0c\u4e26\u60f3\u6aa2\u67e5\u67d0\u500b\u6c34\u679c\u662f\u5426\u5728\u9019\u500b\u5217\u8868\u4e2d\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfruits = [&#8216;apple&#8217;, &#8216;banana&#8217;, &#8216;orange&#8217;, &#8216;grape&#8217;]\n<p># \u6aa2\u67e5 &#8216;banana&#8217; \u662f\u5426\u5b58\u5728\u65bc fruits \u4e2d<br \/>\nif &#8216;banana&#8217; in fruits:<br \/>\n    print(&#8220;\u9999\u8549\u5728\u5217\u8868\u4e2d\uff01&#8221;)<br \/>\nelse:<br \/>\n    print(&#8220;\u9999\u8549\u4e0d\u5728\u5217\u8868\u4e2d\u3002&#8221;)<br \/>\n&#8220;`<\/p>\n<h2>2. Python \u4e32\u5217 `not in` \u904b\u7b97\u5b50<\/h2>\n<p>`not in` \u904b\u7b97\u5b50\u5247\u662f\u7528\u4f86\u6aa2\u67e5\u67d0\u500b\u5143\u7d20\u662f\u5426\u4e0d\u5b58\u5728\u65bc\u4e32\u5217\u4e2d\u3002\u8fd4\u56de\u503c\u7684\u908f\u8f2f\u8207 `in` \u76f8\u53cd\uff1a\u82e5\u5143\u7d20\u4e0d\u5b58\u5728\u65bc\u4e32\u5217\u4e2d\uff0c\u5247\u8fd4\u56de `True`\uff1b\u82e5\u5b58\u5728\uff0c\u5247\u8fd4\u56de `False`\u3002<\/p>\n<p>&#8220;`python<br \/>\nlist1 = [1, 2, 3, 4, 5]\n<p># \u6aa2\u67e5\u5143\u7d206\u662f\u5426\u4e0d\u5b58\u5728\u65bclist1\u4e2d<br \/>\nprint(6 not in list1)  # \u56de\u50b3 True<\/p>\n<p># \u6aa2\u67e5\u5143\u7d203\u662f\u5426\u4e0d\u5b58\u5728\u65bclist1\u4e2d<br \/>\nprint(3 not in list1)  # \u56de\u50b3 False<br \/>\n&#8220;`<\/p>\n<h3>\u932f\u8aa4\u6392\u9664<\/h3>\n<p>\u5728\u4f7f\u7528\u9019\u4e9b\u904b\u7b97\u5b50\u6642\uff0c\u901a\u5e38\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u60c5\u6cc1\uff1a<br \/>\n&#8211; **\u5143\u7d20\u985e\u578b\u932f\u8aa4**\uff1a\u78ba\u4fdd\u6aa2\u67e5\u7684\u5143\u7d20\u8207\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u985e\u578b\u4e00\u81f4\uff0c\u5982\u6574\u6578\u8207\u5b57\u4e32\u4e0d\u80fd\u6df7\u7528\u3002<br \/>\n&#8211; **\u7a7a\u4e32\u5217**\uff1a\u5982\u679c\u4e32\u5217\u70ba\u7a7a\uff0c\u5247\u6240\u6709\u6aa2\u67e5\u90fd\u6703\u8fd4\u56de `False`\u3002<\/p>\n<h2>3. \u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9019\u4e9b\u904b\u7b97\u5b50\u4e0d\u50c5\u9069\u7528\u65bc\u6aa2\u67e5\u7c21\u55ae\u7684\u6578\u5b57\u6216\u5b57\u4e32\uff0c\u9084\u53ef\u4ee5\u7528\u65bc\u66f4\u8907\u96dc\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u5982\u7269\u4ef6\u548c\u5b57\u5178\u3002\u4f8b\u5982\uff0c\u7576\u4f60\u9700\u8981\u6aa2\u67e5\u67d0\u500b\u7269\u4ef6\u7684\u5c6c\u6027\u662f\u5426\u5728\u4e00\u500b\u7269\u4ef6\u4e32\u5217\u4e2d\u6642\uff0c\u9019\u4e9b\u904b\u7b97\u5b50\u540c\u6a23\u9069\u7528\u3002<\/p>\n<p>\u60f3\u4e86\u89e3\u66f4\u591a\u95dc\u65bc Python \u7684\u4f7f\u7528\u65b9\u5f0f\uff0c\u5efa\u8b70\u53c3\u8003 [\u9019\u7bc7\u6559\u5b78](https:\/\/vocus.cc\/article\/5e5e0d56fd89780001e1cba1) \u4f86\u64f4\u5c55\u4f60\u7684\u77e5\u8b58\u3002<\/p>\n<p>\u6700\u5f8c\uff0c\u7e3d\u7d50\u4f86\u8aaa\uff0cPython \u4e32\u5217\u7684 `in` \u548c `not in` \u904b\u7b97\u5b50\u662f\u6aa2\u67e5\u5143\u7d20\u5b58\u5728\u6027\u7684\u91cd\u8981\u5de5\u5177\uff0c\u53ef\u4ee5\u5e6b\u52a9\u4f60\u5728\u7a0b\u5f0f\u4e2d\u9032\u884c\u6709\u6548\u7684\u689d\u4ef6\u5224\u65b7\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: `in` \u548c `not in` \u904b\u7b97\u5b50\u7684\u6027\u80fd\u5982\u4f55\uff1f**<br \/>\nA1: \u5728\u5e8f\u5217\uff08\u5982\u4e32\u5217\u548c\u5143\u7d44\uff09\u4e2d\uff0c`in` \u548c `not in` \u904b\u7b97\u5b50\u7684\u6027\u80fd\u53d6\u6c7a\u65bc\u4e32\u5217\u7684\u5927\u5c0f\uff0c\u67e5\u8a62\u7684\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n)\u3002<\/p>\n<p>**Q2: \u53ef\u4ee5\u5728\u5b57\u5178\u4e2d\u4f7f\u7528 `in` \u55ce\uff1f**<br \/>\nA2: \u662f\u7684\uff0c`in` \u904b\u7b97\u5b50\u53ef\u4ee5\u7528\u4f86\u6aa2\u67e5\u4e00\u500b\u9375\u662f\u5426\u5b58\u5728\u65bc\u5b57\u5178\u4e2d\u3002\u4f8b\u5982\uff1a`key in my_dict`\u3002<\/p>\n<p>**Q3: \u5982\u679c\u6211\u9700\u8981\u6aa2\u67e5\u591a\u500b\u5143\u7d20\u662f\u5426\u5b58\u5728\u65bc\u4e32\u5217\u4e2d\uff0c\u6709\u4ec0\u9ebc\u597d\u7684\u65b9\u6cd5\u55ce\uff1f**<br \/>\nA3: \u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5c0e\u5f0f\u6216 `any()` \u51fd\u6578\u4f86\u6aa2\u67e5\u591a\u500b\u5143\u7d20\u7684\u5b58\u5728\u6027\u3002\u4f8b\u5982\uff1a<br \/>\n&#8220;`python<br \/>\nelements_to_check = [1, 6]\nexists = any(elem in list1 for elem in elements_to_check)<br \/>\n&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python \u4e2d\uff0c\u4e32\u5217\uff08List\uff09\u662f\u4e00\u7a2e\u975e\u5e38\u5e38&#8230;<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[],"class_list":["post-2642","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-GC","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2642","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=2642"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2642\/revisions"}],"predecessor-version":[{"id":2644,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2642\/revisions\/2644"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=2642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}