{"id":1081344,"date":"2025-01-08T12:38:46","date_gmt":"2025-01-08T04:38:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1081344.html"},"modified":"2025-01-08T12:38:49","modified_gmt":"2025-01-08T04:38:49","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%81%9a%e4%b9%98%e6%b3%95%e8%a1%a8-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1081344.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u505a\u4e58\u6cd5\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183416\/67276aef-88a1-47cb-ab09-a659dfebfdaa.webp\" alt=\"python\u4e2d\u5982\u4f55\u505a\u4e58\u6cd5\u8868\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5236\u4f5c\u4e58\u6cd5\u8868\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u76f4\u89c2\u548c\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5d4c\u5957\u7684for\u5faa\u73af\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u4e0d\u540c\u7684\u65b9\u6cd5\u5728Python\u4e2d\u5236\u4f5c\u4e58\u6cd5\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u5236\u4f5c\u4e58\u6cd5\u8868<\/h3>\n<\/p>\n<p><p>\u5d4c\u5957\u5faa\u73af\u662f\u5236\u4f5c\u4e58\u6cd5\u8868\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7\u4e24\u4e2afor\u5faa\u73af\uff0c\u6211\u4eec\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u7b80\u5355\u7684\u4e58\u6cd5\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def multiplication_table(size):<\/p>\n<p>    for i in range(1, size + 1):<\/p>\n<p>        for j in range(1, size + 1):<\/p>\n<p>            print(f&#39;{i * j:4}&#39;, end=&#39; &#39;)<\/p>\n<p>        print()<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u751f\u6210\u4e58\u6cd5\u8868\uff0csize\u51b3\u5b9a\u4e58\u6cd5\u8868\u7684\u5927\u5c0f\uff0c\u4f8b\u598210<\/strong><\/h2>\n<p>multiplication_table(10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>multiplication_table<\/code>\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570<code>size<\/code>\uff0c\u8868\u793a\u4e58\u6cd5\u8868\u7684\u5927\u5c0f\u3002\u901a\u8fc7\u5d4c\u5957\u7684for\u5faa\u73af\uff0c\u6211\u4eec\u904d\u5386\u6bcf\u4e00\u4e2a\u4e58\u6cd5\u7ec4\u5408\uff0c\u5e76\u4f7f\u7528<code>print<\/code>\u51fd\u6570\u683c\u5f0f\u5316\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5236\u4f5c\u4e58\u6cd5\u8868<\/h3>\n<\/p>\n<p><p>Python\u7684\u5217\u8868\u63a8\u5bfc\u5f0f\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u751f\u6210\u4e58\u6cd5\u8868\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u5982\u5d4c\u5957\u5faa\u73af\u90a3\u4e48\u76f4\u89c2\uff0c\u4f46\u5b83\u66f4\u4e3a\u7b80\u6d01\u548c\u4f18\u96c5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">size = 10<\/p>\n<p>table = [[i * j for j in range(1, size + 1)] for i in range(1, size + 1)]<\/p>\n<p>for row in table:<\/p>\n<p>    print(&#39; &#39;.join(f&#39;{num:4}&#39; for num in row))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e00\u4e2a\u5305\u542b\u4e58\u6cd5\u7ed3\u679c\u7684\u4e8c\u7ef4\u5217\u8868<code>table<\/code>\uff0c\u7136\u540e\u901a\u8fc7\u904d\u5386\u6bcf\u4e00\u884c\u5e76\u683c\u5f0f\u5316\u8f93\u51fa\u7ed3\u679c\uff0c\u751f\u6210\u4e58\u6cd5\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528NumPy\u5236\u4f5c\u4e58\u6cd5\u8868<\/h3>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u52a0\u9ad8\u6548\u5730\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u3002\u901a\u8fc7NumPy\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u7b80\u6d01\u5730\u751f\u6210\u4e58\u6cd5\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def numpy_multiplication_table(size):<\/p>\n<p>    table = np.outer(np.arange(1, size + 1), np.arange(1, size + 1))<\/p>\n<p>    for row in table:<\/p>\n<p>        print(&#39; &#39;.join(f&#39;{num:4}&#39; for num in row))<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u751f\u6210\u4e58\u6cd5\u8868<\/strong><\/h2>\n<p>numpy_multiplication_table(10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>numpy_multiplication_table<\/code>\u51fd\u6570\u4f7f\u7528<code>np.outer<\/code>\u51fd\u6570\u751f\u6210\u4e58\u6cd5\u8868\u3002<code>np.outer<\/code>\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u6570\u7ec4\uff0c\u8fd4\u56de\u5b83\u4eec\u7684\u5916\u79ef\uff0c\u751f\u6210\u4e00\u4e2a\u5305\u542b\u4e58\u6cd5\u7ed3\u679c\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002\u7136\u540e\u6211\u4eec\u901a\u8fc7\u904d\u5386\u6bcf\u4e00\u884c\u5e76\u683c\u5f0f\u5316\u8f93\u51fa\u7ed3\u679c\uff0c\u751f\u6210\u4e58\u6cd5\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Pandas\u5236\u4f5c\u4e58\u6cd5\u8868<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u52a0\u65b9\u4fbf\u5730\u5904\u7406\u548c\u5c55\u793a\u6570\u636e\u3002\u901a\u8fc7Pandas\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u4e3a\u4f18\u96c5\u5730\u751f\u6210\u548c\u5c55\u793a\u4e58\u6cd5\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def pandas_multiplication_table(size):<\/p>\n<p>    index = range(1, size + 1)<\/p>\n<p>    df = pd.DataFrame(index=index, columns=index)<\/p>\n<p>    for i in index:<\/p>\n<p>        for j in index:<\/p>\n<p>            df.loc[i, j] = i * j<\/p>\n<p>    print(df)<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u751f\u6210\u4e58\u6cd5\u8868<\/strong><\/h2>\n<p>pandas_multiplication_table(10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>pandas_multiplication_table<\/code>\u51fd\u6570\u4f7f\u7528Pandas\u751f\u6210\u4e58\u6cd5\u8868\u3002\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u884c\u548c\u5217\u7d22\u5f15\u7684\u7a7aDataFrame\uff0c\u7136\u540e\u901a\u8fc7\u5d4c\u5957\u7684for\u5faa\u73af\u586b\u5145DataFrame\u3002\u6700\u540e\uff0c\u6211\u4eec\u901a\u8fc7<code>print<\/code>\u51fd\u6570\u5c55\u793aDataFrame\uff0c\u751f\u6210\u4e58\u6cd5\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u901a\u8fc7\u51fd\u6570\u53c2\u6570\u5b9a\u5236\u4e58\u6cd5\u8868<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u53c2\u6570\u6765\u5b9a\u5236\u4e58\u6cd5\u8868\uff0c\u4f8b\u5982\u51b3\u5b9a\u4e58\u6cd5\u8868\u7684\u5927\u5c0f\u3001\u662f\u5426\u663e\u793a\u8868\u5934\u3001\u662f\u5426\u5bf9\u9f50\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def custom_multiplication_table(size, show_header=True, align=True):<\/p>\n<p>    if show_header:<\/p>\n<p>        header = &#39; &#39;.join(f&#39;{i:4}&#39; for i in range(1, size + 1))<\/p>\n<p>        print(f&#39;    {header}&#39;)<\/p>\n<p>        print(&#39;   &#39; + &#39;----&#39; * size)<\/p>\n<p>    for i in range(1, size + 1):<\/p>\n<p>        if show_header:<\/p>\n<p>            print(f&#39;{i:2} |&#39;, end=&#39; &#39;)<\/p>\n<p>        for j in range(1, size + 1):<\/p>\n<p>            if align:<\/p>\n<p>                print(f&#39;{i * j:4}&#39;, end=&#39; &#39;)<\/p>\n<p>            else:<\/p>\n<p>                print(i * j, end=&#39; &#39;)<\/p>\n<p>        print()<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u751f\u6210\u5b9a\u5236\u7684\u4e58\u6cd5\u8868<\/strong><\/h2>\n<p>custom_multiplication_table(10, show_header=True, align=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>custom_multiplication_table<\/code>\u51fd\u6570\u63a5\u53d7\u4e09\u4e2a\u53c2\u6570\uff1a<code>size<\/code>\u8868\u793a\u4e58\u6cd5\u8868\u7684\u5927\u5c0f\uff0c<code>show_header<\/code>\u8868\u793a\u662f\u5426\u663e\u793a\u8868\u5934\uff0c<code>align<\/code>\u8868\u793a\u662f\u5426\u5bf9\u9f50\u8f93\u51fa\u7ed3\u679c\u3002\u901a\u8fc7\u4f20\u9012\u4e0d\u540c\u7684\u53c2\u6570\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u751f\u6210\u4e0d\u540c\u6837\u5f0f\u7684\u4e58\u6cd5\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u751f\u6210\u4e58\u6cd5\u8868\u5e76\u4fdd\u5b58\u5230\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u5c06\u4e58\u6cd5\u8868\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u4f8b\u5982\u4fdd\u5b58\u4e3a\u6587\u672c\u6587\u4ef6\u6216\u8005CSV\u6587\u4ef6\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7Python\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><h4>\u4fdd\u5b58\u4e3a\u6587\u672c\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def save_multiplication_table_to_txt(size, filename):<\/p>\n<p>    with open(filename, &#39;w&#39;) as f:<\/p>\n<p>        for i in range(1, size + 1):<\/p>\n<p>            for j in range(1, size + 1):<\/p>\n<p>                f.write(f&#39;{i * j:4} &#39;)<\/p>\n<p>            f.write(&#39;\\n&#39;)<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u751f\u6210\u4e58\u6cd5\u8868\u5e76\u4fdd\u5b58\u5230\u6587\u672c\u6587\u4ef6<\/strong><\/h2>\n<p>save_multiplication_table_to_txt(10, &#39;multiplication_table.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>def save_multiplication_table_to_csv(size, filename):<\/p>\n<p>    with open(filename, &#39;w&#39;, newline=&#39;&#39;) as f:<\/p>\n<p>        writer = csv.writer(f)<\/p>\n<p>        for i in range(1, size + 1):<\/p>\n<p>            row = [i * j for j in range(1, size + 1)]<\/p>\n<p>            writer.writerow(row)<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u751f\u6210\u4e58\u6cd5\u8868\u5e76\u4fdd\u5b58\u5230CSV\u6587\u4ef6<\/strong><\/h2>\n<p>save_multiplication_table_to_csv(10, &#39;multiplication_table.csv&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5206\u522b\u5b9a\u4e49\u4e86\u4e24\u4e2a\u51fd\u6570<code>save_multiplication_table_to_txt<\/code>\u548c<code>save_multiplication_table_to_csv<\/code>\uff0c\u7528\u4e8e\u5c06\u4e58\u6cd5\u8868\u4fdd\u5b58\u5230\u6587\u672c\u6587\u4ef6\u548cCSV\u6587\u4ef6\u4e2d\u3002\u901a\u8fc7\u4f20\u9012\u6587\u4ef6\u540d\u53c2\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u751f\u6210\u7684\u4e58\u6cd5\u8868\u4fdd\u5b58\u5230\u6307\u5b9a\u7684\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u56fe\u5f62\u5316\u5c55\u793a\u4e58\u6cd5\u8868<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u56fe\u5f62\u5316\u5e93\u6765\u5c55\u793a\u4e58\u6cd5\u8868\uff0c\u4f8b\u5982Matplotlib\u3002\u901a\u8fc7\u70ed\u56fe\uff08heatmap\uff09\u7684\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u4e3a\u76f4\u89c2\u5730\u5c55\u793a\u4e58\u6cd5\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import seaborn as sns<\/p>\n<p>import numpy as np<\/p>\n<p>def plot_multiplication_table(size):<\/p>\n<p>    table = np.outer(np.arange(1, size + 1), np.arange(1, size + 1))<\/p>\n<p>    plt.figure(figsize=(10, 8))<\/p>\n<p>    sns.heatmap(table, annot=True, fmt=&#39;d&#39;, cmap=&#39;Blues&#39;, cbar=False, xticklabels=range(1, size + 1), yticklabels=range(1, size + 1))<\/p>\n<p>    plt.title(&#39;Multiplication Table&#39;)<\/p>\n<p>    plt.xlabel(&#39;Multiplicand&#39;)<\/p>\n<p>    plt.ylabel(&#39;Multiplier&#39;)<\/p>\n<p>    plt.show()<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u751f\u6210\u5e76\u5c55\u793a\u4e58\u6cd5\u8868\u70ed\u56fe<\/strong><\/h2>\n<p>plot_multiplication_table(10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>plot_multiplication_table<\/code>\u51fd\u6570\u4f7f\u7528Matplotlib\u548cSeaborn\u751f\u6210\u4e58\u6cd5\u8868\u7684\u70ed\u56fe\u3002\u6211\u4eec\u9996\u5148\u4f7f\u7528<code>np.outer<\/code>\u751f\u6210\u5305\u542b\u4e58\u6cd5\u7ed3\u679c\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u7136\u540e\u901a\u8fc7Seaborn\u7684<code>heatmap<\/code>\u51fd\u6570\u751f\u6210\u70ed\u56fe\uff0c\u5e76\u901a\u8fc7Matplotlib\u8fdb\u884c\u663e\u793a\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5236\u4f5c\u4e58\u6cd5\u8868\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u5305\u62ec\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001NumPy\u3001Pandas\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u901a\u8fc7\u4f20\u9012\u53c2\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u5236\u4e58\u6cd5\u8868\u7684\u6837\u5f0f\uff1b\u901a\u8fc7\u6587\u4ef6\u64cd\u4f5c\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e58\u6cd5\u8868\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff1b\u901a\u8fc7\u56fe\u5f62\u5316\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u4e3a\u76f4\u89c2\u5730\u5c55\u793a\u4e58\u6cd5\u8868\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5236\u4f5c\u548c\u5c55\u793a\u4e58\u6cd5\u8868\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u4e86\u89e3\u548c\u5b9e\u73b0\u4e58\u6cd5\u8868\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e58\u6cd5\u8868\u7684\u57fa\u672c\u7ed3\u6784\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u521b\u5efa\u4e58\u6cd5\u8868\uff0c\u9996\u5148\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u3002\u5916\u5c42\u5faa\u73af\u7528\u4e8e\u904d\u5386\u884c\uff081\u523010\uff09\uff0c\u5185\u5c42\u5faa\u73af\u7528\u4e8e\u904d\u5386\u5217\uff081\u523010\uff09\uff0c\u901a\u8fc7\u8ba1\u7b97\u884c\u548c\u5217\u7684\u4e58\u79ef\u6765\u8f93\u51fa\u7ed3\u679c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">for i in range(1, 11):\n    for j in range(1, 11):\n        print(f&quot;{i * j:4}&quot;, end=&quot;&quot;)\n    print()\n<\/code><\/pre>\n<p><strong>\u6211\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u65b9\u6cd5\u6765\u683c\u5f0f\u5316\u4e58\u6cd5\u8868\u7684\u8f93\u51fa\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u683c\u5f0f\u5316\u8f93\u51fa\u3002\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5982<code>f-string<\/code>\u3001<code>str.format()<\/code>\u6216\u8005<code>%<\/code>\u683c\u5f0f\u5316\u3002\u901a\u8fc7\u8c03\u6574\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u4e58\u6cd5\u8868\u7684\u5bf9\u9f50\u66f4\u52a0\u7f8e\u89c2\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>f&quot;{i * j:4}&quot;<\/code>\u6765\u786e\u4fdd\u6bcf\u4e2a\u6570\u5b57\u5360\u7528\u56db\u4e2a\u5b57\u7b26\u7684\u5bbd\u5ea6\uff0c\u4ece\u800c\u4f7f\u5217\u5bf9\u9f50\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u4e58\u6cd5\u8868\u8f93\u51fa\u5230\u6587\u4ef6\u4e2d\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\u5c06\u4e58\u6cd5\u8868\u8f93\u51fa\u5230\u6587\u672c\u6587\u4ef6\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528<code>with open(&quot;filename.txt&quot;, &quot;w&quot;) as file:<\/code>\u6765\u6253\u5f00\u6587\u4ef6\u5e76\u5199\u5165\u5185\u5bb9\u3002\u6bcf\u6b21\u6253\u5370\u4e58\u6cd5\u8868\u7684\u4e00\u884c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>file.write()<\/code>\u65b9\u6cd5\u5c06\u884c\u5199\u5165\u6587\u4ef6\uff0c\u800c\u4e0d\u662f\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&quot;multiplication_table.txt&quot;, &quot;w&quot;) as file:\n    for i in range(1, 11):\n        line = &quot;&quot;\n        for j in range(1, 11):\n            line += f&quot;{i * j:4}&quot;\n        file.write(line + &quot;\\n&quot;)\n<\/code><\/pre>\n<p><strong>\u6709\u6ca1\u6709\u73b0\u6210\u7684\u5e93\u53ef\u4ee5\u751f\u6210\u4e58\u6cd5\u8868\uff1f<\/strong><br \/>\u867d\u7136Python\u7684\u6807\u51c6\u5e93\u6ca1\u6709\u4e13\u95e8\u7528\u4e8e\u751f\u6210\u4e58\u6cd5\u8868\u7684\u5e93\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u3002NumPy\u53ef\u4ee5\u8f7b\u677e\u5730\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\uff0c\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u6765\u751f\u6210\u4e58\u6cd5\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\ntable = np.arange(1, 11).reshape(10, 1) * np.arange(1, 11)\nprint(table)\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u4e58\u6cd5\u8868\u7684NumPy\u6570\u7ec4\uff0c\u5e76\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u6253\u5370\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5236\u4f5c\u4e58\u6cd5\u8868\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u3002 \u5176\u4e2d\uff0c\u6700\u76f4\u89c2\u548c\u5e38 [&hellip;]","protected":false},"author":3,"featured_media":1081355,"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\/1081344"}],"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=1081344"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1081344\/revisions"}],"predecessor-version":[{"id":1081360,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1081344\/revisions\/1081360"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1081355"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1081344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1081344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1081344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}