{"id":1025701,"date":"2024-12-31T10:36:15","date_gmt":"2024-12-31T02:36:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1025701.html"},"modified":"2024-12-31T10:36:18","modified_gmt":"2024-12-31T02:36:18","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%8f%90%e5%8f%96%e5%88%97%e8%a1%a8%e4%b8%ad%e7%9a%84%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1025701.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u63d0\u53d6\u5217\u8868\u4e2d\u7684\u5143\u7d20"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/c9896c17-81df-4e21-ac7a-601c96a860ab.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5982\u4f55\u63d0\u53d6\u5217\u8868\u4e2d\u7684\u5143\u7d20\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u63d0\u53d6\u5217\u8868\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u7d22\u5f15\u3001\u5207\u7247\u3001\u5217\u8868\u89e3\u6790\u3001\u5faa\u73af\u3001\u5185\u7f6e\u51fd\u6570\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c\u7d22\u5f15\u548c\u5207\u7247\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u7d22\u5f15\u53ef\u4ee5\u63d0\u53d6\u5355\u4e2a\u5143\u7d20\uff0c\u901a\u8fc7\u5207\u7247\u53ef\u4ee5\u63d0\u53d6\u4e00\u6bb5\u8fde\u7eed\u7684\u5143\u7d20\u3002\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u63d0\u53d6\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5bf9\u5217\u8868\u8fdb\u884c\u64cd\u4f5c\u3002\u5faa\u73af\u548c\u5185\u7f6e\u51fd\u6570\u5982<code>filter()<\/code>\u3001<code>map()<\/code>\u7b49\u5219\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u63d0\u53d6\u9700\u6c42\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u4e3e\u4f8b\u8bf4\u660e\u5b83\u4eec\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u7d22\u5f15\u548c\u5207\u7247<\/p>\n<\/p>\n<p><p>\u7d22\u5f15\u548c\u5207\u7247\u662f\u6700\u57fa\u672c\u7684\u5217\u8868\u5143\u7d20\u63d0\u53d6\u65b9\u6cd5\u3002\u7d22\u5f15\u7528\u4e8e\u63d0\u53d6\u5355\u4e2a\u5143\u7d20\uff0c\u800c\u5207\u7247\u7528\u4e8e\u63d0\u53d6\u4e00\u6bb5\u8fde\u7eed\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5217\u8868\u7684\u7d22\u5f15\u4ece0\u5f00\u59cb\u3002\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u76f4\u63a5\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 40, 50]<\/p>\n<p>first_element = my_list[0]<\/p>\n<p>print(first_element)  # \u8f93\u51fa\uff1a10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7d22\u5f15\u4e5f\u53ef\u4ee5\u662f\u8d1f\u6570\uff0c\u8868\u793a\u4ece\u5217\u8868\u672b\u5c3e\u5f00\u59cb\u8ba1\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">last_element = my_list[-1]<\/p>\n<p>print(last_element)  # \u8f93\u51fa\uff1a50<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5207\u7247<\/h3>\n<\/p>\n<p><p>\u5207\u7247\u53ef\u4ee5\u63d0\u53d6\u5217\u8868\u4e2d\u7684\u4e00\u4e2a\u5b50\u5217\u8868\uff0c\u8bed\u6cd5\u4e3a<code>list[start:stop:step]<\/code>\u3002<code>start<\/code>\u662f\u8d77\u59cb\u7d22\u5f15\uff0c<code>stop<\/code>\u662f\u7ed3\u675f\u7d22\u5f15\uff0c<code>step<\/code>\u662f\u6b65\u957f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sub_list = my_list[1:4]<\/p>\n<p>print(sub_list)  # \u8f93\u51fa\uff1a[20, 30, 40]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u7701\u7565<code>start<\/code>\u6216<code>stop<\/code>\uff0c\u8868\u793a\u4ece\u5217\u8868\u5f00\u5934\u6216\u76f4\u5230\u5217\u8868\u7ed3\u5c3e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sub_list = my_list[:3]<\/p>\n<p>print(sub_list)  # \u8f93\u51fa\uff1a[10, 20, 30]<\/p>\n<p>sub_list = my_list[2:]<\/p>\n<p>print(sub_list)  # \u8f93\u51fa\uff1a[30, 40, 50]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u5217\u8868\u89e3\u6790<\/p>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5bf9\u5217\u8868\u8fdb\u884c\u64cd\u4f5c\u3002\u5b83\u7684\u8bed\u6cd5\u4e3a<code>[expression for item in iterable if condition]<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5217\u8868\uff0c\u60f3\u63d0\u53d6\u5176\u4e2d\u7684\u6240\u6709\u5076\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>even_numbers = [x for x in my_list if x % 2 == 0]<\/p>\n<p>print(even_numbers)  # \u8f93\u51fa\uff1a[2, 4, 6, 8, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5faa\u73af<\/p>\n<\/p>\n<p><p>\u5faa\u73af\u662f\u63d0\u53d6\u5217\u8868\u5143\u7d20\u7684\u53e6\u4e00\u79cd\u5e38\u89c1\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u63d0\u53d6\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u9700\u8981\u63d0\u53d6\u5217\u8868\u4e2d\u6240\u6709\u5927\u4e8e5\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>greater_than_five = []<\/p>\n<p>for x in my_list:<\/p>\n<p>    if x &gt; 5:<\/p>\n<p>        greater_than_five.append(x)<\/p>\n<p>print(greater_than_five)  # \u8f93\u51fa\uff1a[6, 7, 8, 9, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5185\u7f6e\u51fd\u6570<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u7f6e\u51fd\u6570\uff0c\u5982<code>filter()<\/code>\u3001<code>map()<\/code>\u7b49\uff0c\u53ef\u4ee5\u7528\u4e8e\u5217\u8868\u5143\u7d20\u7684\u63d0\u53d6\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3><code>filter()<\/code><\/h3>\n<\/p>\n<p><p><code>filter()<\/code>\u51fd\u6570\u7528\u4e8e\u8fc7\u6ee4\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u8bed\u6cd5\u4e3a<code>filter(function, iterable)<\/code>\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u8fc7\u6ee4\u540e\u7684\u8fed\u4ee3\u5668\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u9700\u8981\u63d0\u53d6\u5217\u8868\u4e2d\u7684\u6240\u6709\u5076\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>even_numbers = list(filter(lambda x: x % 2 == 0, my_list))<\/p>\n<p>print(even_numbers)  # \u8f93\u51fa\uff1a[2, 4, 6, 8, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><code>map()<\/code><\/h3>\n<\/p>\n<p><p><code>map()<\/code>\u51fd\u6570\u7528\u4e8e\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u4e00\u4e2a\u51fd\u6570\uff0c\u8bed\u6cd5\u4e3a<code>map(function, iterable)<\/code>\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u5904\u7406\u540e\u7684\u8fed\u4ee3\u5668\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u9700\u8981\u5c06\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u52a01\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>incremented_list = list(map(lambda x: x + 1, my_list))<\/p>\n<p>print(incremented_list)  # \u8f93\u51fa\uff1a[2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001<code>itertools<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p>Python\u7684<code>itertools<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u4e2a\u7528\u4e8e\u8fed\u4ee3\u64cd\u4f5c\u7684\u5de5\u5177\uff0c\u5176\u4e2d\u7684\u4e00\u4e9b\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u5217\u8868\u5143\u7d20\u7684\u63d0\u53d6\u3002<\/p>\n<\/p>\n<p><h3><code>itertools.compress()<\/code><\/h3>\n<\/p>\n<p><p><code>compress()<\/code>\u51fd\u6570\u53ef\u4ee5\u6839\u636e\u4e00\u4e2a\u9009\u62e9\u5668\u5217\u8868\u5bf9\u5143\u7d20\u8fdb\u884c\u8fc7\u6ee4\uff0c\u8bed\u6cd5\u4e3a<code>itertools.compress(data, selectors)<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u9009\u62e9\u5668\u5217\u8868\uff0c\u7528\u4e8e\u9009\u62e9\u8981\u63d0\u53d6\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>selectors = [True, False, True, False, True]<\/p>\n<p>filtered_list = list(itertools.compress(my_list, selectors))<\/p>\n<p>print(filtered_list)  # \u8f93\u51fa\uff1a[1, 3, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><code>itertools.islice()<\/code><\/h3>\n<\/p>\n<p><p><code>islice()<\/code>\u51fd\u6570\u7528\u4e8e\u4ece\u8fed\u4ee3\u5668\u4e2d\u63d0\u53d6\u5143\u7d20\uff0c\u7c7b\u4f3c\u4e8e\u5217\u8868\u5207\u7247\uff0c\u8bed\u6cd5\u4e3a<code>itertools.islice(iterable, start, stop, step)<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u9700\u8981\u4ece\u5217\u8868\u4e2d\u63d0\u53d6\u4ece\u7d22\u5f151\u5230\u7d22\u5f154\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>sliced_list = list(itertools.islice(my_list, 1, 4))<\/p>\n<p>print(sliced_list)  # \u8f93\u51fa\uff1a[2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001<code>numpy<\/code>\u5e93<\/p>\n<\/p>\n<p><p>\u5982\u679c\u6211\u4eec\u5904\u7406\u7684\u662f\u6570\u503c\u6570\u636e\uff0c<code>numpy<\/code>\u5e93\u63d0\u4f9b\u4e86\u66f4\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u65b9\u6cd5\u3002<code>numpy<\/code>\u7684\u6570\u7ec4\u5bf9\u8c61\u6bd4Python\u7684\u5217\u8868\u66f4\u9ad8\u6548\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a<code>numpy<\/code>\u6570\u7ec4\uff0c\u60f3\u63d0\u53d6\u5176\u4e2d\u7684\u6240\u6709\u5076\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>my_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])<\/p>\n<p>even_numbers = my_array[my_array % 2 == 0]<\/p>\n<p>print(even_numbers)  # \u8f93\u51fa\uff1a[2 4 6 8 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001<code>pandas<\/code>\u5e93<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6570\u636e\u5206\u6790\u4efb\u52a1\uff0c<code>pandas<\/code>\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u64cd\u4f5c\u5de5\u5177\u3002<code>pandas<\/code>\u7684<code>Series<\/code>\u548c<code>DataFrame<\/code>\u5bf9\u8c61\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5143\u7d20\u63d0\u53d6\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a<code>pandas<\/code>\u7684<code>Series<\/code>\u5bf9\u8c61\uff0c\u60f3\u63d0\u53d6\u5176\u4e2d\u7684\u6240\u6709\u5927\u4e8e5\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>my_series = pd.Series([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])<\/p>\n<p>greater_than_five = my_series[my_series &gt; 5]<\/p>\n<p>print(greater_than_five)<\/p>\n<h2><strong>\u8f93\u51fa\uff1a<\/strong><\/h2>\n<h2><strong>5     6<\/strong><\/h2>\n<h2><strong>6     7<\/strong><\/h2>\n<h2><strong>7     8<\/strong><\/h2>\n<h2><strong>8     9<\/strong><\/h2>\n<h2><strong>9    10<\/strong><\/h2>\n<h2><strong>dtype: int64<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516b\u3001<code>collections<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p>Python\u7684<code>collections<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u51e0\u79cd\u989d\u5916\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982<code>deque<\/code>\u3001<code>Counter<\/code>\u3001<code>OrderedDict<\/code>\u7b49\uff0c\u5b83\u4eec\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5217\u8868\u5143\u7d20\u7684\u63d0\u53d6\u3002<\/p>\n<\/p>\n<p><h3><code>deque<\/code><\/h3>\n<\/p>\n<p><p><code>deque<\/code>\u662f\u4e00\u4e2a\u53cc\u7aef\u961f\u5217\uff0c\u53ef\u4ee5\u5728\u4e24\u7aef\u9ad8\u6548\u5730\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u9700\u8981\u4ece\u4e00\u4e2a<code>deque<\/code>\u4e2d\u63d0\u53d6\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import deque<\/p>\n<p>my_deque = deque([1, 2, 3, 4, 5])<\/p>\n<p>first_element = my_deque[0]<\/p>\n<p>last_element = my_deque[-1]<\/p>\n<p>print(first_element)  # \u8f93\u51fa\uff1a1<\/p>\n<p>print(last_element)   # \u8f93\u51fa\uff1a5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><code>Counter<\/code><\/h3>\n<\/p>\n<p><p><code>Counter<\/code>\u662f\u4e00\u4e2a\u8ba1\u6570\u5668\uff0c\u7528\u4e8e\u8ba1\u7b97\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5217\u8868\uff0c\u60f3\u63d0\u53d6\u51fa\u73b0\u6b21\u6570\u6700\u591a\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>my_list = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]<\/p>\n<p>counter = Counter(my_list)<\/p>\n<p>most_common = counter.most_common(1)<\/p>\n<p>print(most_common)  # \u8f93\u51fa\uff1a[(4, 4)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u4e86\u89e3\u4e86Python\u4e2d\u63d0\u53d6\u5217\u8868\u5143\u7d20\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u7d22\u5f15\u3001\u5207\u7247\u3001\u5217\u8868\u89e3\u6790\u3001\u5faa\u73af\u3001\u5185\u7f6e\u51fd\u6570\u3001<code>itertools<\/code>\u6a21\u5757\u3001<code>numpy<\/code>\u5e93\u3001<code>pandas<\/code>\u5e93\u548c<code>collections<\/code>\u6a21\u5757\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u70b9\uff0c\u8bfb\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u5217\u8868\u5143\u7d20\u7684\u63d0\u53d6\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u5728Python\u7f16\u7a0b\u4e2d\u7684\u6570\u636e\u5904\u7406\u5de5\u4f5c\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u63d0\u53d6\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u63d0\u53d6\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5143\u7d20\uff0c\u4f8b\u5982\u4f7f\u7528<code>list[index]<\/code>\u6765\u83b7\u53d6\u5355\u4e2a\u5143\u7d20\u3002\u5982\u679c\u60f3\u8981\u63d0\u53d6\u591a\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\uff0c\u4f8b\u5982<code>list[start:end]<\/code>\u6765\u83b7\u53d6\u4e00\u4e2a\u8303\u56f4\u5185\u7684\u5143\u7d20\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u548c<code>filter()<\/code>\u51fd\u6570\u6765\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u63d0\u53d6\u5143\u7d20\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u67d0\u4e2a\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\uff1f<\/strong><br \/>\u8981\u68c0\u67e5\u67d0\u4e2a\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u3002\u4f8b\u5982\uff0c<code>if element in list:<\/code>\u8bed\u53e5\u4f1a\u8fd4\u56deTrue\u6216False\uff0c\u6307\u793a\u8be5\u5143\u7d20\u662f\u5426\u5728\u5217\u8868\u4e2d\u3002\u5982\u679c\u9700\u8981\u67e5\u627e\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u4f7f\u7528<code>list.index(element)<\/code>\u65b9\u6cd5\uff0c\u5b83\u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u7d22\u5f15\uff0c\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\u5219\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u5927\u578b\u5217\u8868\u65f6\uff0c\u6709\u54ea\u4e9b\u6280\u5de7\u53ef\u4ee5\u4f18\u5316\u5143\u7d20\u63d0\u53d6\u7684\u6548\u7387\uff1f<\/strong><br \/>\u5728\u5904\u7406\u5927\u578b\u5217\u8868\u65f6\uff0c\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u5143\u7d20\u67e5\u627e\u7684\u6548\u7387\uff0c\u56e0\u4e3a\u96c6\u5408\u7684\u67e5\u627e\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(1)\u3002\u5982\u679c\u9700\u8981\u9891\u7e41\u67e5\u627e\uff0c\u53ef\u4ee5\u5148\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528<code>numpy<\/code>\u5e93\uff0c\u5b83\u53ef\u4ee5\u5904\u7406\u5927\u578b\u6570\u7ec4\u5e76\u63d0\u4f9b\u9ad8\u6548\u7684\u64cd\u4f5c\u3002\u4f7f\u7528<code>numpy<\/code>\u7684\u6570\u7ec4\u53ef\u4ee5\u5229\u7528\u5176\u5185\u7f6e\u7684\u5411\u91cf\u5316\u64cd\u4f5c\uff0c\u63d0\u9ad8\u63d0\u53d6\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u63d0\u53d6\u5217\u8868\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u7d22\u5f15\u3001\u5207\u7247\u3001\u5217\u8868\u89e3\u6790\u3001\u5faa\u73af\u3001\u5185\u7f6e\u51fd\u6570\u7b49\u3002 \u5176\u4e2d\uff0c\u7d22\u5f15\u548c\u5207\u7247 [&hellip;]","protected":false},"author":3,"featured_media":1025714,"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\/1025701"}],"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=1025701"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1025701\/revisions"}],"predecessor-version":[{"id":1025717,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1025701\/revisions\/1025717"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1025714"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1025701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1025701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1025701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}