{"id":1085070,"date":"2025-01-08T13:12:47","date_gmt":"2025-01-08T05:12:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1085070.html"},"modified":"2025-01-08T13:12:49","modified_gmt":"2025-01-08T05:12:49","slug":"python%e5%a6%82%e4%bd%95%e8%87%aa%e5%ae%9a%e4%b9%89%e5%ad%97%e5%85%b8%e5%b9%b6%e4%b8%94%e5%af%bc%e5%87%ba-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1085070.html","title":{"rendered":"python\u5982\u4f55\u81ea\u5b9a\u4e49\u5b57\u5178\u5e76\u4e14\u5bfc\u51fa"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24195128\/21e02a0b-06bd-4bad-9977-f77fef627ed4.webp\" alt=\"python\u5982\u4f55\u81ea\u5b9a\u4e49\u5b57\u5178\u5e76\u4e14\u5bfc\u51fa\" \/><\/p>\n<p><p> <strong>Python\u81ea\u5b9a\u4e49\u5b57\u5178\u5e76\u4e14\u5bfc\u51fa\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u521b\u5efa\u5b57\u5178\u3001\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9\u3001\u4f7f\u7528json\u6a21\u5757\u5bfc\u51fa\u5b57\u5178\u4e3aJSON\u6587\u4ef6\u3002<\/strong>\u5176\u4e2d\uff0cjson\u6a21\u5757\u662fPython\u4e2d\u7528\u4e8e\u7f16\u7801\u548c\u89e3\u7801JSON\u6570\u636e\u7684\u6807\u51c6\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u65b9\u5f0f\u5c06Python\u5bf9\u8c61\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u5e76\u5199\u5165\u6587\u4ef6\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e9b\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u521b\u5efa\u81ea\u5b9a\u4e49\u5b57\u5178<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u81ea\u5b9a\u4e49\u5b57\u5178\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u82b1\u62ec\u53f7 <code>{}<\/code> \u6216\u8005 <code>dict()<\/code> \u6784\u9020\u51fd\u6570\u6765\u521b\u5efa\u5b57\u5178\u3002\u5b57\u5178\u662f\u952e\u503c\u5bf9\u7684\u96c6\u5408\uff0c\u952e\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u800c\u503c\u53ef\u4ee5\u91cd\u590d\u3002\u4ee5\u4e0b\u662f\u4e24\u79cd\u521b\u5efa\u5b57\u5178\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u82b1\u62ec\u53f7\u521b\u5efa\u5b57\u5178<\/p>\n<p>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<h2><strong>\u4f7f\u7528 dict() \u6784\u9020\u51fd\u6570\u521b\u5efa\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = dict(name=&#39;Alice&#39;, age=25, city=&#39;New York&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9<\/p>\n<\/p>\n<p><p>\u4e00\u65e6\u5b57\u5178\u521b\u5efa\u5b8c\u6210\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u65b0\u952e\u503c\u5bf9\u8d4b\u503c\u7ed9\u5b57\u5178\u6765\u6dfb\u52a0\u65b0\u5143\u7d20\u3002\u5982\u679c\u952e\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u4f1a\u66f4\u65b0\u5bf9\u5e94\u7684\u503c\u3002\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u6216\u66f4\u65b0\u952e\u503c\u5bf9\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict[&#39;em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l&#39;] = &#39;alice@example.com&#39;  # \u6dfb\u52a0\u65b0\u952e\u503c\u5bf9<\/p>\n<p>my_dict[&#39;age&#39;] = 26  # \u66f4\u65b0\u5df2\u6709\u952e\u7684\u503c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5bfc\u51fa\u5b57\u5178\u4e3aJSON\u6587\u4ef6<\/p>\n<\/p>\n<p><p>Python\u7684json\u6a21\u5757\u63d0\u4f9b\u4e86\u5c06\u5b57\u5178\u5bfc\u51fa\u4e3aJSON\u6587\u4ef6\u7684\u7b80\u5355\u65b9\u6cd5\u3002\u4f7f\u7528 <code>json.dump()<\/code> \u51fd\u6570\u53ef\u4ee5\u5c06\u5b57\u5178\u5199\u5165\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5bfc\u51fa\u5b57\u5178\u4e3aJSON\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u5b9a\u4e49\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = {<\/p>\n<p>    &#39;name&#39;: &#39;Alice&#39;,<\/p>\n<p>    &#39;age&#39;: 26,<\/p>\n<p>    &#39;city&#39;: &#39;New York&#39;,<\/p>\n<p>    &#39;email&#39;: &#39;alice@example.com&#39;<\/p>\n<p>}<\/p>\n<h2><strong>\u5c06\u5b57\u5178\u5bfc\u51fa\u4e3aJSON\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.json&#39;, &#39;w&#39;) as json_file:<\/p>\n<p>    json.dump(my_dict, json_file, indent=4)  # indent\u53c2\u6570\u7528\u4e8e\u7f8e\u5316\u8f93\u51fa<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u5b57\u5178\u7684\u57fa\u672c\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u662fPython\u4e2d\u4e00\u79cd\u91cd\u8981\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5177\u6709\u9ad8\u6548\u7684\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5b57\u5178\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5b58\u50a8\u548c\u7ba1\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u521b\u5efa\uff0c\u9664\u4e86\u524d\u9762\u63d0\u5230\u7684\u82b1\u62ec\u53f7\u548c <code>dict()<\/code> \u6784\u9020\u51fd\u6570\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528 <code>fromkeys()<\/code> \u65b9\u6cd5\u521b\u5efa\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528 fromkeys() \u65b9\u6cd5\u521b\u5efa\u5b57\u5178<\/p>\n<p>keys = [&#39;name&#39;, &#39;age&#39;, &#39;city&#39;]<\/p>\n<p>default_value = None<\/p>\n<p>my_dict = dict.fromkeys(keys, default_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bbf\u95ee\u5b57\u5178\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u952e\u6765\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c\u3002\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u5f15\u53d1 <code>KeyError<\/code> \u5f02\u5e38\u3002\u4e3a\u4e86\u907f\u514d\u5f02\u5e38\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>get()<\/code> \u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u76f4\u63a5\u8bbf\u95ee\u5b57\u5178\u5143\u7d20<\/p>\n<p>print(my_dict[&#39;name&#39;])<\/p>\n<h2><strong>\u4f7f\u7528 get() \u65b9\u6cd5\u8bbf\u95ee\u5b57\u5178\u5143\u7d20<\/strong><\/h2>\n<p>print(my_dict.get(&#39;name&#39;))<\/p>\n<p>print(my_dict.get(&#39;email&#39;, &#39;Not Found&#39;))  # \u6307\u5b9a\u9ed8\u8ba4\u503c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b57\u5178\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u521b\u5efa\u3001\u8bbf\u95ee\u3001\u6dfb\u52a0\u548c\u5220\u9664\u64cd\u4f5c\uff0c\u5b57\u5178\u8fd8\u652f\u6301\u4e00\u4e9b\u9ad8\u7ea7\u64cd\u4f5c\uff0c\u5982\u5b57\u5178\u5408\u5e76\u3001\u904d\u5386\u3001\u6392\u5e8f\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b57\u5178\u5408\u5e76<\/h4>\n<\/p>\n<p><p>\u5728Python 3.9\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>|<\/code> \u8fd0\u7b97\u7b26\u5408\u5e76\u4e24\u4e2a\u5b57\u5178\u3002\u5bf9\u4e8e\u8f83\u65e9\u7248\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>update()<\/code> \u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dict1 = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p>dict2 = {&#39;b&#39;: 3, &#39;c&#39;: 4}<\/p>\n<h2><strong>\u4f7f\u7528 | \u8fd0\u7b97\u7b26\u5408\u5e76\u5b57\u5178\uff08Python 3.9+\uff09<\/strong><\/h2>\n<p>merged_dict = dict1 | dict2<\/p>\n<p>print(merged_dict)  # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;b&#39;: 3, &#39;c&#39;: 4}<\/p>\n<h2><strong>\u4f7f\u7528 update() \u65b9\u6cd5\u5408\u5e76\u5b57\u5178<\/strong><\/h2>\n<p>dict1.update(dict2)<\/p>\n<p>print(dict1)  # \u8f93\u51fa: {&#39;a&#39;: 1, &#39;b&#39;: 3, &#39;c&#39;: 4}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u904d\u5386\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>items()<\/code> \u65b9\u6cd5\u904d\u5386\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\uff0c\u4f7f\u7528 <code>keys()<\/code> \u65b9\u6cd5\u904d\u5386\u952e\uff0c\u4f7f\u7528 <code>values()<\/code> \u65b9\u6cd5\u904d\u5386\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u904d\u5386\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9<\/p>\n<p>for key, value in my_dict.items():<\/p>\n<p>    print(f&#39;{key}: {value}&#39;)<\/p>\n<h2><strong>\u904d\u5386\u5b57\u5178\u4e2d\u7684\u952e<\/strong><\/h2>\n<p>for key in my_dict.keys():<\/p>\n<p>    print(key)<\/p>\n<h2><strong>\u904d\u5386\u5b57\u5178\u4e2d\u7684\u503c<\/strong><\/h2>\n<p>for value in my_dict.values():<\/p>\n<p>    print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u6392\u5e8f\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>sorted()<\/code> \u51fd\u6570\u5bf9\u5b57\u5178\u6309\u952e\u6216\u6309\u503c\u8fdb\u884c\u6392\u5e8f\u3002\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u4e00\u4e2a\u6392\u5e8f\u540e\u7684\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6309\u952e\u6392\u5e8f<\/p>\n<p>sorted_by_key = dict(sorted(my_dict.items()))<\/p>\n<p>print(sorted_by_key)<\/p>\n<h2><strong>\u6309\u503c\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_by_value = dict(sorted(my_dict.items(), key=lambda item: item[1]))<\/p>\n<p>print(sorted_by_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001JSON\u6a21\u5757\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u6613\u4e8e\u4eba\u9605\u8bfb\u548c\u7f16\u5199\uff0c\u4e5f\u6613\u4e8e\u673a\u5668\u89e3\u6790\u548c\u751f\u6210\u3002Python\u7684json\u6a21\u5757\u63d0\u4f9b\u4e86\u7f16\u7801\u548c\u89e3\u7801JSON\u6570\u636e\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5c06\u5b57\u5178\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>json.dumps()<\/code> \u51fd\u6570\u5c06\u5b57\u5178\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>json_str = json.dumps(my_dict, indent=4)<\/p>\n<p>print(json_str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5c06JSON\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>json.loads()<\/code> \u51fd\u6570\u5c06JSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">json_str = &#39;{&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 26, &quot;city&quot;: &quot;New York&quot;, &quot;email&quot;: &quot;alice@example.com&quot;}&#39;<\/p>\n<p>my_dict = json.loads(json_str)<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u8bfb\u53d6\u548c\u5199\u5165JSON\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u5c06\u5b57\u5178\u8f6c\u6362\u4e3aJSON\u5b57\u7b26\u4e32\uff0cjson\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u5c06\u5b57\u5178\u5199\u5165\u6587\u4ef6\u548c\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6JSON\u6570\u636e\u7684\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u5b57\u5178\u5199\u5165JSON\u6587\u4ef6<\/p>\n<p>with open(&#39;output.json&#39;, &#39;w&#39;) as json_file:<\/p>\n<p>    json.dump(my_dict, json_file, indent=4)<\/p>\n<h2><strong>\u4eceJSON\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5b57\u5178<\/strong><\/h2>\n<p>with open(&#39;output.json&#39;, &#39;r&#39;) as json_file:<\/p>\n<p>    my_dict = json.load(json_file)<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5b57\u5178\u7684\u5b9e\u9645\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u573a\u666f\uff0c\u5982\u914d\u7f6e\u7ba1\u7406\u3001\u6570\u636e\u805a\u5408\u3001\u7f13\u5b58\u7b49\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5b9e\u9645\u5e94\u7528\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u914d\u7f6e\u7ba1\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u901a\u5e38\u9700\u8981\u7ba1\u7406\u5927\u91cf\u7684\u914d\u7f6e\u4fe1\u606f\u3002\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u5b58\u50a8\u548c\u7ba1\u7406\u8fd9\u4e9b\u914d\u7f6e\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">config = {<\/p>\n<p>    &#39;database&#39;: {<\/p>\n<p>        &#39;host&#39;: &#39;localhost&#39;,<\/p>\n<p>        &#39;port&#39;: 3306,<\/p>\n<p>        &#39;user&#39;: &#39;root&#39;,<\/p>\n<p>        &#39;password&#39;: &#39;password&#39;<\/p>\n<p>    },<\/p>\n<p>    &#39;logging&#39;: {<\/p>\n<p>        &#39;level&#39;: &#39;INFO&#39;,<\/p>\n<p>        &#39;file&#39;: &#39;\/var\/log\/app.log&#39;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<h2><strong>\u8bbf\u95ee\u914d\u7f6e\u4fe1\u606f<\/strong><\/h2>\n<p>db_host = config[&#39;database&#39;][&#39;host&#39;]<\/p>\n<p>log_level = config[&#39;logging&#39;][&#39;level&#39;]<\/p>\n<p>print(f&#39;Database host: {db_host}&#39;)<\/p>\n<p>print(f&#39;Logging level: {log_level}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6570\u636e\u805a\u5408<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u7528\u4e8e\u805a\u5408\u548c\u7edf\u8ba1\u6570\u636e\u3002\u4f8b\u5982\uff0c\u7edf\u8ba1\u4e00\u6bb5\u6587\u672c\u4e2d\u6bcf\u4e2a\u5355\u8bcd\u51fa\u73b0\u7684\u6b21\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;this is a sample text with several words this is a sample text with some different words&quot;<\/p>\n<p>word_counts = {}<\/p>\n<h2><strong>\u7edf\u8ba1\u5355\u8bcd\u51fa\u73b0\u6b21\u6570<\/strong><\/h2>\n<p>for word in text.split():<\/p>\n<p>    word_counts[word] = word_counts.get(word, 0) + 1<\/p>\n<h2><strong>\u8f93\u51fa\u7edf\u8ba1\u7ed3\u679c<\/strong><\/h2>\n<p>print(word_counts)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u7f13\u5b58<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u7f13\u5b58\u8ba1\u7b97\u7ed3\u679c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5b57\u5178\u7f13\u5b58\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u8ba1\u7b97\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cache = {}<\/p>\n<p>def fibonacci(n):<\/p>\n<p>    if n in cache:<\/p>\n<p>        return cache[n]<\/p>\n<p>    if n &lt;= 1:<\/p>\n<p>        return n<\/p>\n<p>    result = fibonacci(n - 1) + fibonacci(n - 2)<\/p>\n<p>    cache[n] = result<\/p>\n<p>    return result<\/p>\n<h2><strong>\u8ba1\u7b97\u6590\u6ce2\u90a3\u5951\u6570\u5217<\/strong><\/h2>\n<p>for i in range(10):<\/p>\n<p>    print(f&#39;Fibonacci({i}) = {fibonacci(i)}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u4e86Python\u4e2d\u81ea\u5b9a\u4e49\u5b57\u5178\u5e76\u5bfc\u51fa\u7684\u5404\u79cd\u65b9\u6cd5\u548c\u5e94\u7528\u3002\u5b57\u5178\u4f5c\u4e3a\u4e00\u79cd\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u573a\u666f\u3002\u901a\u8fc7\u4f7f\u7528json\u6a21\u5757\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u5b57\u5178\u5bfc\u51fa\u4e3aJSON\u6587\u4ef6\uff0c\u4ece\u800c\u5b9e\u73b0\u6570\u636e\u7684\u6301\u4e45\u5316\u548c\u4ea4\u6362\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u5e76\u6fc0\u53d1\u4f60\u5728Python\u7f16\u7a0b\u4e2d\u7684\u66f4\u591a\u521b\u610f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u81ea\u5b9a\u4e49\u5b57\u5178\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u521b\u5efa\u81ea\u5b9a\u4e49\u5b57\u5178\u76f8\u5bf9\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528\u5927\u62ec\u53f7 <code>{}<\/code> \u6765\u5b9a\u4e49\u5b57\u5178\uff0c\u6216\u4f7f\u7528 <code>dict()<\/code> \u51fd\u6570\u3002\u5b57\u5178\u7531\u952e\u503c\u5bf9\u7ec4\u6210\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u6240\u9700\u7684\u952e\u548c\u503c\u6765\u521b\u5efa\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}\n<\/code><\/pre>\n<p>\u6216\u8005\u4f7f\u7528 <code>dict()<\/code> \u51fd\u6570\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = dict(name=&#39;Alice&#39;, age=25, city=&#39;New York&#39;)\n<\/code><\/pre>\n<p>\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u5f0f\uff0c\u90fd\u53ef\u4ee5\u8f7b\u677e\u6784\u5efa\u4e00\u4e2a\u5305\u542b\u5404\u79cd\u4fe1\u606f\u7684\u5b57\u5178\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u81ea\u5b9a\u4e49\u5b57\u5178\u5bfc\u51fa\u4e3a\u6587\u4ef6\uff1f<\/strong><br \/>\u5bfc\u51fa\u5b57\u5178\u7684\u5e38\u7528\u65b9\u6cd5\u662f\u5c06\u5176\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\uff0c\u5e76\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528 <code>json<\/code> \u6a21\u5757\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5bfc\u5165\u8be5\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528 <code>json.dump()<\/code> \u5c06\u5b57\u5178\u5199\u5165\u6587\u4ef6\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import json\n\nmy_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}\nwith open(&#39;output.json&#39;, &#39;w&#39;) as json_file:\n    json.dump(my_dict, json_file)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06\u5b57\u5178\u5bfc\u51fa\u4e3a <code>output.json<\/code> \u6587\u4ef6\uff0c\u65b9\u4fbf\u540e\u7eed\u4f7f\u7528\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06\u5b57\u5178\u5bfc\u51fa\u4e3a\u5176\u4ed6\u683c\u5f0f\uff1f<\/strong><br \/>\u9664\u4e86JSON\uff0c\u5b57\u5178\u8fd8\u53ef\u4ee5\u5bfc\u51fa\u4e3aCSV\u6216Excel\u7b49\u683c\u5f0f\u3002\u5bf9\u4e8eCSV\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>csv<\/code> \u6a21\u5757\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import csv\n\nmy_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}\nwith open(&#39;output.csv&#39;, &#39;w&#39;, newline=&#39;&#39;) as csv_file:\n    writer = csv.writer(csv_file)\n    writer.writerow(my_dict.keys())  # \u5199\u5165\u8868\u5934\n    writer.writerow(my_dict.values())  # \u5199\u5165\u503c\n<\/code><\/pre>\n<p>\u5bf9\u4e8eExcel\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>pandas<\/code> \u5e93\uff0c\u5177\u4f53\u65b9\u6cd5\u662f\u5c06\u5b57\u5178\u8f6c\u6362\u4e3aDataFrame\u540e\u518d\u5bfc\u51fa\u3002\u8fd9\u6837\uff0c\u5b57\u5178\u7684\u5bfc\u51fa\u65b9\u5f0f\u5c31\u66f4\u52a0\u7075\u6d3b\u591a\u6837\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u81ea\u5b9a\u4e49\u5b57\u5178\u5e76\u4e14\u5bfc\u51fa\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u521b\u5efa\u5b57\u5178\u3001\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9\u3001\u4f7f\u7528json\u6a21\u5757\u5bfc\u51fa\u5b57\u5178\u4e3a [&hellip;]","protected":false},"author":3,"featured_media":1085076,"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\/1085070"}],"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=1085070"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1085070\/revisions"}],"predecessor-version":[{"id":1085078,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1085070\/revisions\/1085078"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1085076"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1085070"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1085070"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1085070"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}