{"id":1187032,"date":"2025-01-15T20:00:19","date_gmt":"2025-01-15T12:00:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1187032.html"},"modified":"2025-01-15T20:00:23","modified_gmt":"2025-01-15T12:00:23","slug":"python%e5%a6%82%e4%bd%95%e5%86%99%e8%ae%a1%e7%ae%97%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1187032.html","title":{"rendered":"python\u5982\u4f55\u5199\u8ba1\u7b97\u7a0b\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25135647\/936cc3e6-7b6d-4904-aa64-b079a09774b9.webp\" alt=\"python\u5982\u4f55\u5199\u8ba1\u7b97\u7a0b\u5e8f\" \/><\/p>\n<p><p> <strong>Python\u5199\u8ba1\u7b97\u7a0b\u5e8f\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\u9009\u62e9\u5408\u9002\u7684\u5e93\u3001\u7f16\u5199\u51fd\u6570\u3001\u5904\u7406\u8f93\u5165\u8f93\u51fa\u3001\u8c03\u8bd5\u548c\u4f18\u5316\u7b49\u3002\u4f7f\u7528Python\u7f16\u5199\u8ba1\u7b97\u7a0b\u5e8f\u65f6\uff0c\u9996\u5148\u8981\u660e\u786e\u8ba1\u7b97\u4efb\u52a1\u7684\u5177\u4f53\u9700\u6c42\uff0c\u7136\u540e\u9009\u62e9\u5408\u9002\u7684\u5e93\u5982NumPy\u3001SciPy\u3001Pandas\u7b49\uff0c\u7f16\u5199\u51fd\u6570\u5b9e\u73b0\u5177\u4f53\u7684\u8ba1\u7b97\u903b\u8f91\uff0c\u5e76\u8fdb\u884c\u8c03\u8bd5\u548c\u4f18\u5316\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u9009\u62e9\u5408\u9002\u7684\u5e93<\/strong>\u662f\u7f16\u5199Python\u8ba1\u7b97\u7a0b\u5e8f\u7684\u7b2c\u4e00\u6b65\u3002Python\u6709\u8bb8\u591a\u5f3a\u5927\u7684\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u5404\u79cd\u590d\u6742\u7684\u8ba1\u7b97\u4efb\u52a1\u3002\u4f8b\u5982\uff0cNumPy\u7528\u4e8e\u9ad8\u6548\u5730\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\uff0cSciPy\u7528\u4e8e\u79d1\u5b66\u548c\u5de5\u7a0b\u8ba1\u7b97\uff0cPandas\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\uff0cMatplotlib\u7528\u4e8e\u7ed8\u56fe\u548c\u53ef\u89c6\u5316\u3002\u9009\u62e9\u9002\u5408\u7684\u5e93\u53ef\u4ee5\u5927\u5927\u7b80\u5316\u6211\u4eec\u7684\u5de5\u4f5c\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u9009\u62e9\u5408\u9002\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u5e93\u662f\u7f16\u5199Python\u8ba1\u7b97\u7a0b\u5e8f\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u4e0d\u540c\u7684\u5e93\u63d0\u4f9b\u4e86\u4e0d\u540c\u7684\u529f\u80fd\u548c\u4f18\u52bf\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u8ba1\u7b97\u7a0b\u5e8f\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684Python\u5e93\u53ca\u5176\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001NumPy<\/h4>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u6700\u91cd\u8981\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\u4e4b\u4e00\uff0c\u4e3b\u8981\u7528\u4e8e\u9ad8\u6548\u5730\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u3002\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u529f\u80fd\u5f3a\u5927\u7684\u5de5\u5177\u548c\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5404\u79cd\u6570\u5b66\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6570\u7ec4<\/strong><\/h2>\n<p>a = np.array([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u8ba1\u7b97\u6570\u7ec4\u7684\u5e73\u5747\u503c<\/strong><\/h2>\n<p>mean = np.mean(a)<\/p>\n<p>print(&quot;Mean:&quot;, mean)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001SciPy<\/h4>\n<\/p>\n<p><p>SciPy\u662f\u57fa\u4e8eNumPy\u6784\u5efa\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u79d1\u5b66\u548c\u5de5\u7a0b\u8ba1\u7b97\u7684\u5de5\u5177\u548c\u51fd\u6570\u3002\u5b83\u5305\u62ec\u4f18\u5316\u3001\u63d2\u503c\u3001\u79ef\u5206\u3001\u7ebf\u6027\u4ee3\u6570\u3001\u7edf\u8ba1\u548c\u56fe\u50cf\u5904\u7406\u7b49\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.integrate import quad<\/p>\n<h2><strong>\u5b9a\u4e49\u88ab\u79ef\u51fd\u6570<\/strong><\/h2>\n<p>def integrand(x):<\/p>\n<p>    return x2<\/p>\n<h2><strong>\u8ba1\u7b97\u79ef\u5206<\/strong><\/h2>\n<p>result, error = quad(integrand, 0, 1)<\/p>\n<p>print(&quot;Integral:&quot;, result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001Pandas<\/h4>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u5e93\uff0c\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\u3002\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u636e\u64cd\u4f5c\u5de5\u5177\uff0c\u5982DataFrame\u548cSeries\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u3001\u5206\u6790\u548c\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aDataFrame<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;], &#39;Age&#39;: [24, 27, 22]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u8ba1\u7b97\u5e73\u5747\u5e74\u9f84<\/strong><\/h2>\n<p>mean_age = df[&#39;Age&#39;].mean()<\/p>\n<p>print(&quot;Mean Age:&quot;, mean_age)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001Matplotlib<\/h4>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u7528\u4e8e\u7ed8\u56fe\u548c\u6570\u636e\u53ef\u89c6\u5316\u7684\u5e93\uff0c\u53ef\u4ee5\u751f\u6210\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\uff0c\u5982\u6298\u7ebf\u56fe\u3001\u6563\u70b9\u56fe\u3001\u67f1\u72b6\u56fe\u7b49\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u76f4\u89c2\u5730\u5c55\u793a\u6570\u636e\u548c\u8ba1\u7b97\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [1, 4, 9, 16, 25]<\/p>\n<h2><strong>\u7ed8\u5236\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<p>plt.xlabel(&#39;X&#39;)<\/p>\n<p>plt.ylabel(&#39;Y&#39;)<\/p>\n<p>plt.title(&#39;Line Plot&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u7f16\u5199\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u7f16\u5199\u51fd\u6570\u662f\u5b9e\u73b0\u8ba1\u7b97\u903b\u8f91\u7684\u5173\u952e\u6b65\u9aa4\u3002\u51fd\u6570\u53ef\u4ee5\u5c06\u590d\u6742\u7684\u8ba1\u7b97\u4efb\u52a1\u5206\u89e3\u4e3a\u591a\u4e2a\u7b80\u5355\u7684\u6b65\u9aa4\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\u548c\u6613\u4e8e\u7ef4\u62a4\u3002\u5728\u7f16\u5199\u51fd\u6570\u65f6\uff0c\u6211\u4eec\u9700\u8981\u8003\u8651\u51fd\u6570\u7684\u8f93\u5165\u3001\u8f93\u51fa\u548c\u5177\u4f53\u7684\u8ba1\u7b97\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\u6765\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u3002\u51fd\u6570\u53ef\u4ee5\u6709\u4e00\u4e2a\u6216\u591a\u4e2a\u53c2\u6570\uff0c\u5e76\u4e14\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u6216\u591a\u4e2a\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>result = add(3, 5)<\/p>\n<p>print(&quot;Sum:&quot;, result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u8f93\u5165<\/h4>\n<\/p>\n<p><p>\u51fd\u6570\u7684\u8f93\u5165\u53ef\u4ee5\u662f\u7b80\u5355\u7684\u6570\u503c\u3001\u5b57\u7b26\u4e32\u6216\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5217\u8868\u3001\u5b57\u5178\u548c\u6570\u7ec4\u3002\u5728\u7f16\u5199\u51fd\u6570\u65f6\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u8f93\u5165\u7684\u6570\u636e\u7c7b\u578b\u548c\u683c\u5f0f\u662f\u6b63\u786e\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_mean(numbers):<\/p>\n<p>    if not numbers:<\/p>\n<p>        return 0<\/p>\n<p>    return sum(numbers) \/ len(numbers)<\/p>\n<p>mean = calculate_mean([1, 2, 3, 4, 5])<\/p>\n<p>print(&quot;Mean:&quot;, mean)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5b9e\u73b0\u8ba1\u7b97\u903b\u8f91<\/h4>\n<\/p>\n<p><p>\u51fd\u6570\u7684\u6838\u5fc3\u662f\u5b9e\u73b0\u5177\u4f53\u7684\u8ba1\u7b97\u903b\u8f91\u3002\u5728\u7f16\u5199\u8ba1\u7b97\u903b\u8f91\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5404\u79cd\u6570\u5b66\u8fd0\u7b97\u7b26\u3001\u5185\u7f6e\u51fd\u6570\u548c\u5e93\u51fd\u6570\u6765\u5b9e\u73b0\u590d\u6742\u7684\u8ba1\u7b97\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def factorial(n):<\/p>\n<p>    if n == 0:<\/p>\n<p>        return 1<\/p>\n<p>    else:<\/p>\n<p>        return n * factorial(n - 1)<\/p>\n<p>result = factorial(5)<\/p>\n<p>print(&quot;Factorial:&quot;, result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5904\u7406\u8f93\u5165\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>\u5904\u7406\u8f93\u5165\u8f93\u51fa\u662f\u8ba1\u7b97\u7a0b\u5e8f\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002\u6211\u4eec\u9700\u8981\u4ece\u7528\u6237\u6216\u6587\u4ef6\u4e2d\u83b7\u53d6\u8f93\u5165\u6570\u636e\uff0c\u5e76\u5c06\u8ba1\u7b97\u7ed3\u679c\u8f93\u51fa\u5230\u5c4f\u5e55\u6216\u6587\u4ef6\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u8f93\u5165\u8f93\u51fa\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u83b7\u53d6\u7528\u6237\u8f93\u5165<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u4ece\u7528\u6237\u83b7\u53d6\u8f93\u5165\u6570\u636e\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>input()<\/code>\u51fd\u6570\u8fd4\u56de\u7684\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5982\u679c\u9700\u8981\u5176\u4ed6\u6570\u636e\u7c7b\u578b\uff0c\u9700\u8981\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u7528\u6237\u8f93\u5165<\/p>\n<p>number = int(input(&quot;Enter a number: &quot;))<\/p>\n<h2><strong>\u8ba1\u7b97\u5e73\u65b9<\/strong><\/h2>\n<p>square = number  2<\/p>\n<p>print(&quot;Square:&quot;, square)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bfb\u53d6\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u3002\u5e38\u89c1\u7684\u6587\u4ef6\u8bfb\u53d6\u6a21\u5f0f\u5305\u62ec\u6587\u672c\u6a21\u5f0f\u548c\u4e8c\u8fdb\u5236\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6\u6587\u4ef6<\/p>\n<p>with open(&#39;data.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = file.read()<\/p>\n<p>print(&quot;File Content:&quot;, data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5199\u5165\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002\u5e38\u89c1\u7684\u6587\u4ef6\u5199\u5165\u6a21\u5f0f\u5305\u62ec\u8986\u76d6\u6a21\u5f0f\u548c\u8ffd\u52a0\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5199\u5165\u6587\u4ef6<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&quot;Hello, World!&quot;)<\/p>\n<p>print(&quot;Data written to file.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u8c03\u8bd5\u548c\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u8c03\u8bd5\u548c\u4f18\u5316\u662f\u786e\u4fdd\u8ba1\u7b97\u7a0b\u5e8f\u6b63\u786e\u6027\u548c\u6548\u7387\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177\u548c\u65b9\u6cd5\u6765\u67e5\u627e\u548c\u4fee\u590d\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\uff0c\u5e76\u901a\u8fc7\u4f18\u5316\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u6765\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177<\/h4>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u8c03\u8bd5\u5de5\u5177\uff0c\u5982\u5185\u7f6e\u7684<code>pdb<\/code>\u6a21\u5757\u548c\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u4e2d\u7684\u8c03\u8bd5\u529f\u80fd\u3002\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bbe\u7f6e\u65ad\u70b9\u3001\u5355\u6b65\u6267\u884c\u548c\u67e5\u770b\u53d8\u91cf\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pdb<\/p>\n<p>def add(a, b):<\/p>\n<p>    pdb.set_trace()  # \u8bbe\u7f6e\u65ad\u70b9<\/p>\n<p>    return a + b<\/p>\n<p>result = add(3, 5)<\/p>\n<p>print(&quot;Sum:&quot;, result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f18\u5316\u7b97\u6cd5<\/h4>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8ba1\u7b97\u7a0b\u5e8f\u7684\u6027\u80fd\u3002\u5728\u7f16\u5199\u8ba1\u7b97\u7a0b\u5e8f\u65f6\uff0c\u6211\u4eec\u9700\u8981\u8003\u8651\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u9009\u62e9\u9ad8\u6548\u7684\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5<\/p>\n<p>def quicksort(arr):<\/p>\n<p>    if len(arr) &lt;= 1:<\/p>\n<p>        return arr<\/p>\n<p>    pivot = arr[len(arr) \/\/ 2]<\/p>\n<p>    left = [x for x in arr if x &lt; pivot]<\/p>\n<p>    middle = [x for x in arr if x == pivot]<\/p>\n<p>    right = [x for x in arr if x &gt; pivot]<\/p>\n<p>    return quicksort(left) + middle + quicksort(right)<\/p>\n<p>arr = [3, 6, 8, 10, 1, 2, 1]<\/p>\n<p>sorted_arr = quicksort(arr)<\/p>\n<p>print(&quot;Sorted Array:&quot;, sorted_arr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4f7f\u7528\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8ba1\u7b97\u7a0b\u5e8f\u7684\u6027\u80fd\u3002Python\u4e2d\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u5305\u62ec\u5217\u8868\u3001\u5b57\u5178\u3001\u96c6\u5408\u548c\u5143\u7ec4\u3002\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5b57\u5178\u8fdb\u884c\u5feb\u901f\u67e5\u627e<\/p>\n<p>data = {&#39;Alice&#39;: 24, &#39;Bob&#39;: 27, &#39;Charlie&#39;: 22}<\/p>\n<p>name = &#39;Bob&#39;<\/p>\n<p>age = data.get(name, &#39;Not found&#39;)<\/p>\n<p>print(f&quot;{name}&#39;s age:&quot;, age)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6848\u4f8b\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u7f16\u5199Python\u8ba1\u7b97\u7a0b\u5e8f\uff0c\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u5177\u4f53\u7684\u6848\u4f8b\u6765\u8fdb\u884c\u5206\u6790\u548c\u5b9e\u73b0\u3002\u5047\u8bbe\u6211\u4eec\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f\u6765\u8ba1\u7b97\u4e00\u7ec4\u6570\u636e\u7684\u7edf\u8ba1\u4fe1\u606f\uff0c\u5305\u62ec\u5e73\u5747\u503c\u3001\u6807\u51c6\u5dee\u3001\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u9700\u6c42\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u7a0b\u5e8f\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u660e\u786e\u5177\u4f53\u7684\u9700\u6c42\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u7ec4\u6570\u636e\uff0c\u6211\u4eec\u9700\u8981\u8ba1\u7b97\u4ee5\u4e0b\u7edf\u8ba1\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5e73\u5747\u503c\uff1a\u6570\u636e\u7684\u5e73\u5747\u503c\u3002<\/li>\n<li>\u6807\u51c6\u5dee\uff1a\u6570\u636e\u7684\u6807\u51c6\u5dee\uff0c\u8868\u793a\u6570\u636e\u7684\u79bb\u6563\u7a0b\u5ea6\u3002<\/li>\n<li>\u6700\u5927\u503c\uff1a\u6570\u636e\u4e2d\u7684\u6700\u5927\u503c\u3002<\/li>\n<li>\u6700\u5c0f\u503c\uff1a\u6570\u636e\u4e2d\u7684\u6700\u5c0f\u503c\u3002<\/li>\n<\/ul>\n<p><h4>2\u3001\u9009\u62e9\u5408\u9002\u7684\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u6848\u4f8b\u4e2d\uff0c\u6211\u4eec\u9009\u62e9\u4f7f\u7528NumPy\u5e93\u6765\u8fdb\u884c\u8ba1\u7b97\uff0c\u56e0\u4e3aNumPy\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u548c\u4e30\u5bcc\u7684\u6570\u5b66\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5404\u79cd\u7edf\u8ba1\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u7f16\u5199\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u7f16\u5199\u4e00\u4e2a\u51fd\u6570<code>calculate_statistics<\/code>\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u6570\u7ec4\u4f5c\u4e3a\u8f93\u5165\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5b57\u5178\uff0c\u5305\u542b\u5e73\u5747\u503c\u3001\u6807\u51c6\u5dee\u3001\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_statistics(data):<\/p>\n<p>    statistics = {<\/p>\n<p>        &#39;mean&#39;: np.mean(data),<\/p>\n<p>        &#39;std_dev&#39;: np.std(data),<\/p>\n<p>        &#39;max&#39;: np.max(data),<\/p>\n<p>        &#39;min&#39;: np.min(data)<\/p>\n<p>    }<\/p>\n<p>    return statistics<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u5904\u7406\u8f93\u5165\u8f93\u51fa<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u7f16\u5199\u4e00\u4e2a\u4e3b\u51fd\u6570<code>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n<\/code>\uff0c\u4ece\u7528\u6237\u83b7\u53d6\u8f93\u5165\u6570\u636e\uff0c\u5e76\u8c03\u7528<code>calculate_statistics<\/code>\u51fd\u6570\u8fdb\u884c\u8ba1\u7b97\uff0c\u6700\u540e\u5c06\u7ed3\u679c\u8f93\u51fa\u5230\u5c4f\u5e55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def main():<\/p>\n<p>    # \u83b7\u53d6\u7528\u6237\u8f93\u5165<\/p>\n<p>    data = list(map(float, input(&quot;Enter numbers separated by spaces: &quot;).split()))<\/p>\n<p>    # \u8ba1\u7b97\u7edf\u8ba1\u4fe1\u606f<\/p>\n<p>    statistics = calculate_statistics(np.array(data))<\/p>\n<p>    # \u8f93\u51fa\u7ed3\u679c<\/p>\n<p>    print(&quot;Statistics:&quot;)<\/p>\n<p>    print(f&quot;Mean: {statistics[&#39;mean&#39;]}&quot;)<\/p>\n<p>    print(f&quot;Standard Deviation: {statistics[&#39;std_dev&#39;]}&quot;)<\/p>\n<p>    print(f&quot;Max: {statistics[&#39;max&#39;]}&quot;)<\/p>\n<p>    print(f&quot;Min: {statistics[&#39;min&#39;]}&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5\u3001\u8c03\u8bd5\u548c\u4f18\u5316<\/h4>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u5b8c\u7a0b\u5e8f\u540e\uff0c\u6211\u4eec\u9700\u8981\u8fdb\u884c\u8c03\u8bd5\u548c\u4f18\u5316\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u6d4b\u8bd5\u6570\u636e\u6765\u9a8c\u8bc1\u7a0b\u5e8f\u7684\u6b63\u786e\u6027\uff0c\u5e76\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177\u67e5\u627e\u548c\u4fee\u590d\u53ef\u80fd\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6d4b\u8bd5\u6570\u636e<\/p>\n<p>test_data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<h2><strong>\u8ba1\u7b97\u7edf\u8ba1\u4fe1\u606f<\/strong><\/h2>\n<p>statistics = calculate_statistics(np.array(test_data))<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>print(&quot;Test Statistics:&quot;)<\/p>\n<p>print(f&quot;Mean: {statistics[&#39;mean&#39;]}&quot;)<\/p>\n<p>print(f&quot;Standard Deviation: {statistics[&#39;std_dev&#39;]}&quot;)<\/p>\n<p>print(f&quot;Max: {statistics[&#39;max&#39;]}&quot;)<\/p>\n<p>print(f&quot;Min: {statistics[&#39;min&#39;]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u5b8c\u6574\u7684Python\u8ba1\u7b97\u7a0b\u5e8f\u3002\u4ece\u9009\u62e9\u5408\u9002\u7684\u5e93\u3001\u7f16\u5199\u51fd\u6570\u3001\u5904\u7406\u8f93\u5165\u8f93\u51fa\uff0c\u5230\u8c03\u8bd5\u548c\u4f18\u5316\uff0c\u6bcf\u4e2a\u6b65\u9aa4\u90fd\u9700\u8981\u6211\u4eec\u4ed4\u7ec6\u601d\u8003\u548c\u5b9e\u73b0\u3002\u7f16\u5199\u8ba1\u7b97\u7a0b\u5e8f\u4e0d\u4ec5\u9700\u8981\u638c\u63e1Python\u7684\u57fa\u7840\u8bed\u6cd5\u548c\u5e93\u7684\u4f7f\u7528\uff0c\u8fd8\u9700\u8981\u5177\u5907\u4e00\u5b9a\u7684\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u77e5\u8bc6\u3002\u901a\u8fc7\u4e0d\u65ad\u7684\u5b9e\u8df5\u548c\u5b66\u4e60\uff0c\u6211\u4eec\u53ef\u4ee5\u63d0\u9ad8\u7f16\u5199\u8ba1\u7b97\u7a0b\u5e8f\u7684\u80fd\u529b\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u7ed3\u6765\u8bf4\uff0c\u7f16\u5199Python\u8ba1\u7b97\u7a0b\u5e8f\u5305\u62ec\u9009\u62e9\u5408\u9002\u7684\u5e93\u3001\u7f16\u5199\u51fd\u6570\u3001\u5904\u7406\u8f93\u5165\u8f93\u51fa\u3001\u8c03\u8bd5\u548c\u4f18\u5316\u7b49\u6b65\u9aa4\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u53ef\u4ee5\u7b80\u5316\u5de5\u4f5c\uff0c\u63d0\u9ad8\u6548\u7387\uff1b\u7f16\u5199\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u5177\u4f53\u7684\u8ba1\u7b97\u903b\u8f91\uff1b\u5904\u7406\u8f93\u5165\u8f93\u51fa\u53ef\u4ee5\u83b7\u53d6\u548c\u8f93\u51fa\u6570\u636e\uff1b\u8c03\u8bd5\u548c\u4f18\u5316\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u6b63\u786e\u6027\u548c\u6548\u7387\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u548c\u65b9\u6cd5\uff0c\u5b9e\u73b0\u5404\u79cd\u590d\u6742\u7684\u8ba1\u7b97\u4efb\u52a1\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684Python\u5e93\u8fdb\u884c\u8ba1\u7b97\u7a0b\u5e8f\u7684\u5f00\u53d1\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u8bb8\u591a\u5f3a\u5927\u7684\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8fdb\u884c\u8ba1\u7b97\u7a0b\u5e8f\u7684\u5f00\u53d1\u3002\u5e38\u89c1\u7684\u5e93\u5305\u62ecNumPy\u3001Pandas\u548cSciPy\u3002NumPy\u4e13\u6ce8\u4e8e\u9ad8\u6548\u7684\u6570\u7ec4\u8fd0\u7b97\uff0cPandas\u5219\u63d0\u4f9b\u4e86\u6570\u636e\u5904\u7406\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u800cSciPy\u5219\u9002\u5408\u66f4\u590d\u6742\u7684\u6570\u5b66\u8ba1\u7b97\u3002\u6839\u636e\u4f60\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u7a0b\u5e8f\u6027\u80fd\u3002<\/p>\n<p><strong>Python\u8ba1\u7b97\u7a0b\u5e8f\u7684\u57fa\u672c\u7ed3\u6784\u662f\u4ec0\u4e48\u6837\u7684\uff1f<\/strong><br \/>\u4e00\u4e2a\u5178\u578b\u7684Python\u8ba1\u7b97\u7a0b\u5e8f\u7ed3\u6784\u901a\u5e38\u5305\u62ec\u5bfc\u5165\u6240\u9700\u7684\u5e93\u3001\u5b9a\u4e49\u51fd\u6570\u4ee5\u53ca\u4e3b\u7a0b\u5e8f\u90e8\u5206\u3002\u5728\u51fd\u6570\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5b9e\u73b0\u5177\u4f53\u7684\u8ba1\u7b97\u903b\u8f91\uff0c\u800c\u4e3b\u7a0b\u5e8f\u5219\u8d1f\u8d23\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\u5e76\u8f93\u51fa\u7ed3\u679c\u3002\u8fd9\u6837\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u4e5f\u65b9\u4fbf\u540e\u671f\u7684\u7ef4\u62a4\u4e0e\u6269\u5c55\u3002<\/p>\n<p><strong>\u5982\u4f55\u8c03\u8bd5\u548c\u4f18\u5316Python\u8ba1\u7b97\u7a0b\u5e8f\uff1f<\/strong><br \/>\u8c03\u8bd5Python\u7a0b\u5e8f\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>print()<\/code>\u51fd\u6570\u6765\u8f93\u51fa\u53d8\u91cf\u7684\u503c\uff0c\u5e2e\u52a9\u8ffd\u8e2a\u4ee3\u7801\u6267\u884c\u7684\u8fc7\u7a0b\u3002\u6b64\u5916\uff0c\u4f7f\u7528Python\u7684\u8c03\u8bd5\u5de5\u5177\u5982<code>pdb<\/code>\u53ef\u4ee5\u8ba9\u4f60\u9010\u884c\u6267\u884c\u4ee3\u7801\uff0c\u65b9\u4fbf\u67e5\u627e\u95ee\u9898\u3002\u4f18\u5316\u65b9\u9762\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u7b97\u6cd5\u3001\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u4ee5\u53ca\u5229\u7528\u5e76\u884c\u8ba1\u7b97\u6765\u63d0\u5347\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5199\u8ba1\u7b97\u7a0b\u5e8f\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\u9009\u62e9\u5408\u9002\u7684\u5e93\u3001\u7f16\u5199\u51fd\u6570\u3001\u5904\u7406\u8f93\u5165\u8f93\u51fa\u3001\u8c03\u8bd5\u548c\u4f18\u5316\u7b49\u3002\u4f7f\u7528Python\u7f16\u5199\u8ba1 [&hellip;]","protected":false},"author":3,"featured_media":1187043,"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\/1187032"}],"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=1187032"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1187032\/revisions"}],"predecessor-version":[{"id":1187049,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1187032\/revisions\/1187049"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1187043"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1187032"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1187032"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1187032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}