{"id":1116063,"date":"2025-01-08T18:13:00","date_gmt":"2025-01-08T10:13:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1116063.html"},"modified":"2025-01-08T18:13:03","modified_gmt":"2025-01-08T10:13:03","slug":"python%e5%a6%82%e4%bd%95%e5%8e%bb%e6%8e%89%e6%9c%80%e9%ab%98%e5%88%86%e5%92%8c%e6%9c%80%e4%bd%8e%e5%88%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1116063.html","title":{"rendered":"python\u5982\u4f55\u53bb\u6389\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080539\/f2078339-f8cd-450f-8da9-0345d04e4d03.webp\" alt=\"python\u5982\u4f55\u53bb\u6389\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u53bb\u6389\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u6392\u5e8f\u3001\u5207\u7247\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<strong>\u6392\u5e8f<\/strong>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u53bb\u6389\u7b2c\u4e00\u4e2a\u548c\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u5373\u53ef\u53bb\u6389\u6700\u4f4e\u5206\u548c\u6700\u9ad8\u5206\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u8be6\u7ec6\u6b65\u9aa4\u548c\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def remove_extremes(scores):<\/p>\n<p>    if len(scores) &lt;= 2:<\/p>\n<p>        return []  # If there are less than 3 scores, return an empty list<\/p>\n<p>    sorted_scores = sorted(scores)  # Sort the list<\/p>\n<p>    return sorted_scores[1:-1]  # Return the list without the first and last elements<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u5185\u7f6e\u51fd\u6570\u63d0\u4f9b\u4e86\u4fbf\u6377\u7684\u65b9\u6cd5\u6765\u5904\u7406\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u53bb\u6389\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def remove_extremes(scores):<\/p>\n<p>    if len(scores) &lt;= 2:<\/p>\n<p>        return []  # If there are less than 3 scores, return an empty list<\/p>\n<p>    max_score = max(scores)  # Find the maximum score<\/p>\n<p>    min_score = min(scores)  # Find the minimum score<\/p>\n<p>    scores.remove(max_score)  # Remove the maximum score<\/p>\n<p>    scores.remove(min_score)  # Remove the minimum score<\/p>\n<p>    return scores<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u68c0\u67e5\u5217\u8868\u7684\u957f\u5ea6\uff0c\u5982\u679c\u5217\u8868\u4e2d\u6ca1\u6709\u8db3\u591f\u7684\u5143\u7d20\uff0c\u5373\u5c11\u4e8e3\u4e2a\u5143\u7d20\uff0c\u90a3\u4e48\u76f4\u63a5\u8fd4\u56de\u4e00\u4e2a\u7a7a\u5217\u8868\uff0c\u56e0\u4e3a\u53bb\u6389\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206\u540e\u5c06\u6ca1\u6709\u5269\u4f59\u5143\u7d20\u53ef\u4ee5\u4f7f\u7528\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>max()<\/code>\u548c<code>min()<\/code>\u5206\u522b\u627e\u5230\u5217\u8868\u4e2d\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\uff0c\u5e76\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u5c06\u5b83\u4eec\u4ece\u5217\u8868\u4e2d\u79fb\u9664\u3002\u6700\u540e\uff0c\u8fd4\u56de\u5904\u7406\u540e\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u53bb\u6389\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206\u3002\u6392\u5e8f\u540e\u7684\u5217\u8868\uff0c\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u6700\u4f4e\u5206\uff0c\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u662f\u6700\u9ad8\u5206\u3002\u901a\u8fc7\u5207\u7247\u64cd\u4f5c\uff0c\u6211\u4eec\u53ef\u4ee5\u53bb\u6389\u8fd9\u4e9b\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def remove_extremes(scores):<\/p>\n<p>    if len(scores) &lt;= 2:<\/p>\n<p>        return []  # If there are less than 3 scores, return an empty list<\/p>\n<p>    sorted_scores = sorted(scores)  # Sort the list<\/p>\n<p>    return sorted_scores[1:-1]  # Return the list without the first and last elements<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u540c\u6837\u9996\u5148\u68c0\u67e5\u5217\u8868\u7684\u957f\u5ea6\u3002\u5982\u679c\u5217\u8868\u957f\u5ea6\u5c11\u4e8e3\u4e2a\u5143\u7d20\uff0c\u5219\u8fd4\u56de\u7a7a\u5217\u8868\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>sorted()<\/code>\u51fd\u6570\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002\u6392\u5e8f\u540e\u7684\u5217\u8868\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u6700\u4f4e\u5206\uff0c\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u662f\u6700\u9ad8\u5206\u3002\u901a\u8fc7\u5207\u7247\u64cd\u4f5c<code>[1:-1]<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u53bb\u6389\u8fd9\u4e24\u4e2a\u5143\u7d20\u5e76\u8fd4\u56de\u5904\u7406\u540e\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u96c6\u5408<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u96c6\u5408\u53ef\u4ee5\u6709\u6548\u5730\u53bb\u9664\u91cd\u590d\u5143\u7d20\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u96c6\u5408\u672c\u8eab\u662f\u65e0\u5e8f\u7684\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5f71\u54cd\u7ed3\u679c\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u96c6\u5408\u53bb\u6389\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def remove_extremes(scores):<\/p>\n<p>    if len(scores) &lt;= 2:<\/p>\n<p>        return []  # If there are less than 3 scores, return an empty list<\/p>\n<p>    scores_set = set(scores)  # Convert list to set to remove duplicates<\/p>\n<p>    scores_set.remove(max(scores_set))  # Remove the maximum score<\/p>\n<p>    scores_set.remove(min(scores_set))  # Remove the minimum score<\/p>\n<p>    return list(scores_set)  # Convert set back to list<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u4ee5\u53bb\u9664\u91cd\u590d\u7684\u5143\u7d20\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u96c6\u5408\u7684<code>remove()<\/code>\u65b9\u6cd5\u53bb\u9664\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206\uff0c\u6700\u540e\u5c06\u96c6\u5408\u8f6c\u6362\u56de\u5217\u8868\u5e76\u8fd4\u56de\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u96c6\u5408\u662f\u65e0\u5e8f\u7684\uff0c\u56e0\u6b64\u8fd4\u56de\u7684\u5217\u8868\u987a\u5e8f\u53ef\u80fd\u4e0e\u539f\u5217\u8868\u4e0d\u540c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Counter<\/h3>\n<\/p>\n<p><p><code>collections.Counter<\/code>\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u7c7b\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7edf\u8ba1\u5217\u8868\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>Counter<\/code>\u53bb\u6389\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>def remove_extremes(scores):<\/p>\n<p>    if len(scores) &lt;= 2:<\/p>\n<p>        return []  # If there are less than 3 scores, return an empty list<\/p>\n<p>    counter = Counter(scores)  # Count occurrences of each score<\/p>\n<p>    max_score = max(counter)  # Find the maximum score<\/p>\n<p>    min_score = min(counter)  # Find the minimum score<\/p>\n<p>    counter[max_score] -= 1  # Decrease the count of the maximum score by 1<\/p>\n<p>    counter[min_score] -= 1  # Decrease the count of the minimum score by 1<\/p>\n<p>    result = []<\/p>\n<p>    for score, count in counter.items():<\/p>\n<p>        result.extend([score] * count)  # Add each score count times to the result list<\/p>\n<p>    return result<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>Counter<\/code>\u6765\u7edf\u8ba1\u6bcf\u4e2a\u5206\u6570\u7684\u51fa\u73b0\u6b21\u6570\u3002\u7136\u540e\uff0c\u627e\u5230\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206\uff0c\u5e76\u5c06\u5b83\u4eec\u7684\u8ba1\u6570\u51cf\u4e00\u3002\u6700\u540e\uff0c\u6211\u4eec\u6784\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u53bb\u6389\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206\u540e\u7684\u5206\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u53bb\u6389\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def custom_sort(scores):<\/p>\n<p>    return sorted(scores, key=lambda x: (x, -scores.count(x)))<\/p>\n<p>def remove_extremes(scores):<\/p>\n<p>    if len(scores) &lt;= 2:<\/p>\n<p>        return []  # If there are less than 3 scores, return an empty list<\/p>\n<p>    sorted_scores = custom_sort(scores)  # Custom sort the list<\/p>\n<p>    return sorted_scores[1:-1]  # Return the list without the first and last elements<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570<code>custom_sort<\/code>\uff0c\u8be5\u51fd\u6570\u9996\u5148\u6309\u5206\u6570\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u6309\u5206\u6570\u51fa\u73b0\u7684\u6b21\u6570\u8fdb\u884c\u9006\u5e8f\u6392\u5e8f\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5206\u6570\u76f8\u540c\u65f6\uff0c\u51fa\u73b0\u6b21\u6570\u591a\u7684\u5206\u6570\u6392\u5728\u524d\u9762\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u8fd9\u4e2a\u81ea\u5b9a\u4e49\u6392\u5e8f\u51fd\u6570\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u53bb\u6389\u7b2c\u4e00\u4e2a\u548c\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528Numpy\u5e93<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u6b63\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Numpy\u5e93\u6765\u63d0\u9ad8\u6027\u80fd\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Numpy\u53bb\u6389\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def remove_extremes(scores):<\/p>\n<p>    if len(scores) &lt;= 2:<\/p>\n<p>        return []  # If there are less than 3 scores, return an empty list<\/p>\n<p>    scores_array = np.array(scores)  # Convert list to numpy array<\/p>\n<p>    max_score = np.max(scores_array)  # Find the maximum score<\/p>\n<p>    min_score = np.min(scores_array)  # Find the minimum score<\/p>\n<p>    scores_array = scores_array[scores_array != max_score]  # Remove the maximum score<\/p>\n<p>    scores_array = scores_array[scores_array != min_score]  # Remove the minimum score<\/p>\n<p>    return scores_array.tolist()  # Convert numpy array back to list<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u5217\u8868\u8f6c\u6362\u4e3aNumpy\u6570\u7ec4\u3002\u7136\u540e\uff0c\u4f7f\u7528Numpy\u7684<code>max()<\/code>\u548c<code>min()<\/code>\u51fd\u6570\u627e\u5230\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\uff0c\u5e76\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u53bb\u9664\u8fd9\u4e9b\u503c\u3002\u6700\u540e\uff0c\u5c06\u5904\u7406\u540e\u7684Numpy\u6570\u7ec4\u8f6c\u6362\u56de\u5217\u8868\u5e76\u8fd4\u56de\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u5e93\u662f\u5904\u7406\u6570\u636e\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u4ee5\u4e0b\u662f\u4f7f\u7528Pandas\u53bb\u6389\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def remove_extremes(scores):<\/p>\n<p>    if len(scores) &lt;= 2:<\/p>\n<p>        return []  # If there are less than 3 scores, return an empty list<\/p>\n<p>    scores_series = pd.Series(scores)  # Convert list to pandas series<\/p>\n<p>    max_score = scores_series.max()  # Find the maximum score<\/p>\n<p>    min_score = scores_series.min()  # Find the minimum score<\/p>\n<p>    scores_series = scores_series[scores_series != max_score]  # Remove the maximum score<\/p>\n<p>    scores_series = scores_series[scores_series != min_score]  # Remove the minimum score<\/p>\n<p>    return scores_series.tolist()  # Convert pandas series back to list<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u5217\u8868\u8f6c\u6362\u4e3aPandas\u7684Series\u5bf9\u8c61\u3002\u7136\u540e\uff0c\u4f7f\u7528Pandas\u7684<code>max()<\/code>\u548c<code>min()<\/code>\u65b9\u6cd5\u627e\u5230\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\uff0c\u5e76\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u53bb\u9664\u8fd9\u4e9b\u503c\u3002\u6700\u540e\uff0c\u5c06\u5904\u7406\u540e\u7684Series\u5bf9\u8c61\u8f6c\u6362\u56de\u5217\u8868\u5e76\u8fd4\u56de\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u7edf\u8ba1\u5e93<\/h3>\n<\/p>\n<p><p>Python\u7684statistics\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6709\u7528\u7684\u7edf\u8ba1\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u6765\u53bb\u6389\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import statistics<\/p>\n<p>def remove_extremes(scores):<\/p>\n<p>    if len(scores) &lt;= 2:<\/p>\n<p>        return []  # If there are less than 3 scores, return an empty list<\/p>\n<p>    max_score = max(scores)  # Find the maximum score<\/p>\n<p>    min_score = min(scores)  # Find the minimum score<\/p>\n<p>    filtered_scores = [score for score in scores if score != max_score and score != min_score]<\/p>\n<p>    return filtered_scores<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u627e\u5230\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\uff0c\u7136\u540e\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8fc7\u6ee4\u6389\u8fd9\u4e9b\u503c\u3002\u6700\u540e\u8fd4\u56de\u5904\u7406\u540e\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u591a\u7ebf\u7a0b\u6765\u63d0\u9ad8\u6027\u80fd\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u591a\u7ebf\u7a0b\u53bb\u6389\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def remove_extremes(scores):<\/p>\n<p>    if len(scores) &lt;= 2:<\/p>\n<p>        return []  # If there are less than 3 scores, return an empty list<\/p>\n<p>    def find_max_min():<\/p>\n<p>        nonlocal max_score, min_score<\/p>\n<p>        max_score = max(scores)  # Find the maximum score<\/p>\n<p>        min_score = min(scores)  # Find the minimum score<\/p>\n<p>    max_score = None<\/p>\n<p>    min_score = None<\/p>\n<p>    thread = threading.Thread(target=find_max_min)<\/p>\n<p>    thread.start()<\/p>\n<p>    thread.join()<\/p>\n<p>    filtered_scores = [score for score in scores if score != max_score and score != min_score]<\/p>\n<p>    return filtered_scores<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u591a\u7ebf\u7a0b\u6765\u67e5\u627e\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u3002\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u7ebf\u7a0b\u51fd\u6570\u6765\u67e5\u627e\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\uff0c\u7136\u540e\u542f\u52a8\u7ebf\u7a0b\u5e76\u7b49\u5f85\u5176\u5b8c\u6210\u3002\u6700\u540e\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8fc7\u6ee4\u6389\u8fd9\u4e9b\u503c\u5e76\u8fd4\u56de\u5904\u7406\u540e\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u7efc\u5408\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u7efc\u5408\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7efc\u5408\u5e94\u7528\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import pandas as pd<\/p>\n<p>from collections import Counter<\/p>\n<p>def remove_extremes(scores):<\/p>\n<p>    if len(scores) &lt;= 2:<\/p>\n<p>        return []  # If there are less than 3 scores, return an empty list<\/p>\n<p>    # Convert list to numpy array<\/p>\n<p>    scores_array = np.array(scores)<\/p>\n<p>    max_score = np.max(scores_array)<\/p>\n<p>    min_score = np.min(scores_array)<\/p>\n<p>    # Convert list to pandas series<\/p>\n<p>    scores_series = pd.Series(scores)<\/p>\n<p>    scores_series = scores_series[scores_series != max_score]<\/p>\n<p>    scores_series = scores_series[scores_series != min_score]<\/p>\n<p>    # Use Counter to count occurrences<\/p>\n<p>    counter = Counter(scores_series)<\/p>\n<p>    result = [score for score in scores_series if counter[score] &gt; 1]<\/p>\n<p>    return result<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u5217\u8868\u8f6c\u6362\u4e3aNumpy\u6570\u7ec4\u5e76\u627e\u5230\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u3002\u7136\u540e\u5c06\u5217\u8868\u8f6c\u6362\u4e3aPandas\u7684Series\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u53bb\u9664\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u3002\u6700\u540e\u4f7f\u7528Counter\u7edf\u8ba1\u6bcf\u4e2a\u5206\u6570\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u5e76\u6784\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5305\u542b\u53bb\u6389\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206\u540e\u7684\u5206\u6570\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u53bb\u6389Python\u5217\u8868\u4e2d\u7684\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206\uff0c\u4ece\u800c\u4f7f\u6570\u636e\u66f4\u52a0\u7a33\u5b9a\u548c\u53ef\u9760\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u9009\u62e9\u6700\u9002\u5408\u81ea\u5df1\u9700\u6c42\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u5904\u7406\u7684\u6548\u7387\u548c\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u53bb\u6389\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>max()<\/code>\u548c<code>min()<\/code>\u51fd\u6570\u6765\u627e\u5230\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206\uff0c\u7136\u540e\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u8fc7\u6ee4\u51fd\u6570\u53bb\u6389\u8fd9\u4e9b\u5143\u7d20\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">scores = [90, 80, 70, 85, 95]\nmax_score = max(scores)\nmin_score = min(scores)\nfiltered_scores = [score for score in scores if score != max_score and score != min_score]\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<code>filtered_scores<\/code>\u5c06\u5305\u542b\u53bb\u6389\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206\u540e\u7684\u6210\u7ee9\u3002<\/p>\n<p><strong>\u4f7f\u7528numpy\u5e93\u5982\u4f55\u7b80\u5316\u53bb\u6389\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206\u7684\u64cd\u4f5c\uff1f<\/strong><br \/>\u5229\u7528numpy\u5e93\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5904\u7406\u6570\u7ec4\u6570\u636e\uff0c\u7279\u522b\u662f\u5f53\u6570\u636e\u91cf\u8f83\u5927\u65f6\u3002\u53ef\u4ee5\u4f7f\u7528<code>np.delete()<\/code>\u7ed3\u5408<code>np.argmax()<\/code>\u548c<code>np.argmin()<\/code>\u65b9\u6cd5\uff0c\u53bb\u6389\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\nscores = np.array([90, 80, 70, 85, 95])\nfiltered_scores = np.delete(scores, [np.argmax(scores), np.argmin(scores)])\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<code>filtered_scores<\/code>\u5c06\u53ea\u5305\u542b\u4e2d\u95f4\u7684\u5206\u6570\u3002<\/p>\n<p><strong>\u53bb\u6389\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206\u540e\u5982\u4f55\u8ba1\u7b97\u5269\u4f59\u5206\u6570\u7684\u5e73\u5747\u503c\uff1f<\/strong><br \/>\u5728\u53bb\u6389\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sum()<\/code>\u548c<code>len()<\/code>\u51fd\u6570\u8ba1\u7b97\u5269\u4f59\u5206\u6570\u7684\u5e73\u5747\u503c\u3002\u786e\u4fdd\u5728\u8ba1\u7b97\u4e4b\u524d\uff0c\u5217\u8868\u4e2d\u4ecd\u7136\u6709\u8db3\u591f\u7684\u5206\u6570\u6570\u636e\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">if len(filtered_scores) &gt; 0:\n    average_score = sum(filtered_scores) \/ len(filtered_scores)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u4fbf\u53ef\u4ee5\u5f97\u5230\u53bb\u6389\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206\u540e\u5269\u4f59\u5206\u6570\u7684\u5e73\u5747\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u53bb\u6389\u6700\u9ad8\u5206\u548c\u6700\u4f4e\u5206\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u6392\u5e8f\u3001\u5207\u7247\u3002 \u5176\u4e2d\uff0c\u6392\u5e8f\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b [&hellip;]","protected":false},"author":3,"featured_media":1116071,"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\/1116063"}],"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=1116063"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1116063\/revisions"}],"predecessor-version":[{"id":1116072,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1116063\/revisions\/1116072"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1116071"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1116063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1116063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1116063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}