{"id":1018669,"date":"2024-12-27T12:44:57","date_gmt":"2024-12-27T04:44:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1018669.html"},"modified":"2024-12-27T12:45:01","modified_gmt":"2024-12-27T04:45:01","slug":"python%e5%a6%82%e4%bd%95%e7%94%a8in%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1018669.html","title":{"rendered":"python\u5982\u4f55\u7528in\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25161313\/27118e2c-9f25-464d-92e4-bad9d56865d1.webp\" alt=\"python\u5982\u4f55\u7528in\u51fd\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c<code>in<\/code>\u51fd\u6570\u4e3b\u8981\u7528\u4e8e\u68c0\u67e5\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u67d0\u4e2a\u5e8f\u5217\u4e2d\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u5143\u7ec4\u6216\u5b57\u5178\u7684\u952e\u4e2d\u3002<code>in<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff1a\u5982\u679c\u5143\u7d20\u5b58\u5728\u4e8e\u5e8f\u5217\u4e2d\uff0c\u5219\u8fd4\u56de<code>True<\/code>\uff0c\u5426\u5219\u8fd4\u56de<code>False<\/code>\u3002\u6700\u5e38\u7528\u7684\u5f62\u5f0f\u662f\u5728<code>if<\/code>\u8bed\u53e5\u4e2d\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\u3001\u5728\u5faa\u73af\u4e2d\u8fed\u4ee3\uff0c\u6216\u7528\u4e8e\u96c6\u5408\u64cd\u4f5c\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u68c0\u67e5\u67d0\u4e2a\u5b50\u5b57\u7b26\u4e32\u662f\u5426\u5b58\u5728\u4e8e\u5176\u4e2d\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32<code>text = &quot;Hello, world!&quot;<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>&quot;Hello&quot; in text<\/code>\u6765\u68c0\u67e5<code>text<\/code>\u4e2d\u662f\u5426\u5305\u542b\u5b50\u5b57\u7b26\u4e32&quot;Hello&quot;\u3002\u5982\u679c\u5305\u542b\uff0c\u8868\u8fbe\u5f0f\u5c06\u8fd4\u56de<code>True<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001<code>IN<\/code>\u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p><code>in<\/code>\u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u9002\u7528\u4e8e\u591a\u79cd\u6570\u636e\u7c7b\u578b\u3002\u6211\u4eec\u6765\u8be6\u7ec6\u770b\u770b\u5b83\u5728\u4e0d\u540c\u60c5\u5883\u4e2d\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b57\u7b26\u4e32\u4e2d\u7684\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u662f\u4e00\u4e2a\u5b57\u7b26\u5e8f\u5217\uff0c\u56e0\u6b64\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u6765\u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u6216\u5b50\u5b57\u7b26\u4e32\u662f\u5426\u5b58\u5728\u4e8e\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u3002\u8fd9\u4e2a\u529f\u80fd\u5bf9\u4e8e\u6587\u672c\u5206\u6790\u548c\u5904\u7406\u975e\u5e38\u6709\u7528\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;The quick brown fox jumps over the lazy dog&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u6765\u68c0\u67e5\u662f\u5426\u5b58\u5728\u67d0\u4e2a\u5355\u8bcd\u6216\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;quick&quot; in text)  # \u8f93\u51fa: True<\/p>\n<p>print(&quot;slow&quot; in text)   # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u7528\u6cd5\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u3001\u9a8c\u8bc1\u6570\u636e\u6216\u8fc7\u6ee4\u6587\u672c\u65f6\u975e\u5e38\u6709\u7528\u3002\u8003\u8651\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a\u5728\u5f00\u53d1\u4e00\u4e2a\u7b80\u5355\u7684\u6587\u672c\u8fc7\u6ee4\u5668\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u68c0\u67e5\u654f\u611f\u8bcd\u6c47\u662f\u5426\u51fa\u73b0\u5728\u7528\u6237\u8f93\u5165\u7684\u6587\u672c\u4e2d\u3002\u6b64\u65f6\uff0c<code>in<\/code>\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u68c0\u6d4b\u5e76\u91c7\u53d6\u76f8\u5e94\u63aa\u65bd\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5217\u8868\u3001\u5143\u7ec4\u4e2d\u7684\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u5728\u5217\u8868\u6216\u5143\u7ec4\u4e2d\uff0c<code>in<\/code>\u7528\u4e8e\u68c0\u67e5\u67d0\u4e2a\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u5176\u4e2d\u3002\u8fd9\u5bf9\u4e8e\u6570\u636e\u9a8c\u8bc1\u3001\u67e5\u91cd\u6216\u6761\u4ef6\u7b5b\u9009\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;, &quot;date&quot;]<\/p>\n<p>print(&quot;apple&quot; in fruits)  # \u8f93\u51fa: True<\/p>\n<p>print(&quot;orange&quot; in fruits) # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u7528\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u9009\u62e9\u3001\u5224\u65ad\u67d0\u4e2a\u9009\u9879\u662f\u5426\u53ef\u7528\u65f6\u5feb\u901f\u4f5c\u51fa\u51b3\u7b56\u3002\u5728\u5f00\u53d1\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u901a\u8fc7\u7528\u6237\u9009\u62e9\u7684\u83dc\u5355\u9879\u6765\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\uff0c\u901a\u8fc7<code>in<\/code>\u51fd\u6570\u53ef\u4ee5\u5feb\u901f\u9a8c\u8bc1\u7528\u6237\u8f93\u5165\u7684\u6709\u6548\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001<code>IN<\/code>\u51fd\u6570\u5728\u5b57\u5178\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u4e00\u79cd\u952e\u503c\u5bf9\u96c6\u5408\uff0c\u5728\u5b57\u5178\u4e2d\uff0c<code>in<\/code>\u51fd\u6570\u7528\u4e8e\u68c0\u67e5\u67d0\u4e2a\u952e\u662f\u5426\u5b58\u5728\uff0c\u800c\u4e0d\u662f\u503c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u68c0\u67e5\u5b57\u5178\u952e<\/h4>\n<\/p>\n<p><p>\u5728\u5b57\u5178\u4e2d\uff0c<code>in<\/code>\u51fd\u6570\u4e3b\u8981\u7528\u4e8e\u68c0\u67e5\u67d0\u4e2a\u952e\u662f\u5426\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d\u3002\u8fd9\u5728\u67e5\u627e\u548c\u66f4\u65b0\u64cd\u4f5c\u4e2d\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">student_scores = {&quot;Alice&quot;: 85, &quot;Bob&quot;: 92, &quot;Charlie&quot;: 78}<\/p>\n<p>print(&quot;Alice&quot; in student_scores)  # \u8f93\u51fa: True<\/p>\n<p>print(&quot;David&quot; in student_scores)  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u7528\u6cd5\u5728\u5904\u7406\u914d\u7f6e\u6587\u4ef6\u3001\u89e3\u6790JSON\u6570\u636e\u6216\u5728API\u54cd\u5e94\u4e2d\u63d0\u53d6\u4fe1\u606f\u65f6\u975e\u5e38\u6709\u7528\u3002\u901a\u8fc7\u9a8c\u8bc1\u952e\u7684\u5b58\u5728\u6027\uff0c\u53ef\u4ee5\u907f\u514d\u5728\u8bbf\u95ee\u5b57\u5178\u65f6\u5f15\u53d1<code>KeyError<\/code>\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7ed3\u5408<code>get<\/code>\u65b9\u6cd5\u7684\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u5728\u5b57\u5178\u4e2d\uff0c\u7ed3\u5408\u4f7f\u7528<code>in<\/code>\u548c<code>get<\/code>\u65b9\u6cd5\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u5904\u7406\u952e\u7684\u5b58\u5728\u6027\u68c0\u67e5\u3002<code>get<\/code>\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u63d0\u4f9b\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff0c\u5f53\u952e\u4e0d\u5b58\u5728\u65f6\u8fd4\u56de\u8be5\u9ed8\u8ba4\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">score = student_scores.get(&quot;David&quot;, &quot;Not Found&quot;)<\/p>\n<p>print(score)  # \u8f93\u51fa: Not Found<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u907f\u514d\u4e86\u76f4\u63a5\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u65f6\u5f15\u53d1\u7684\u9519\u8bef\uff0c\u540c\u65f6\u63d0\u4f9b\u4e86\u4e00\u79cd\u4f18\u96c5\u7684\u65b9\u5f0f\u6765\u5904\u7406\u7f3a\u5931\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001<code>IN<\/code>\u51fd\u6570\u5728\u96c6\u5408\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u662fPython\u4e2d\u7528\u4e8e\u5b58\u50a8\u4e0d\u91cd\u590d\u5143\u7d20\u7684\u5bb9\u5668\u3002<code>in<\/code>\u51fd\u6570\u5728\u96c6\u5408\u4e2d\u7528\u4e8e\u68c0\u67e5\u5143\u7d20\u7684\u5b58\u5728\u6027\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u96c6\u5408\u7684\u57fa\u672c\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u96c6\u5408\u7684\u4e3b\u8981\u7279\u70b9\u4e4b\u4e00\u662f\u5feb\u901f\u67e5\u627e\u3002\u7531\u4e8e\u96c6\u5408\u7684\u5e95\u5c42\u5b9e\u73b0\u662f\u57fa\u4e8e\u54c8\u5e0c\u8868\u7684\uff0c<code>in<\/code>\u64cd\u4f5c\u5728\u96c6\u5408\u4e2d\u975e\u5e38\u9ad8\u6548\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">prime_numbers = {2, 3, 5, 7, 11, 13}<\/p>\n<p>print(5 in prime_numbers)  # \u8f93\u51fa: True<\/p>\n<p>print(9 in prime_numbers)  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u67e5\u627e\u6548\u7387\u5728\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u5c24\u5176\u91cd\u8981\u3002\u901a\u8fc7\u96c6\u5408\uff0c\u53ef\u4ee5\u5feb\u901f\u5b9e\u73b0\u8fc7\u6ee4\u3001\u53bb\u91cd\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u96c6\u5408\u7684\u5176\u4ed6\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u67e5\u627e\uff0c\u96c6\u5408\u8fd8\u652f\u6301\u4e00\u4e9b\u5176\u4ed6\u64cd\u4f5c\uff0c\u6bd4\u5982\u5e76\u96c6\u3001\u4ea4\u96c6\u548c\u5dee\u96c6\u3002\u5728\u8fd9\u4e9b\u64cd\u4f5c\u4e2d\uff0c<code>in<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u9a8c\u8bc1\u7ed3\u679c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">even_numbers = {2, 4, 6, 8, 10}<\/p>\n<p>odd_numbers = {1, 3, 5, 7, 9}<\/p>\n<h2><strong>\u6c42\u4ea4\u96c6<\/strong><\/h2>\n<p>common_numbers = prime_numbers &amp; odd_numbers<\/p>\n<p>print(3 in common_numbers)  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528\u96c6\u5408\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001<code>IN<\/code>\u51fd\u6570\u5728\u5faa\u73af\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p><code>in<\/code>\u51fd\u6570\u5e38\u7528\u4e8e\u5faa\u73af\u4e2d\uff0c\u7528\u4e8e\u904d\u5386\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u3002\u7ed3\u5408<code>for<\/code>\u5faa\u73af\uff0c<code>in<\/code>\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8f7b\u677e\u5730\u904d\u5386\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u5178\u548c\u96c6\u5408\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u904d\u5386\u5217\u8868\u548c\u5143\u7ec4<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7<code>for<\/code>\u5faa\u73af\uff0c\u53ef\u4ee5\u9010\u4e2a\u8bbf\u95ee\u5217\u8868\u6216\u5143\u7ec4\u4e2d\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">colors = [&quot;red&quot;, &quot;green&quot;, &quot;blue&quot;]<\/p>\n<p>for color in colors:<\/p>\n<p>    print(color)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u904d\u5386\u65b9\u5f0f\u5728\u9700\u8981\u9010\u4e00\u5904\u7406\u6216\u4fee\u6539\u5217\u8868\u5143\u7d20\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5728\u5b9e\u73b0\u4e00\u4e2a\u989c\u8272\u8f6c\u6362\u5668\u65f6\uff0c\u53ef\u4ee5\u9010\u4e2a\u8bbf\u95ee\u989c\u8272\u5217\u8868\u5e76\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u904d\u5386\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u5728\u5b57\u5178\u4e2d\uff0c<code>in<\/code>\u51fd\u6570\u7528\u4e8e\u904d\u5386\u952e\u3002\u53ef\u4ee5\u901a\u8fc7<code>items()<\/code>\u65b9\u6cd5\u540c\u65f6\u8bbf\u95ee\u952e\u548c\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for name, score in student_scores.items():<\/p>\n<p>    print(f&quot;{name}: {score}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u904d\u5386\u65b9\u5f0f\u5728\u9700\u8981\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\u3001\u751f\u6210\u62a5\u8868\u6216\u683c\u5f0f\u5316\u8f93\u51fa\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001<code>IN<\/code>\u51fd\u6570\u5728\u6761\u4ef6\u5224\u65ad\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p><code>in<\/code>\u51fd\u6570\u5e38\u7528\u4e8e\u6761\u4ef6\u5224\u65ad\u4e2d\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u6839\u636e\u5143\u7d20\u7684\u5b58\u5728\u6027\u4f5c\u51fa\u51b3\u7b56\u3002\u8fd9\u5728\u7528\u6237\u8f93\u5165\u9a8c\u8bc1\u3001\u7a0b\u5e8f\u903b\u8f91\u63a7\u5236\u7b49\u573a\u666f\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u7528\u6237\u8f93\u5165\u9a8c\u8bc1<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\uff0c\u5e38\u5e38\u9700\u8981\u9a8c\u8bc1\u8f93\u5165\u7684\u6709\u6548\u6027\u3002\u901a\u8fc7<code>in<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5feb\u901f\u68c0\u67e5\u8f93\u5165\u662f\u5426\u5728\u5141\u8bb8\u7684\u9009\u9879\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">valid_options = [&quot;yes&quot;, &quot;no&quot;, &quot;maybe&quot;]<\/p>\n<p>user_input = input(&quot;Enter your choice: &quot;).lower()<\/p>\n<p>if user_input in valid_options:<\/p>\n<p>    print(&quot;Valid choice!&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Invalid choice.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u9a8c\u8bc1\u65b9\u5f0f\u7b80\u5355\u9ad8\u6548\uff0c\u80fd\u591f\u6709\u6548\u5730\u9632\u6b62\u9519\u8bef\u8f93\u5165\u5bfc\u81f4\u7684\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7a0b\u5e8f\u903b\u8f91\u63a7\u5236<\/h4>\n<\/p>\n<p><p>\u5728\u7a0b\u5e8f\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u6839\u636e\u67d0\u4e9b\u6761\u4ef6\u6267\u884c\u7279\u5b9a\u7684\u4ee3\u7801\u5757\u3002\u901a\u8fc7<code>in<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u903b\u8f91\u63a7\u5236\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">status = &quot;completed&quot;<\/p>\n<p>if status in [&quot;completed&quot;, &quot;finished&quot;, &quot;done&quot;]:<\/p>\n<p>    print(&quot;Task is complete.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Task is not complete.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u903b\u8f91\u63a7\u5236\u65b9\u5f0f\u4f7f\u5f97\u4ee3\u7801\u6e05\u6670\u6613\u8bfb\uff0c\u540c\u65f6\u4e5f\u63d0\u9ad8\u4e86\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001<code>IN<\/code>\u51fd\u6570\u7684\u6027\u80fd\u548c\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u867d\u7136<code>in<\/code>\u51fd\u6570\u975e\u5e38\u65b9\u4fbf\uff0c\u4f46\u5728\u4f7f\u7528\u65f6\u4ecd\u9700\u6ce8\u610f\u6027\u80fd\u548c\u4e00\u4e9b\u7ec6\u8282\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6027\u80fd\u8003\u8651<\/h4>\n<\/p>\n<p><p><code>in<\/code>\u64cd\u4f5c\u7684\u6027\u80fd\u53d6\u51b3\u4e8e\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002\u5bf9\u4e8e\u5217\u8868\u548c\u5143\u7ec4\uff0c<code>in<\/code>\u64cd\u4f5c\u662f\u7ebf\u6027\u7684\uff08O(n)\uff09\uff0c\u56e0\u4e3a\u9700\u8981\u9010\u4e2a\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u3002\u4f46\u5bf9\u4e8e\u96c6\u5408\u548c\u5b57\u5178\uff0c<code>in<\/code>\u64cd\u4f5c\u662f\u5e38\u6570\u65f6\u95f4\uff08O(1)\uff09\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u57fa\u4e8e\u54c8\u5e0c\u8868\u5b9e\u73b0\u7684\u3002\u56e0\u6b64\uff0c\u5728\u9700\u8981\u9891\u7e41\u67e5\u627e\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>in<\/code>\u51fd\u6570\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u6bd4\u8f83\u5bf9\u8c61\u7684\u7c7b\u578b\u548c\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u5728\u5b57\u7b26\u4e32\u4e2d\u8fdb\u884c<code>in<\/code>\u64cd\u4f5c\u65f6\uff0c\u4f1a\u533a\u5206\u5927\u5c0f\u5199\u3002\u56e0\u6b64\uff0c\u53ef\u80fd\u9700\u8981\u5728\u6bd4\u8f83\u524d\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u6807\u51c6\u5316\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u5728\u4f7f\u7528<code>in<\/code>\u8fdb\u884c\u5b57\u5178\u952e\u68c0\u67e5\u65f6\uff0c\u9700\u8981\u660e\u786e\u53ea\u662f\u5728\u68c0\u67e5\u952e\u7684\u5b58\u5728\u6027\uff0c\u800c\u4e0d\u662f\u503c\u3002\u56e0\u6b64\uff0c\u5728\u9700\u8981\u68c0\u67e5\u5b57\u5178\u503c\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\u800c\u8a00\uff0c<code>in<\/code>\u51fd\u6570\u662fPython\u4e2d\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u591a\u79cd\u6570\u636e\u7c7b\u578b\u548c\u573a\u666f\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528<code>in<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u7ed3\u5408\u5177\u4f53\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u548c\u65b9\u6cd5\uff0c\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u4e2d\u7684in\u51fd\u6570\u6709\u4ec0\u4e48\u7528\u5904\uff1f<\/strong><br \/>in\u51fd\u6570\u662fPython\u4e2d\u7528\u4e8e\u68c0\u67e5\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u4e00\u4e2a\u5bb9\u5668\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u6216\u5b57\u5178\uff09\u7684\u5173\u952e\u5b57\u3002\u901a\u8fc7\u4f7f\u7528in\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5224\u65ad\u67d0\u4e2a\u503c\u662f\u5426\u5728\u6307\u5b9a\u7684\u96c6\u5408\u4e2d\uff0c\u8fd9\u5728\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\u548c\u6570\u636e\u9a8c\u8bc1\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528in\u6765\u5224\u65ad\u4e00\u4e2a\u5b57\u6bcd\u662f\u5426\u5728\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\uff0c\u6216\u8005\u4e00\u4e2a\u6570\u503c\u662f\u5426\u5728\u4e00\u4e2a\u6570\u5b57\u5217\u8868\u4e2d\u3002<\/p>\n<p><strong>\u4f7f\u7528in\u51fd\u6570\u65f6\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u9700\u8981\u907f\u514d\uff1f<\/strong><br \/>\u5728\u4f7f\u7528in\u51fd\u6570\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u5728\u4e0d\u652f\u6301in\u64cd\u4f5c\u7684\u7c7b\u578b\u4e0a\u4f7f\u7528\u5b83\uff0c\u4f8b\u5982\u5c1d\u8bd5\u5728\u6574\u6570\u6216\u6d6e\u70b9\u6570\u4e0a\u4f7f\u7528in\u3002\u6b64\u5916\uff0c\u6ce8\u610f\u533a\u5206\u5927\u5c0f\u5199\uff0c\u56e0\u4e3ain\u51fd\u6570\u5728\u5b57\u7b26\u4e32\u6bd4\u8f83\u65f6\u662f\u533a\u5206\u5927\u5c0f\u5199\u7684\u3002\u6b64\u5916\uff0c\u5f53\u4f7f\u7528in\u68c0\u67e5\u5b57\u5178\u65f6\uff0c\u9ed8\u8ba4\u662f\u68c0\u67e5\u5b57\u5178\u7684\u952e\u800c\u975e\u503c\uff0c\u56e0\u6b64\u9700\u8981\u660e\u786e\u4f60\u8981\u68c0\u67e5\u7684\u5bf9\u8c61\u7c7b\u578b\u3002<\/p>\n<p><strong>in\u51fd\u6570\u7684\u6027\u80fd\u5982\u4f55\uff1f<\/strong><br \/>in\u51fd\u6570\u7684\u6027\u80fd\u4e0e\u5bb9\u5668\u7c7b\u578b\u6709\u5173\u3002\u5bf9\u4e8e\u5217\u8868\u548c\u5143\u7ec4\uff0cin\u51fd\u6570\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662fO(n)\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u904d\u5386\u6574\u4e2a\u5bb9\u5668\u6765\u67e5\u627e\u5143\u7d20\u3002\u5bf9\u4e8e\u96c6\u5408\u548c\u5b57\u5178\uff0cin\u51fd\u6570\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662fO(1)\uff0c\u56e0\u4e3a\u5b83\u4eec\u57fa\u4e8e\u54c8\u5e0c\u8868\uff0c\u56e0\u6b64\u67e5\u627e\u901f\u5ea6\u66f4\u5feb\u3002\u56e0\u6b64\uff0c\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u4f7f\u7528in\u51fd\u6570\u7684\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0cin\u51fd\u6570\u4e3b\u8981\u7528\u4e8e\u68c0\u67e5\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u67d0\u4e2a\u5e8f\u5217\u4e2d\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u5143\u7ec4\u6216\u5b57\u5178\u7684\u952e\u4e2d\u3002in\u51fd\u6570 [&hellip;]","protected":false},"author":3,"featured_media":1018677,"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\/1018669"}],"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=1018669"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1018669\/revisions"}],"predecessor-version":[{"id":1018680,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1018669\/revisions\/1018680"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1018677"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1018669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1018669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1018669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}