{"id":981020,"date":"2024-12-27T06:59:05","date_gmt":"2024-12-26T22:59:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/981020.html"},"modified":"2024-12-27T06:59:07","modified_gmt":"2024-12-26T22:59:07","slug":"python%e7%9a%84contain%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/981020.html","title":{"rendered":"python\u7684contain\u5982\u4f55\u4f7f\u7528"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210145\/bc4e4f93-912b-4a78-8af0-ace1c3190461.webp\" alt=\"python\u7684contain\u5982\u4f55\u4f7f\u7528\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d\uff1a<br \/>\u5728Python\u4e2d\uff0c\u5224\u65ad\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u67d0\u4e2a\u5e8f\u5217\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5173\u952e\u5b57<code>in<\/code>\uff0c\u8fd9\u4e00\u64cd\u4f5c\u5e38\u88ab\u79f0\u4e3a\u201ccont<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n\u201d\u64cd\u4f5c\u3002<strong><code>in<\/code>\u5173\u952e\u5b57\u3001<code>__contains__<\/code>\u65b9\u6cd5\u3001<code>any()<\/code>\u548c<code>all()<\/code>\u51fd\u6570<\/strong>\u662f\u5b9e\u73b0\u201ccontain\u201d\u64cd\u4f5c\u7684\u51e0\u79cd\u5e38\u89c1\u65b9\u5f0f\u3002<code>in<\/code>\u5173\u952e\u5b57\u662f\u6700\u76f4\u63a5\u548c\u7b80\u4fbf\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u5143\u7ec4\u3001\u96c6\u5408\u548c\u5b57\u5178\u7b49\u6570\u636e\u7c7b\u578b\u3002\u5176\u57fa\u672c\u7528\u6cd5\u662f\uff1a<code>element in sequence<\/code>\uff0c\u8fd4\u56de\u503c\u662f\u5e03\u5c14\u7c7b\u578b\uff0c\u8868\u793a\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u5e8f\u5217\u4e2d\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5feb\u901f\u5224\u65ad\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u67d0\u4e2a\u5e8f\u5217\u4e2d\uff0c\u800c\u65e0\u9700\u904d\u5386\u6574\u4e2a\u5e8f\u5217\u3002  <\/p>\n<\/p>\n<p><p>\u4e00\u3001<code>IN<\/code>\u5173\u952e\u5b57\u7684\u4f7f\u7528  <\/p>\n<\/p>\n<p><p><code>in<\/code>\u5173\u952e\u5b57\u662fPython\u4e2d\u7528\u4e8e\u68c0\u67e5\u67d0\u4e2a\u5143\u7d20\u662f\u5426\u5728\u67d0\u4e2a\u5e8f\u5217\u4e2d\u7684\u6700\u5e38\u89c1\u65b9\u6cd5\u3002\u5b83\u7684\u4f7f\u7528\u65b9\u5f0f\u975e\u5e38\u7b80\u5355\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u5143\u7ec4\u3001\u96c6\u5408\u548c\u5b57\u5178\u3002  <\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b57\u7b26\u4e32\u4e2d\u7684\u4f7f\u7528<\/strong><br \/>\u5728\u5b57\u7b26\u4e32\u4e2d\uff0c<code>in<\/code>\u5173\u952e\u5b57\u7528\u4e8e\u68c0\u67e5\u4e00\u4e2a\u5b50\u5b57\u7b26\u4e32\u662f\u5426\u5b58\u5728\u4e8e\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u3002\u4f8b\u5982\uff1a  <\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, World!&quot;<\/p>\n<p>result = &quot;World&quot; in text<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aTrue<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>&quot;World&quot; in text<\/code>\u8fd4\u56de<code>True<\/code>\uff0c\u56e0\u4e3a\u5b57\u7b26\u4e32<code>&quot;World&quot;<\/code>\u786e\u5b9e\u5728<code>text<\/code>\u4e2d\u5b58\u5728\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5217\u8868\u4e2d\u7684\u4f7f\u7528<\/strong><br \/>\u5728\u5217\u8868\u4e2d\uff0c<code>in<\/code>\u5173\u952e\u5b57\u7528\u4e8e\u68c0\u67e5\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\u3002\u4f8b\u5982\uff1a  <\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>result = 3 in numbers<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aTrue<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>3 in numbers<\/code>\u8fd4\u56de<code>True<\/code>\uff0c\u56e0\u4e3a\u6570\u5b57<code>3<\/code>\u5728\u5217\u8868<code>numbers<\/code>\u4e2d\u5b58\u5728\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001<code>__CONTAINS__<\/code>\u65b9\u6cd5\u7684\u4f7f\u7528  <\/p>\n<\/p>\n<p><p><code>__contains__<\/code>\u662fPython\u4e2d\u7684\u4e00\u4e2a\u7279\u6b8a\u65b9\u6cd5\uff0c\u7528\u4e8e\u5b9e\u73b0<code>in<\/code>\u5173\u952e\u5b57\u7684\u529f\u80fd\u3002\u5927\u591a\u6570\u5185\u7f6e\u5bb9\u5668\u7c7b\u578b\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u96c6\u5408\u7b49\uff09\u90fd\u5b9e\u73b0\u4e86<code>__contains__<\/code>\u65b9\u6cd5\u3002  <\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u81ea\u5b9a\u4e49\u7c7b\u4e2d\u5b9e\u73b0<code>__contains__<\/code><\/strong><br \/>\u5982\u679c\u4f60\u5b9a\u4e49\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7c7b\uff0c\u5e76\u5e0c\u671b\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u6765\u68c0\u67e5\u8be5\u7c7b\u7684\u5b9e\u4f8b\u662f\u5426\u5305\u542b\u67d0\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0<code>__contains__<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a  <\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyCollection:<\/p>\n<p>    def __init__(self, items):<\/p>\n<p>        self.items = items<\/p>\n<p>    def __contains__(self, item):<\/p>\n<p>        return item in self.items<\/p>\n<p>collection = MyCollection([1, 2, 3, 4, 5])<\/p>\n<p>result = 3 in collection<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aTrue<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>MyCollection<\/code>\u7684\u7c7b\uff0c\u5e76\u5728\u5176\u4e2d\u5b9e\u73b0\u4e86<code>__contains__<\/code>\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u6765\u68c0\u67e5\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u96c6\u5408\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7406\u89e3<code>__contains__<\/code>\u7684\u6027\u80fd<\/strong><br \/><code>__contains__<\/code>\u65b9\u6cd5\u7684\u6027\u80fd\u53d6\u51b3\u4e8e\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5217\u8868\u548c\u5143\u7ec4\uff0c<code>__contains__<\/code>\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662fO(n)\uff0c\u56e0\u4e3a\u9700\u8981\u904d\u5386\u6574\u4e2a\u5e8f\u5217\u3002\u800c\u5bf9\u4e8e\u96c6\u5408\u548c\u5b57\u5178\uff0c<code>__contains__<\/code>\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662fO(1)\uff0c\u56e0\u4e3a\u5b83\u4eec\u4f7f\u7528\u4e86\u54c8\u5e0c\u8868\u7ed3\u6784\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001<code>ANY()<\/code>\u548c<code>ALL()<\/code>\u51fd\u6570\u7684\u4f7f\u7528  <\/p>\n<\/p>\n<p><p><code>any()<\/code>\u548c<code>all()<\/code>\u662fPython\u4e2d\u4e24\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u6d4b\u8bd5\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u5e03\u5c14\u503c\u3002\u5b83\u4eec\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u201ccontain\u201d\u64cd\u4f5c\u7684\u67d0\u4e9b\u53d8\u4f53\u3002  <\/p>\n<\/p>\n<ol>\n<li>\n<p><strong><code>any()<\/code>\u51fd\u6570\u7684\u4f7f\u7528<\/strong><br \/><code>any()<\/code>\u51fd\u6570\u7528\u4e8e\u68c0\u67e5\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u662f\u5426\u81f3\u5c11\u6709\u4e00\u4e2a\u5143\u7d20\u4e3a<code>True<\/code>\u3002\u5982\u679c\u662f\uff0c\u5219\u8fd4\u56de<code>True<\/code>\uff1b\u5426\u5219\u8fd4\u56de<code>False<\/code>\u3002\u4f8b\u5982\uff1a  <\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [0, 0, 3, 0, 0]<\/p>\n<p>result = any(numbers)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aTrue<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>any(numbers)<\/code>\u8fd4\u56de<code>True<\/code>\uff0c\u56e0\u4e3a\u5217\u8868<code>numbers<\/code>\u4e2d\u81f3\u5c11\u6709\u4e00\u4e2a\u975e\u96f6\u5143\u7d20\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong><code>all()<\/code>\u51fd\u6570\u7684\u4f7f\u7528<\/strong><br \/><code>all()<\/code>\u51fd\u6570\u7528\u4e8e\u68c0\u67e5\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u6240\u6709\u5143\u7d20\u662f\u5426\u90fd\u4e3a<code>True<\/code>\u3002\u5982\u679c\u662f\uff0c\u5219\u8fd4\u56de<code>True<\/code>\uff1b\u5426\u5219\u8fd4\u56de<code>False<\/code>\u3002\u4f8b\u5982\uff1a  <\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>result = all(numbers)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aTrue<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>all(numbers)<\/code>\u8fd4\u56de<code>True<\/code>\uff0c\u56e0\u4e3a\u5217\u8868<code>numbers<\/code>\u4e2d\u7684\u6240\u6709\u5143\u7d20\u90fd\u662f\u975e\u96f6\u7684\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5728\u5b57\u5178\u4e2d\u7684\u201cCONTAIN\u201d\u64cd\u4f5c  <\/p>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u5bb9\u5668\u7c7b\u578b\uff0c<code>in<\/code>\u5173\u952e\u5b57\u5728\u5b57\u5178\u4e2d\u6709\u5176\u7279\u5b9a\u7684\u7528\u6cd5\u3002  <\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u68c0\u67e5\u952e\u662f\u5426\u5b58\u5728<\/strong><br \/>\u5728\u5b57\u5178\u4e2d\uff0c<code>in<\/code>\u5173\u952e\u5b57\u7528\u4e8e\u68c0\u67e5\u67d0\u4e2a\u952e\u662f\u5426\u5b58\u5728\u3002\u4f8b\u5982\uff1a  <\/p>\n<\/p>\n<p><pre><code class=\"language-python\">student_scores = {&quot;Alice&quot;: 85, &quot;Bob&quot;: 90, &quot;Charlie&quot;: 78}<\/p>\n<p>result = &quot;Alice&quot; in student_scores<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aTrue<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>&quot;Alice&quot; in student_scores<\/code>\u8fd4\u56de<code>True<\/code>\uff0c\u56e0\u4e3a\u5b57\u5178\u4e2d\u5b58\u5728\u952e<code>&quot;Alice&quot;<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u68c0\u67e5\u503c\u662f\u5426\u5b58\u5728<\/strong><br \/>\u867d\u7136<code>in<\/code>\u5173\u952e\u5b57\u4e0d\u80fd\u76f4\u63a5\u7528\u4e8e\u68c0\u67e5\u5b57\u5178\u4e2d\u7684\u503c\uff0c\u4f46\u53ef\u4ee5\u7ed3\u5408\u5b57\u5178\u7684<code>values()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a  <\/p>\n<\/p>\n<p><pre><code class=\"language-python\">student_scores = {&quot;Alice&quot;: 85, &quot;Bob&quot;: 90, &quot;Charlie&quot;: 78}<\/p>\n<p>result = 90 in student_scores.values()<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aTrue<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>90 in student_scores.values()<\/code>\u8fd4\u56de<code>True<\/code>\uff0c\u56e0\u4e3a\u5b57\u5178\u4e2d\u5b58\u5728\u503c<code>90<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8fdb\u884c\u590d\u6742\u7684\u201cCONTAIN\u201d\u64cd\u4f5c  <\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u800c\u5f3a\u5927\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u521b\u5efa\u65b0\u5217\u8868\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u590d\u6742\u7684\u201ccontain\u201d\u64cd\u4f5c\u3002  <\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5728\u5217\u8868\u4e2d\u67e5\u627e\u591a\u4e2a\u5143\u7d20<\/strong><br \/>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5217\u8868\uff0c\u60f3\u8981\u68c0\u67e5\u5176\u4e2d\u662f\u5426\u5305\u542b\u591a\u4e2a\u7279\u5b9a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u4f8b\u5982\uff1a  <\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>targets = [2, 3]<\/p>\n<p>result = all(target in numbers for target in targets)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aTrue<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>all(target in numbers for target in targets)<\/code>\u8fd4\u56de<code>True<\/code>\uff0c\u56e0\u4e3a\u5217\u8868<code>numbers<\/code>\u4e2d\u5305\u542b\u6240\u6709\u76ee\u6807\u5143\u7d20\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5728\u5d4c\u5957\u5217\u8868\u4e2d\u67e5\u627e\u5143\u7d20<\/strong><br \/>\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u5d4c\u5957\u5217\u8868\uff0c\u60f3\u8981\u68c0\u67e5\u5176\u4e2d\u662f\u5426\u5305\u542b\u67d0\u4e2a\u5143\u7d20\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u4f8b\u5982\uff1a  <\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_list = [[1, 2], [3, 4], [5, 6]]<\/p>\n<p>target = 4<\/p>\n<p>result = any(target in sublist for sublist in nested_list)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1aTrue<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>any(target in sublist for sublist in nested_list)<\/code>\u8fd4\u56de<code>True<\/code>\uff0c\u56e0\u4e3a\u5d4c\u5957\u5217\u8868\u4e2d\u786e\u5b9e\u5305\u542b\u76ee\u6807\u5143\u7d20\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230Python\u4e2d\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u201ccontain\u201d\u64cd\u4f5c\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u7279\u5b9a\u7684\u4f7f\u7528\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002\u5728\u7f16\u5199Python\u4ee3\u7801\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u5c06\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u68c0\u67e5\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c<code>in<\/code>\u5173\u952e\u5b57\u53ef\u4ee5\u7528\u6765\u68c0\u67e5\u67d0\u4e2a\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u7b49\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u68c0\u67e5\u6570\u5b575\u662f\u5426\u5728\u5217\u8868\u4e2d\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]\nif 5 in my_list:\n    print(&quot;5\u5728\u5217\u8868\u4e2d\u3002&quot;)\nelse:\n    print(&quot;5\u4e0d\u5728\u5217\u8868\u4e2d\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u6570\u636e\u7c7b\u578b\u7684\u68c0\u67e5\u3002<\/p>\n<p><strong>\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u65f6\uff0c\u5982\u4f55\u5904\u7406\u5927\u5c0f\u5199\u654f\u611f\u7684\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u68c0\u67e5\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b\u67d0\u4e2a\u5b50\u4e32\u65f6\uff0c\u5927\u5c0f\u5199\u53ef\u80fd\u4f1a\u5f71\u54cd\u7ed3\u679c\u3002\u4e3a\u4e86\u5ffd\u7565\u5927\u5c0f\u5199\uff0c\u53ef\u4ee5\u5148\u5c06\u4e24\u4e2a\u5b57\u7b26\u4e32\u90fd\u8f6c\u6362\u4e3a\u5c0f\u5199\u6216\u5927\u5199\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_string = &quot;Hello World&quot;\nif &quot;hello&quot; in my_string.lower():\n    print(&quot;\u5b50\u4e32\u5b58\u5728\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u786e\u4fdd\u4e86\u68c0\u67e5\u8fc7\u7a0b\u4e0d\u53d7\u5927\u5c0f\u5199\u7684\u5f71\u54cd\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u5339\u914d\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c<code>in<\/code>\u5173\u952e\u5b57\u662f\u5426\u9002\u7528\u4e8e\u5b57\u5178\uff1f\u5982\u4f55\u4f7f\u7528\uff1f<\/strong><br \/>\u786e\u5b9e\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u5728\u5b57\u5178\u4e2d\u68c0\u67e5\u67d0\u4e2a\u952e\u662f\u5426\u5b58\u5728\u3002\u901a\u8fc7\u952e\u540d\u8fdb\u884c\u68c0\u67e5\u65f6\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u5e03\u5c14\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}\nif &#39;b&#39; in my_dict:\n    print(&quot;&#39;b&#39;\u662f\u5b57\u5178\u7684\u4e00\u4e2a\u952e\u3002&quot;)\nelse:\n    print(&quot;&#39;b&#39;\u4e0d\u662f\u5b57\u5178\u7684\u4e00\u4e2a\u952e\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u5f97\u5728\u5b57\u5178\u4e2d\u67e5\u627e\u952e\u53d8\u5f97\u975e\u5e38\u65b9\u4fbf\u4e14\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d\uff1a\u5728Python\u4e2d\uff0c\u5224\u65ad\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u67d0\u4e2a\u5e8f\u5217\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5173\u952e\u5b57in\uff0c\u8fd9\u4e00\u64cd\u4f5c\u5e38\u88ab\u79f0\u4e3a\u201ccontA [&hellip;]","protected":false},"author":3,"featured_media":981029,"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\/981020"}],"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=981020"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/981020\/revisions"}],"predecessor-version":[{"id":981032,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/981020\/revisions\/981032"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/981029"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=981020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=981020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=981020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}