{"id":2654,"date":"2025-05-29T17:39:22","date_gmt":"2025-06-04T09:57:22","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2654"},"modified":"2025-06-04T17:39:22","modified_gmt":"2025-06-04T09:57:22","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%b2%e5%88%97%e4%b8%ad%e7%9a%84in%e5%92%8cnot-in%e9%81%8b%e7%ae%97%e5%ad%90-2","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%b2%e5%88%97%e4%b8%ad%e7%9a%84in%e5%92%8cnot-in%e9%81%8b%e7%ae%97%e5%ad%90-2\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e32\u5217\u4e2d\u7684 in \u548c not in \u904b\u7b97\u5b50\uff1a2025 \u6700\u65b0\u6559\u5b78\u8207\u5be6\u7528\u7bc4\u4f8b"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, \u4e32\u5217, in, not in, \u904b\u7b97\u5b50, \u6aa2\u67e5\u5143\u7d20, Python \u6559\u5b78\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u4e32\u5217(List) in \u548c not in \u904b\u7b97\u5b50\uff1a2025 \u6700\u65b0\u6559\u5b78\u8207\u5be6\u7528\u7bc4\u4f8b<\/h1>\n<p>Python \u7684\u4e32\u5217(List) \u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u5143\u7d20\u6aa2\u67e5\u529f\u80fd\uff0c\u5176\u4e2d `in` \u548c `not in` \u904b\u7b97\u5b50\u662f\u6700\u5e38\u7528\u7684\u5e03\u6797\u503c\uff08Boolean\uff09\u904b\u7b97\u5b50\u3002\u672c\u6587\u5c07\u6df1\u5165\u4ecb\u7d39\u9019\u5169\u500b\u904b\u7b97\u5b50\u7684\u7528\u6cd5\uff0c\u4e26\u63d0\u4f9b 2025 \u5e74\u6700\u65b0\u7684\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<p>## \u4f7f\u7528 in \u904b\u7b97\u5b50\u6aa2\u67e5\u5143\u7d20\u5b58\u5728\u6027<\/p>\n<p>`in` \u904b\u7b97\u5b50\u7528\u65bc\u6aa2\u67e5\u67d0\u500b\u5143\u7d20\u662f\u5426\u5b58\u5728\u65bc\u4e32\u5217\u4e2d\u3002\u5982\u679c\u5143\u7d20\u5b58\u5728\uff0c\u8fd4\u56de `True`\uff0c\u5426\u5247\u8fd4\u56de `False`\u3002<\/p>\n<p>&#8220;`python<br \/>\nlist1 = [1, 2, 3, 4, 5]\n<p># \u6aa2\u67e5 3 \u662f\u5426\u5b58\u5728\u65bc list1 \u4e2d<br \/>\nprint(3 in list1)  # \u56de\u50b3 True<\/p>\n<p># \u6aa2\u67e5 6 \u662f\u5426\u5b58\u5728\u65bc list1 \u4e2d<br \/>\nprint(6 in list1)  # \u56de\u50b3 False<br \/>\n&#8220;`<\/p>\n<p>### \u932f\u8aa4\u6392\u9664<br \/>\n\u5982\u679c\u4f60\u9047\u5230 `TypeError`\uff0c\u8acb\u78ba\u8a8d\u4f60\u6aa2\u67e5\u7684\u5143\u7d20\u985e\u578b\u8207\u4e32\u5217\u4e2d\u5143\u7d20\u7684\u985e\u578b\u76f8\u7b26\u3002<\/p>\n<p>## \u4f7f\u7528 not in \u904b\u7b97\u5b50\u6aa2\u67e5\u5143\u7d20\u4e0d\u5b58\u5728\u6027<\/p>\n<p>`not in` \u904b\u7b97\u5b50\u7528\u65bc\u6aa2\u67e5\u67d0\u500b\u5143\u7d20\u662f\u5426\u4e0d\u5b58\u5728\u65bc\u4e32\u5217\u4e2d\u3002\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u8fd4\u56de `True`\uff0c\u5426\u5247\u8fd4\u56de `False`\u3002<\/p>\n<p>&#8220;`python<br \/>\nlist1 = [1, 2, 3, 4, 5]\n<p># \u6aa2\u67e5 3 \u662f\u5426\u4e0d\u5b58\u5728\u65bc list1 \u4e2d<br \/>\nprint(3 not in list1)  # \u56de\u50b3 False<\/p>\n<p># \u6aa2\u67e5 6 \u662f\u5426\u4e0d\u5b58\u5728\u65bc list1 \u4e2d<br \/>\nprint(6 not in list1)  # \u56de\u50b3 True<br \/>\n&#8220;`<\/p>\n<p>### \u5ef6\u4f38\u61c9\u7528<br \/>\n\u9019\u4e9b\u904b\u7b97\u5b50\u4e0d\u50c5\u9650\u65bc\u6574\u6578\uff0c\u60a8\u9084\u53ef\u4ee5\u7528\u65bc\u6aa2\u67e5\u5b57\u4e32\u6216\u5176\u4ed6\u8cc7\u6599\u985e\u578b\u7684\u5b58\u5728\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfruits = [&#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;cherry&#8221;]\n<p># \u6aa2\u67e5 &#8216;banana&#8217; \u662f\u5426\u5b58\u5728\u65bc fruits \u4e2d<br \/>\nif &#8220;banana&#8221; in fruits:<br \/>\n    print(&#8220;\u9999\u8549\u5728\u5217\u8868\u4e2d&#8221;)<br \/>\nelse:<br \/>\n    print(&#8220;\u9999\u8549\u4e0d\u5728\u5217\u8868\u4e2d&#8221;)<br \/>\n&#8220;`<\/p>\n<p>## \u7d9c\u5408\u61c9\u7528\u793a\u7bc4<\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 `in` \u548c `not in` \u904b\u7b97\u5b50\u4f86\u6aa2\u67e5\u4f7f\u7528\u8005\u8f38\u5165\u7684\u503c\u662f\u5426\u5b58\u5728\u65bc\u4e32\u5217\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nlist1 = [1, 2, 3, 4, 5]\n<p># \u8b93\u4f7f\u7528\u8005\u8f38\u5165\u4e00\u500b\u6578\u5b57<br \/>\nnum = int(input(&#8220;\u8acb\u8f38\u5165\u4e00\u500b\u6578\u5b57\uff1a&#8221;))<\/p>\n<p># \u6aa2\u67e5\u4f7f\u7528\u8005\u8f38\u5165\u7684\u6578\u5b57\u662f\u5426\u5b58\u5728\u65bc list1 \u4e2d<br \/>\nif num in list1:<br \/>\n    print(&#8220;\u8f38\u5165\u7684\u6578\u5b57\u5b58\u5728\u65bc list1 \u4e2d&#8221;)<br \/>\nelse:<br \/>\n    print(&#8220;\u8f38\u5165\u7684\u6578\u5b57\u4e0d\u5b58\u5728\u65bc list1 \u4e2d&#8221;)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\u7684\u61c9\u7528\u4e0d\u50c5\u80fd\u589e\u5f37\u4f7f\u7528\u8005\u4e92\u52d5\u6027\uff0c\u9084\u80fd\u5e6b\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3 Python \u4e32\u5217\u7684\u64cd\u4f5c\u3002<\/p>\n<p>\u60f3\u4e86\u89e3\u66f4\u591a Python \u7a0b\u5f0f\u8a2d\u8a08\u7684\u5be6\u7528\u6280\u5de7\uff0c\u6b61\u8fce\u53c3\u8003 [\u9019\u88e1\u7684\u9032\u968e\u6559\u5b78](https:\/\/vocus.cc) \u6216 [\u9019\u88e1\u7684\u76f8\u95dc\u8cc7\u6e90](https:\/\/miner.tw)\u3002<\/p>\n<p>## \u5e38\u898b\u554f\u984c\u89e3\u7b54\uff08Q&#038;A\uff09<\/p>\n<p>**Q1: in \u548c not in \u904b\u7b97\u5b50\u53ef\u4ee5\u7528\u65bc\u5176\u4ed6\u8cc7\u6599\u7d50\u69cb\u55ce\uff1f**<br \/>\nA1: \u662f\u7684\uff0c`in` \u548c `not in` \u904b\u7b97\u5b50\u53ef\u4ee5\u7528\u65bc\u5b57\u4e32\u3001\u5143\u7d44\u548c\u5b57\u5178\u7b49\u8cc7\u6599\u7d50\u69cb\uff0c\u4f86\u6aa2\u67e5\u5143\u7d20\u7684\u5b58\u5728\u6027\u3002<\/p>\n<p>**Q2: \u4f7f\u7528 in \u548c not in \u6aa2\u67e5\u6027\u80fd\u5982\u4f55\uff1f**<br \/>\nA2: \u5728\u4e32\u5217\u4e2d\uff0c`in` \u548c `not in` \u7684\u6aa2\u67e5\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n)\uff0c\u9019\u610f\u5473\u8457\u5728\u6700\u58de\u60c5\u6cc1\u4e0b\u9700\u8981\u6aa2\u67e5\u6574\u500b\u4e32\u5217\u3002<\/p>\n<p>**Q3: \u5982\u4f55\u8655\u7406\u7a7a\u4e32\u5217\u7684\u60c5\u6cc1\uff1f**<br \/>\nA3: \u5982\u679c\u60a8\u6aa2\u67e5\u7684\u4e32\u5217\u662f\u7a7a\u7684\uff0c`in` \u548c `not in` \u6703\u76f4\u63a5\u8fd4\u56de `False` \u548c `True`\uff0c\u9019\u662f\u6b63\u5e38\u884c\u70ba\u3002<\/p>\n<p>\u5e0c\u671b\u9019\u7bc7\u6587\u7ae0\u80fd\u5e6b\u52a9\u4f60\u66f4\u6df1\u5165\u5730\u7406\u89e3 Python \u4e2d\u7684 `in` \u548c `not in` \u904b\u7b97\u5b50\uff01\u5982\u6709\u4efb\u4f55\u554f\u984c\uff0c\u8acb\u96a8\u6642\u7559\u8a00\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u4e32\u5217\u4e2d\u7684in\u548cnot in\u904b\u7b97\u5b50\uff0c\u53ef\u4ee5\u5feb\u901f\u6aa2\u67e5\u5143\u7d20\u662f\u5426\u5b58\u5728\u65bc\u4e32\u5217\u4e2d\uff0c\u63d0\u9ad8\u7a0b\u5f0f\u7684\u6548\u7387\u3002\u672c\u6587\u5c07\u4ecb\u7d39in\u548cnot in\u904b\u7b97\u5b50\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u5011\u4f86\u6aa2\u67e5\u5143\u7d20\u662f\u5426\u5b58\u5728\u65bc\u4e32\u5217\u4e2d\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","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":[15],"class_list":["post-2654","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-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-GO","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2654","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=2654"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2654\/revisions"}],"predecessor-version":[{"id":13050,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2654\/revisions\/13050"}],"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=2654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}