{"id":1039977,"date":"2024-12-31T12:37:20","date_gmt":"2024-12-31T04:37:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1039977.html"},"modified":"2024-12-31T12:37:23","modified_gmt":"2024-12-31T04:37:23","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8python%e6%9d%a5%e6%b1%82%e5%b9%b3%e5%9d%87%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1039977.html","title":{"rendered":"\u5982\u4f55\u4f7f\u7528python\u6765\u6c42\u5e73\u5747\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/b362aebc-db90-439f-a240-a8faa7769fb4.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u4f7f\u7528python\u6765\u6c42\u5e73\u5747\u6570\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u6765\u6c42\u5e73\u5747\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u4f7f\u7528numpy\u5e93\u3001\u4f7f\u7528pandas\u5e93<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e09\u79cd\u65b9\u6cd5\u4e2d\u7684\u4e00\u79cd\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u8981\u4f7f\u7528Python\u6765\u6c42\u5e73\u5747\u6570\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u8fd9\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5904\u7406\u8f83\u5c0f\u7684\u6570\u636e\u96c6\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684\u6570\u636e\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u6216\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u3002\u7136\u540e\uff0c\u8ba1\u7b97\u5217\u8868\u4e2d\u6240\u6709\u5143\u7d20\u7684\u603b\u548c\uff0c\u5e76\u5c06\u5176\u9664\u4ee5\u5143\u7d20\u7684\u6570\u91cf\uff0c\u5373\u53ef\u5f97\u5230\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Python\u5185\u7f6e\u51fd\u6570\u8ba1\u7b97\u5e73\u5747\u6570<\/h3>\n<\/p>\n<p><h4>1\u3001\u57fa\u7840\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Python\u5185\u7f6e\u51fd\u6570\u6765\u8ba1\u7b97\u5e73\u5747\u6570\u975e\u5e38\u7b80\u5355\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5c06\u6570\u636e\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002<\/li>\n<li>\u4f7f\u7528<code>sum()<\/code>\u51fd\u6570\u8ba1\u7b97\u5217\u8868\u4e2d\u6240\u6709\u5143\u7d20\u7684\u603b\u548c\u3002<\/li>\n<li>\u4f7f\u7528<code>len()<\/code>\u51fd\u6570\u8ba1\u7b97\u5217\u8868\u4e2d\u5143\u7d20\u7684\u6570\u91cf\u3002<\/li>\n<li>\u5c06\u603b\u548c\u9664\u4ee5\u6570\u91cf\uff0c\u5f97\u5230\u5e73\u5747\u6570\u3002<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6570\u636e\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d<\/p>\n<p>data = [10, 20, 30, 40, 50]<\/p>\n<h2><strong>\u8ba1\u7b97\u5217\u8868\u4e2d\u6240\u6709\u5143\u7d20\u7684\u603b\u548c<\/strong><\/h2>\n<p>total_sum = sum(data)<\/p>\n<h2><strong>\u8ba1\u7b97\u5217\u8868\u4e2d\u5143\u7d20\u7684\u6570\u91cf<\/strong><\/h2>\n<p>num_elements = len(data)<\/p>\n<h2><strong>\u8ba1\u7b97\u5e73\u5747\u6570<\/strong><\/h2>\n<p>average = total_sum \/ num_elements<\/p>\n<p>print(&quot;\u5e73\u5747\u6570\u4e3a:&quot;, average)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u6570\u636e\u7684\u5217\u8868<code>data<\/code>\u3002\u63a5\u7740\uff0c\u4f7f\u7528<code>sum(data)<\/code>\u8ba1\u7b97\u603b\u548c\uff0c\u4f7f\u7528<code>len(data)<\/code>\u8ba1\u7b97\u5143\u7d20\u6570\u91cf\uff0c\u6700\u540e\u5c06\u603b\u548c\u9664\u4ee5\u5143\u7d20\u6570\u91cf\u5f97\u5230\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u7a7a\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u6709\u65f6\u53ef\u80fd\u4f1a\u9047\u5230\u7a7a\u5217\u8868\u3002\u5982\u679c\u76f4\u63a5\u5bf9\u7a7a\u5217\u8868\u6c42\u5e73\u5747\u6570\uff0c\u4f1a\u5f15\u53d1\u9664\u4ee5\u96f6\u7684\u9519\u8bef\u3002\u56e0\u6b64\uff0c\u5728\u8ba1\u7b97\u5e73\u5747\u6570\u4e4b\u524d\uff0c\u5e94\u8be5\u5148\u68c0\u67e5\u5217\u8868\u662f\u5426\u4e3a\u7a7a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6570\u636e\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d<\/p>\n<p>data = []<\/p>\n<h2><strong>\u68c0\u67e5\u5217\u8868\u662f\u5426\u4e3a\u7a7a<\/strong><\/h2>\n<p>if not data:<\/p>\n<p>    print(&quot;\u5217\u8868\u4e3a\u7a7a\uff0c\u65e0\u6cd5\u8ba1\u7b97\u5e73\u5747\u6570&quot;)<\/p>\n<p>else:<\/p>\n<p>    # \u8ba1\u7b97\u5217\u8868\u4e2d\u6240\u6709\u5143\u7d20\u7684\u603b\u548c<\/p>\n<p>    total_sum = sum(data)<\/p>\n<p>    # \u8ba1\u7b97\u5217\u8868\u4e2d\u5143\u7d20\u7684\u6570\u91cf<\/p>\n<p>    num_elements = len(data)<\/p>\n<p>    # \u8ba1\u7b97\u5e73\u5747\u6570<\/p>\n<p>    average = total_sum \/ num_elements<\/p>\n<p>    print(&quot;\u5e73\u5747\u6570\u4e3a:&quot;, average)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528numpy\u5e93\u8ba1\u7b97\u5e73\u5747\u6570<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5numpy\u5e93<\/h4>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u6570\u7ec4\u64cd\u4f5c\u7684\u51fd\u6570\u3002\u4f7f\u7528NumPy\u5e93\u8ba1\u7b97\u5e73\u5747\u6570\u975e\u5e38\u65b9\u4fbf\u3002\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5NumPy\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528numpy\u8ba1\u7b97\u5e73\u5747\u6570<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u8ba1\u7b97\u5e73\u5747\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u6570\u636e\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d<\/strong><\/h2>\n<p>data = [10, 20, 30, 40, 50]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4<\/strong><\/h2>\n<p>np_data = np.array(data)<\/p>\n<h2><strong>\u8ba1\u7b97\u5e73\u5747\u6570<\/strong><\/h2>\n<p>average = np.mean(np_data)<\/p>\n<p>print(&quot;\u5e73\u5747\u6570\u4e3a:&quot;, average)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u5217\u8868<code>data<\/code>\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4<code>np_data<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>np.mean(np_data)<\/code>\u8ba1\u7b97\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5904\u7406\u591a\u7ef4\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>NumPy\u8fd8\u53ef\u4ee5\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u6bcf\u4e00\u5217\u7684\u5e73\u5747\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u6570\u636e\u5b58\u50a8\u5728\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\u4e2d<\/strong><\/h2>\n<p>data = [[10, 20, 30], [40, 50, 60]]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4<\/strong><\/h2>\n<p>np_data = np.array(data)<\/p>\n<h2><strong>\u8ba1\u7b97\u6bcf\u4e00\u5217\u7684\u5e73\u5747\u6570<\/strong><\/h2>\n<p>average_columns = np.mean(np_data, axis=0)<\/p>\n<p>print(&quot;\u6bcf\u4e00\u5217\u7684\u5e73\u5747\u6570\u4e3a:&quot;, average_columns)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>axis=0<\/code>\u6307\u5b9a\u6cbf\u7740\u5217\u65b9\u5411\u8ba1\u7b97\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528pandas\u5e93\u8ba1\u7b97\u5e73\u5747\u6570<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5pandas\u5e93<\/h4>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u8868\u683c\u6570\u636e\u3002\u4f7f\u7528Pandas\u5e93\u8ba1\u7b97\u5e73\u5747\u6570\u4e5f\u975e\u5e38\u65b9\u4fbf\u3002\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Pandas\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528pandas\u8ba1\u7b97\u5e73\u5747\u6570<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u8ba1\u7b97\u5e73\u5747\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u6570\u636e\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d<\/strong><\/h2>\n<p>data = [10, 20, 30, 40, 50]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u8f6c\u6362\u4e3aPandas Series<\/strong><\/h2>\n<p>series_data = pd.Series(data)<\/p>\n<h2><strong>\u8ba1\u7b97\u5e73\u5747\u6570<\/strong><\/h2>\n<p>average = series_data.mean()<\/p>\n<p>print(&quot;\u5e73\u5747\u6570\u4e3a:&quot;, average)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u5217\u8868<code>data<\/code>\u8f6c\u6362\u4e3aPandas Series\u5bf9\u8c61<code>series_data<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>series_data.mean()<\/code>\u8ba1\u7b97\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5904\u7406\u6570\u636e\u6846<\/h4>\n<\/p>\n<p><p>Pandas\u8fd8\u53ef\u4ee5\u5904\u7406\u6570\u636e\u6846\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u6570\u636e\u6846\u4e2d\u6bcf\u4e00\u5217\u7684\u5e73\u5747\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u6570\u636e\u5b58\u50a8\u5728\u4e00\u4e2a\u5b57\u5178\u4e2d<\/strong><\/h2>\n<p>data = {&#39;A&#39;: [10, 20, 30], &#39;B&#39;: [40, 50, 60]}<\/p>\n<h2><strong>\u5c06\u5b57\u5178\u8f6c\u6362\u4e3aPandas DataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u8ba1\u7b97\u6bcf\u4e00\u5217\u7684\u5e73\u5747\u6570<\/strong><\/h2>\n<p>average_columns = df.mean()<\/p>\n<p>print(&quot;\u6bcf\u4e00\u5217\u7684\u5e73\u5747\u6570\u4e3a:&quot;)<\/p>\n<p>print(average_columns)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u5b57\u5178<code>data<\/code>\u8f6c\u6362\u4e3aPandas DataFrame\u5bf9\u8c61<code>df<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>df.mean()<\/code>\u8ba1\u7b97\u6bcf\u4e00\u5217\u7684\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7efc\u5408\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p>\u7ed3\u5408\u4e0a\u8ff0\u4e09\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7efc\u5408\u5b9e\u4f8b\uff0c\u6839\u636e\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u9009\u62e9\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u8ba1\u7b97\u5e73\u5747\u6570\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>def calculate_average(data, method=&#39;builtin&#39;):<\/p>\n<p>    if not data:<\/p>\n<p>        return &quot;\u5217\u8868\u4e3a\u7a7a\uff0c\u65e0\u6cd5\u8ba1\u7b97\u5e73\u5747\u6570&quot;<\/p>\n<p>    if method == &#39;builtin&#39;:<\/p>\n<p>        total_sum = sum(data)<\/p>\n<p>        num_elements = len(data)<\/p>\n<p>        average = total_sum \/ num_elements<\/p>\n<p>    elif method == &#39;numpy&#39;:<\/p>\n<p>        np_data = np.array(data)<\/p>\n<p>        average = np.mean(np_data)<\/p>\n<p>    elif method == &#39;pandas&#39;:<\/p>\n<p>        series_data = pd.Series(data)<\/p>\n<p>        average = series_data.mean()<\/p>\n<p>    else:<\/p>\n<p>        return &quot;\u65e0\u6548\u7684\u65b9\u6cd5&quot;<\/p>\n<p>    return average<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = [10, 20, 30, 40, 50]<\/p>\n<h2><strong>\u4f7f\u7528\u4e0d\u540c\u7684\u65b9\u6cd5\u8ba1\u7b97\u5e73\u5747\u6570<\/strong><\/h2>\n<p>average_builtin = calculate_average(data, method=&#39;builtin&#39;)<\/p>\n<p>average_numpy = calculate_average(data, method=&#39;numpy&#39;)<\/p>\n<p>average_pandas = calculate_average(data, method=&#39;pandas&#39;)<\/p>\n<p>print(&quot;\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8ba1\u7b97\u5e73\u5747\u6570:&quot;, average_builtin)<\/p>\n<p>print(&quot;\u4f7f\u7528NumPy\u8ba1\u7b97\u5e73\u5747\u6570:&quot;, average_numpy)<\/p>\n<p>print(&quot;\u4f7f\u7528Pandas\u8ba1\u7b97\u5e73\u5747\u6570:&quot;, average_pandas)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>calculate_average<\/code>\uff0c\u6839\u636e\u7528\u6237\u9009\u62e9\u7684\u65b9\u6cd5\u6765\u8ba1\u7b97\u5e73\u5747\u6570\u3002\u7136\u540e\uff0c\u4f7f\u7528\u4e0d\u540c\u7684\u65b9\u6cd5\u8ba1\u7b97\u793a\u4f8b\u6570\u636e\u7684\u5e73\u5747\u6570\u5e76\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u4f7f\u7528Python\u8ba1\u7b97\u5e73\u5747\u6570\u7684\u4e09\u79cd\u5e38\u89c1\u65b9\u6cd5\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528Pandas\u5e93\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u6570\u636e\u5904\u7406\u4efb\u52a1\uff0c\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u5373\u53ef\u6ee1\u8db3\u9700\u6c42\uff1b\u5bf9\u4e8e\u590d\u6742\u7684\u79d1\u5b66\u8ba1\u7b97\u4efb\u52a1\uff0c\u4f7f\u7528NumPy\u5e93\u66f4\u52a0\u9ad8\u6548\uff1b\u5bf9\u4e8e\u5904\u7406\u8868\u683c\u6570\u636e\uff0c\u4f7f\u7528Pandas\u5e93\u66f4\u52a0\u65b9\u4fbf\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\u4f7f\u7528Python\u8ba1\u7b97\u5e73\u5747\u6570\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u8ba1\u7b97\u4e00\u7ec4\u6570\u7684\u5e73\u5747\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8ba1\u7b97\u4e00\u7ec4\u6570\u7684\u5e73\u5747\u6570\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u5c06\u6240\u6709\u6570\u503c\u76f8\u52a0\uff0c\u7136\u540e\u9664\u4ee5\u6570\u503c\u7684\u603b\u4e2a\u6570\u3002\u4f7f\u7528\u5185\u7f6e\u7684<code>sum()<\/code>\u51fd\u6570\u548c<code>len()<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u5217\u8868<code>numbers = [1, 2, 3, 4, 5]<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8ba1\u7b97\u5e73\u5747\u6570\uff1a  <\/p>\n<pre><code class=\"language-python\">average = sum(numbers) \/ len(numbers)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u80fd\u5f97\u5230\u5e73\u5747\u6570\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5e93\u6765\u8ba1\u7b97\u5e73\u5747\u6570\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>\u5229\u7528Python\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\u5982NumPy\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u96c6\u3002NumPy\u63d0\u4f9b\u4e86<code>numpy.mean()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u76f4\u63a5\u8ba1\u7b97\u6570\u7ec4\u7684\u5e73\u5747\u503c\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\nnumbers = np.array([1, 2, 3, 4, 5])\naverage = np.mean(numbers)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u6027\u80fd\u66f4\u4f18\uff0c\u9002\u5408\u5904\u7406\u5927\u578b\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5305\u542b\u975e\u6570\u503c\u5143\u7d20\u7684\u5217\u8868\uff1f<\/strong><br \/>\u5728\u8ba1\u7b97\u5e73\u5747\u6570\u65f6\uff0c\u5982\u679c\u5217\u8868\u4e2d\u5305\u542b\u975e\u6570\u503c\u5143\u7d20\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8fc7\u6ee4\u6389\u975e\u6570\u503c\u5143\u7d20\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = [1, 2, &#39;a&#39;, 4, 5]\nfiltered_numbers = [num for num in numbers if isinstance(num, (int, float))]\naverage = sum(filtered_numbers) \/ len(filtered_numbers) if filtered_numbers else 0\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u786e\u4fdd\u53ea\u8ba1\u7b97\u6709\u6548\u7684\u6570\u503c\uff0c\u907f\u514d\u8fd0\u884c\u65f6\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u6765\u6c42\u5e73\u5747\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u4f7f\u7528numpy\u5e93\u3001\u4f7f\u7528pandas\u5e93\u3002\u4e0b\u9762\u5c06 [&hellip;]","protected":false},"author":3,"featured_media":1039982,"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\/1039977"}],"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=1039977"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1039977\/revisions"}],"predecessor-version":[{"id":1039985,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1039977\/revisions\/1039985"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1039982"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1039977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1039977"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1039977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}