{"id":1087956,"date":"2025-01-08T13:39:58","date_gmt":"2025-01-08T05:39:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1087956.html"},"modified":"2025-01-08T13:40:00","modified_gmt":"2025-01-08T05:40:00","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python%e4%b8%ad%e6%b1%82%e5%b9%b3%e5%9d%87%e5%80%bc-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1087956.html","title":{"rendered":"\u5982\u4f55\u5728Python\u4e2d\u6c42\u5e73\u5747\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200641\/c491c5c0-b88f-47c4-bc33-b27764c9ac10.webp\" alt=\"\u5982\u4f55\u5728Python\u4e2d\u6c42\u5e73\u5747\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u6c42\u5e73\u5747\u503c\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u4f7f\u7528\u7edf\u8ba1\u6a21\u5757\u3001\u4f7f\u7528NumPy\u5e93<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u662f\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u6613\u7528\u4e14\u4e0d\u9700\u8981\u989d\u5916\u7684\u5e93\u652f\u6301\u3002\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u4e09\u79cd\u65b9\u6cd5\uff0c\u5e76\u63a2\u8ba8\u5176\u4f18\u52bf\u548c\u52a3\u52bf\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528Python\u5185\u7f6e\u51fd\u6570\u6c42\u5e73\u5747\u503c\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8ba1\u7b97\u5217\u8868\u4e2d\u6240\u6709\u5143\u7d20\u7684\u548c\uff0c\u7136\u540e\u9664\u4ee5\u5143\u7d20\u7684\u4e2a\u6570\u6765\u83b7\u5f97\u5e73\u5747\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>average = sum(numbers) \/ len(numbers)<\/p>\n<p>print(f&quot;The average is: {average}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5305\u542b\u6570\u5b57\u7684\u5217\u8868<code>numbers<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>sum()<\/code>\u51fd\u6570\u8ba1\u7b97\u5217\u8868\u4e2d\u6240\u6709\u5143\u7d20\u7684\u548c\uff0c\u518d\u4f7f\u7528<code>len()<\/code>\u51fd\u6570\u8ba1\u7b97\u5217\u8868\u4e2d\u5143\u7d20\u7684\u4e2a\u6570\uff0c\u6700\u540e\u5c06\u603b\u548c\u9664\u4ee5\u5143\u7d20\u4e2a\u6570\u5f97\u5230\u5e73\u5747\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4e0d\u9700\u8981\u5b89\u88c5\u989d\u5916\u7684\u5e93<\/li>\n<li>\u7b80\u5355\u6613\u61c2\uff0c\u9002\u5408\u521d\u5b66\u8005<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\uff0c\u8ba1\u7b97\u901f\u5ea6\u53ef\u80fd\u4f1a\u8f83\u6162<\/li>\n<li>\u4e0d\u652f\u6301\u590d\u6742\u7684\u7edf\u8ba1\u5206\u6790<\/li>\n<\/ul>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u7edf\u8ba1\u6a21\u5757<\/h2>\n<\/p>\n<p><p>Python\u7684<code>statistics<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u6709\u7528\u7684\u7edf\u8ba1\u51fd\u6570\uff0c\u5176\u4e2d\u5305\u62ec\u6c42\u5e73\u5747\u503c\u7684\u51fd\u6570<code>mean()<\/code>\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>statistics<\/code>\u6a21\u5757\u6c42\u5e73\u5747\u503c\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import statistics<\/p>\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n<p>average = statistics.mean(numbers)<\/p>\n<p>print(f&quot;The average is: {average}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86<code>statistics<\/code>\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>mean()<\/code>\u51fd\u6570\u76f4\u63a5\u8ba1\u7b97\u5217\u8868<code>numbers<\/code>\u7684\u5e73\u5747\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7edf\u8ba1\u529f\u80fd\uff0c\u5982\u4e2d\u4f4d\u6570\u3001\u4f17\u6570\u7b49<\/li>\n<li>\u4ee3\u7801\u7b80\u6d01\uff0c\u6613\u4e8e\u7ef4\u62a4<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u989d\u5916\u5bfc\u5165<code>statistics<\/code>\u6a21\u5757<\/li>\n<li>\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\uff0c\u8ba1\u7b97\u901f\u5ea6\u4ecd\u7136\u53ef\u80fd\u8f83\u6162<\/li>\n<\/ul>\n<p><h2>\u4e09\u3001\u4f7f\u7528NumPy\u5e93<\/h2>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u548c\u6570\u5b66\u51fd\u6570\u3002\u4f7f\u7528NumPy\u5e93\u6c42\u5e73\u5747\u503c\u4e0d\u4ec5\u5feb\u901f\uff0c\u800c\u4e14\u9002\u7528\u4e8e\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528NumPy\u5e93\u6c42\u5e73\u5747\u503c\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n<p>average = np.mean(numbers)<\/p>\n<p>print(f&quot;The average is: {average}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86NumPy\u5e93\uff0c\u5e76\u5c06\u5176\u547d\u540d\u4e3a<code>np<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>np.mean()<\/code>\u51fd\u6570\u76f4\u63a5\u8ba1\u7b97\u5217\u8868<code>numbers<\/code>\u7684\u5e73\u5747\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u8ba1\u7b97\u901f\u5ea6\u5feb\uff0c\u9002\u5408\u5904\u7406\u5927\u578b\u6570\u636e\u96c6<\/li>\n<li>\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u79d1\u5b66\u8ba1\u7b97\u529f\u80fd<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u5b89\u88c5NumPy\u5e93<\/li>\n<li>\u5bf9\u521d\u5b66\u8005\u800c\u8a00\uff0c\u53ef\u80fd\u4e0d\u5982\u5185\u7f6e\u51fd\u6570\u76f4\u89c2<\/li>\n<\/ul>\n<p><h2>\u56db\u3001\u5904\u7406\u7a7a\u5217\u8868\u548c\u975e\u6570\u503c\u5143\u7d20<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5904\u7406\u53ef\u80fd\u5305\u542b\u7a7a\u5217\u8868\u6216\u975e\u6570\u503c\u5143\u7d20\u7684\u60c5\u51b5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_average(numbers):<\/p>\n<p>    if not numbers:<\/p>\n<p>        return None<\/p>\n<p>    valid_numbers = [num for num in numbers if isinstance(num, (int, float))]<\/p>\n<p>    if not valid_numbers:<\/p>\n<p>        return None<\/p>\n<p>    return sum(valid_numbers) \/ len(valid_numbers)<\/p>\n<p>numbers = [1, 2, &#39;a&#39;, 4, 5]<\/p>\n<p>average = calculate_average(numbers)<\/p>\n<p>if average is not None:<\/p>\n<p>    print(f&quot;The average is: {average}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;The list is empty or cont<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ns no valid numbers.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>calculate_average()<\/code>\uff0c\u9996\u5148\u68c0\u67e5\u5217\u8868\u662f\u5426\u4e3a\u7a7a\uff0c\u5982\u679c\u4e3a\u7a7a\u5219\u8fd4\u56de<code>None<\/code>\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8fc7\u6ee4\u51fa\u6240\u6709\u6709\u6548\u7684\u6570\u503c\u5143\u7d20\uff0c\u5e76\u518d\u6b21\u68c0\u67e5\u8fc7\u6ee4\u540e\u7684\u5217\u8868\u662f\u5426\u4e3a\u7a7a\u3002\u5982\u679c\u4e3a\u7a7a\uff0c\u5219\u8fd4\u56de<code>None<\/code>\uff1b\u5426\u5219\uff0c\u8ba1\u7b97\u5e76\u8fd4\u56de\u5e73\u5747\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5904\u7406\u7a7a\u5217\u8868\u548c\u975e\u6570\u503c\u5143\u7d20\u7684\u60c5\u51b5<\/li>\n<li>\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u8f83\u590d\u6742\uff0c\u521d\u5b66\u8005\u53ef\u80fd\u4e0d\u5bb9\u6613\u7406\u89e3<\/li>\n<\/ul>\n<p><h2>\u4e94\u3001\u6269\u5c55\uff1a\u52a0\u6743\u5e73\u5747\u503c<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u8ba1\u7b97\u52a0\u6743\u5e73\u5747\u503c\uff0c\u5373\u6bcf\u4e2a\u5143\u7d20\u5bf9\u5e73\u5747\u503c\u7684\u8d21\u732e\u4e0d\u540c\u3002\u4ee5\u4e0b\u662f\u8ba1\u7b97\u52a0\u6743\u5e73\u5747\u503c\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def weighted_average(values, weights):<\/p>\n<p>    if len(values) != len(weights):<\/p>\n<p>        raise ValueError(&quot;The length of values and weights must be the same&quot;)<\/p>\n<p>    weighted_sum = sum(value * weight for value, weight in zip(values, weights))<\/p>\n<p>    total_weight = sum(weights)<\/p>\n<p>    return weighted_sum \/ total_weight<\/p>\n<p>values = [1, 2, 3, 4, 5]<\/p>\n<p>weights = [0.1, 0.2, 0.3, 0.4, 0.5]<\/p>\n<p>average = weighted_average(values, weights)<\/p>\n<p>print(f&quot;The weighted average is: {average}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>weighted_average()<\/code>\uff0c\u9996\u5148\u68c0\u67e5\u503c\u548c\u6743\u91cd\u5217\u8868\u7684\u957f\u5ea6\u662f\u5426\u76f8\u540c\uff0c\u5982\u679c\u4e0d\u540c\u5219\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\u3002\u63a5\u7740\uff0c\u6211\u4eec\u8ba1\u7b97\u52a0\u6743\u548c\u4ee5\u53ca\u6743\u91cd\u603b\u548c\uff0c\u5e76\u8fd4\u56de\u52a0\u6743\u548c\u9664\u4ee5\u6743\u91cd\u603b\u548c\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u652f\u6301\u8ba1\u7b97\u52a0\u6743\u5e73\u5747\u503c\uff0c\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u7edf\u8ba1\u5206\u6790<\/li>\n<li>\u63d0\u4f9b\u4e86\u7075\u6d3b\u6027<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u8f83\u590d\u6742\uff0c\u521d\u5b66\u8005\u53ef\u80fd\u4e0d\u5bb9\u6613\u7406\u89e3<\/li>\n<\/ul>\n<p><h2>\u516d\u3001\u5904\u7406\u5927\u6570\u636e\u96c6\u548c\u6027\u80fd\u4f18\u5316<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\u65f6\uff0c\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u6216\u7edf\u8ba1\u6a21\u5757\u53ef\u80fd\u4f1a\u9047\u5230\u6027\u80fd\u74f6\u9888\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u4f18\u5316\u7b56\u7565\uff1a<\/p>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>\u5982\u524d\u6240\u8ff0\uff0cNumPy\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u548c\u6570\u5b66\u51fd\u6570\uff0c\u9002\u5408\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>large_numbers = np.random.rand(1000000)<\/p>\n<p>average = np.mean(large_numbers)<\/p>\n<p>print(f&quot;The average is: {average}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>np.random.rand()<\/code>\u751f\u6210\u4e86\u4e00\u4e2a\u5305\u542b\u4e00\u767e\u4e07\u4e2a\u968f\u673a\u6570\u7684\u6570\u7ec4\uff0c\u5e76\u4f7f\u7528<code>np.mean()<\/code>\u51fd\u6570\u8ba1\u7b97\u5e73\u5747\u503c\u3002NumPy\u5e93\u7684\u9ad8\u6548\u5b9e\u73b0\u4f7f\u5f97\u8ba1\u7b97\u901f\u5ea6\u975e\u5e38\u5feb\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u5206\u5757\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u8d85\u5927\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u8003\u8651\u5c06\u6570\u636e\u5206\u6210\u591a\u4e2a\u5757\uff0c\u9010\u5757\u5904\u7406\u5e76\u7d2f\u52a0\u7ed3\u679c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def chunked_average(numbers, chunk_size):<\/p>\n<p>    total_sum = 0<\/p>\n<p>    total_count = 0<\/p>\n<p>    for i in range(0, len(numbers), chunk_size):<\/p>\n<p>        chunk = numbers[i:i + chunk_size]<\/p>\n<p>        total_sum += sum(chunk)<\/p>\n<p>        total_count += len(chunk)<\/p>\n<p>    return total_sum \/ total_count<\/p>\n<p>large_numbers = [i for i in range(1000000)]<\/p>\n<p>average = chunked_average(large_numbers, 10000)<\/p>\n<p>print(f&quot;The average is: {average}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>chunked_average()<\/code>\uff0c\u6bcf\u6b21\u5904\u7406\u4e00\u4e2a\u5927\u5c0f\u4e3a<code>chunk_size<\/code>\u7684\u5757\uff0c\u7d2f\u52a0\u603b\u548c\u548c\u5143\u7d20\u4e2a\u6570\uff0c\u6700\u540e\u8ba1\u7b97\u5e73\u5747\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5730\u964d\u4f4e\u5185\u5b58\u4f7f\u7528\u5e76\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u4f7f\u7528\u5e76\u884c\u8ba1\u7b97<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6781\u5927\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5e76\u884c\u8ba1\u7b97\u52a0\u901f\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Python\u7684\u591a\u5904\u7406\u6a21\u5757<code>multiprocessing<\/code>\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing as mp<\/p>\n<p>def partial_sum(numbers):<\/p>\n<p>    return sum(numbers), len(numbers)<\/p>\n<p>large_numbers = [i for i in range(1000000)]<\/p>\n<p>chunk_size = 10000<\/p>\n<p>with mp.Pool(processes=4) as pool:<\/p>\n<p>    results = pool.map(partial_sum, [large_numbers[i:i + chunk_size] for i in range(0, len(large_numbers), chunk_size)])<\/p>\n<p>total_sum = sum(result[0] for result in results)<\/p>\n<p>total_count = sum(result[1] for result in results)<\/p>\n<p>average = total_sum \/ total_count<\/p>\n<p>print(f&quot;The average is: {average}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>multiprocessing.Pool<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u56db\u4e2a\u8fdb\u7a0b\u7684\u8fdb\u7a0b\u6c60\uff0c\u5e76\u4f7f\u7528<code>pool.map()<\/code>\u5c06\u90e8\u5206\u548c\u8ba1\u7b97\u5206\u914d\u7ed9\u591a\u4e2a\u8fdb\u7a0b\u5e76\u884c\u6267\u884c\u3002\u6700\u540e\uff0c\u7d2f\u52a0\u6240\u6709\u90e8\u5206\u548c\u548c\u5143\u7d20\u4e2a\u6570\uff0c\u8ba1\u7b97\u5e76\u8fd4\u56de\u5e73\u5747\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u63d0\u9ad8\u8ba1\u7b97\u901f\u5ea6\uff0c\u9002\u5408\u5904\u7406\u8d85\u5927\u6570\u636e\u96c6<\/li>\n<li>\u5145\u5206\u5229\u7528\u591a\u6838\u5904\u7406\u5668\u7684\u8ba1\u7b97\u80fd\u529b<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u590d\u6742\u5ea6\u8f83\u9ad8\uff0c\u521d\u5b66\u8005\u53ef\u80fd\u4e0d\u5bb9\u6613\u7406\u89e3<\/li>\n<li>\u9700\u8981\u8003\u8651\u8fdb\u7a0b\u95f4\u901a\u4fe1\u548c\u540c\u6b65\u95ee\u9898<\/li>\n<\/ul>\n<p><h2>\u4e03\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\u6c42\u5e73\u5747\u503c\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u7edf\u8ba1\u6a21\u5757\u548cNumPy\u5e93\u7b49\u3002\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6570\u636e\u96c6\u7684\u5927\u5c0f\u548c\u5177\u4f53\u9700\u6c42\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u5c0f\u6570\u636e\u96c6\uff0c\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u6216\u7edf\u8ba1\u6a21\u5757\u662f\u6700\u65b9\u4fbf\u7684\u9009\u62e9\uff1b\u5bf9\u4e8e\u5927\u6570\u636e\u96c6\uff0c\u4f7f\u7528NumPy\u5e93\u6216\u5206\u5757\u5904\u7406\u3001\u5e76\u884c\u8ba1\u7b97\u7b49\u65b9\u6cd5\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u4f18\u5316\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u8ba1\u7b97\u5e73\u5747\u503c\u5e76\u6ee1\u8db3\u5404\u79cd\u5e94\u7528\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u591a\u4e2a\u6570\u503c\u7684\u5e73\u5747\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>sum()<\/code>\u548c<code>len()<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u4e00\u7ec4\u6570\u503c\u7684\u5e73\u5747\u503c\u3002\u9996\u5148\uff0c\u5c06\u6570\u503c\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u7136\u540e\u901a\u8fc7<code>sum()<\/code>\u51fd\u6570\u6c42\u548c\uff0c\u63a5\u7740\u7528<code>len()<\/code>\u51fd\u6570\u83b7\u53d6\u6570\u503c\u7684\u6570\u91cf\uff0c\u6700\u540e\u5c06\u4e24\u8005\u76f8\u9664\u5373\u53ef\u5f97\u5230\u5e73\u5747\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">numbers = [10, 20, 30, 40, 50]\naverage = sum(numbers) \/ len(numbers)\nprint(average)  # \u8f93\u51fa\uff1a30.0\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u662f\u5426\u6709\u4e13\u95e8\u7684\u5e93\u6765\u8ba1\u7b97\u5e73\u5747\u503c\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u7684<code>statistics<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a<code>mean()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u8ba1\u7b97\u4e00\u7ec4\u6570\u503c\u7684\u5e73\u5747\u503c\u3002\u4f7f\u7528\u65f6\uff0c\u53ea\u9700\u5bfc\u5165\u8be5\u5e93\uff0c\u7136\u540e\u8c03\u7528<code>mean()<\/code>\u51fd\u6570\u4f20\u5165\u6570\u503c\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import statistics\n\nnumbers = [10, 20, 30, 40, 50]\naverage = statistics.mean(numbers)\nprint(average)  # \u8f93\u51fa\uff1a30\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\u6613\u8bfb\uff0c\u8fd8\u80fd\u5904\u7406\u66f4\u591a\u7edf\u8ba1\u529f\u80fd\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u5927\u6570\u636e\u65f6\uff0c\u5982\u4f55\u9ad8\u6548\u8ba1\u7b97\u5e73\u5747\u503c\uff1f<\/strong><br \/>\u5bf9\u4e8e\u5927\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528NumPy\u5e93\uff0c\u5176\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u8ba1\u7b97\u529f\u80fd\u3002\u4f7f\u7528NumPy\u7684<code>mean()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u91cf\u6570\u503c\u65f6\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\nnumbers = np.array([10, 20, 30, 40, 50])\naverage = np.mean(numbers)\nprint(average)  # \u8f93\u51fa\uff1a30.0\n<\/code><\/pre>\n<p>NumPy\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u5e95\u5c42\u4f18\u5316\uff0c\u4f7f\u5176\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u901f\u5ea6\u66f4\u5feb\uff0c\u5185\u5b58\u5360\u7528\u66f4\u4f4e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u6c42\u5e73\u5747\u503c\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u4f7f\u7528\u7edf\u8ba1\u6a21\u5757\u3001\u4f7f\u7528NumPy\u5e93\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u662f\u6700\u5e38\u89c1\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1087963,"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\/1087956"}],"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=1087956"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1087956\/revisions"}],"predecessor-version":[{"id":1087967,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1087956\/revisions\/1087967"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1087963"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1087956"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1087956"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1087956"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}