{"id":1149086,"date":"2025-01-13T16:45:18","date_gmt":"2025-01-13T08:45:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1149086.html"},"modified":"2025-01-13T16:45:20","modified_gmt":"2025-01-13T08:45:20","slug":"%e5%ad%97%e5%85%b8%e5%a6%82%e4%bd%95%e5%af%bc%e5%85%a5python-append","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1149086.html","title":{"rendered":"\u5b57\u5178\u5982\u4f55\u5bfc\u5165python append"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25175805\/999c7e52-69e2-42b7-96c4-e840e867573d.webp\" alt=\"\u5b57\u5178\u5982\u4f55\u5bfc\u5165python append\" \/><\/p>\n<p><p> <strong>\u5b57\u5178\u5bfc\u5165Python\u5e76\u4f7f\u7528append\u7684\u65b9\u6cd5\u6709\u591a\u79cd\u65b9\u5f0f\uff0c\u5305\u62ec\u76f4\u63a5\u5728\u4ee3\u7801\u4e2d\u58f0\u660e\u5b57\u5178\u3001\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5b57\u5178\u6570\u636e\u3001\u4f7f\u7528\u5e93\u8fdb\u884c\u6570\u636e\u5bfc\u5165\u7b49\u3002<\/strong> \u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e38\u89c1\u7684\u65b9\u5f0f\u662f\u76f4\u63a5\u5728\u4ee3\u7801\u4e2d\u58f0\u660e\u5b57\u5178\u4ee5\u53ca\u4eceJSON\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5b57\u5178\u6570\u636e\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e24\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u76f4\u63a5\u5728\u4ee3\u7801\u4e2d\u58f0\u660e\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5728Python\u4ee3\u7801\u4e2d\u76f4\u63a5\u58f0\u660e\u4e00\u4e2a\u5b57\u5178\uff0c\u5e76\u4f7f\u7528append\u65b9\u6cd5\u6765\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u58f0\u660e\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>\u4f7f\u7528append\u65b9\u6cd5\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>my_dict[&#39;city&#39;] = &#39;New York&#39;<\/p>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u58f0\u660e\u4e86\u4e00\u4e2a\u5b57\u5178 <code>my_dict<\/code>\uff0c\u7136\u540e\u901a\u8fc7\u8d4b\u503c\u7684\u65b9\u5f0f\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002<strong>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b57\u5178\u672c\u8eab\u6ca1\u6709append\u65b9\u6cd5\uff0c\u56e0\u4e3aappend\u662f\u5217\u8868\u7684\u65b9\u6cd5\uff0c\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u5143\u7d20\u662f\u901a\u8fc7\u8d4b\u503c\u64cd\u4f5c\u6765\u5b9e\u73b0\u7684\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4eceJSON\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5b57\u5178\u6570\u636e\u901a\u5e38\u5b58\u50a8\u5728\u6587\u4ef6\u4e2d\uff0c\u4f8b\u5982JSON\u6587\u4ef6\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u5e93<code>json<\/code>\u6765\u8bfb\u53d6\u548c\u5199\u5165JSON\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5b57\u5178<\/strong><\/h2>\n<p>with open(&#39;data.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    my_dict = json.load(file)<\/p>\n<h2><strong>\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>my_dict[&#39;city&#39;] = &#39;New York&#39;<\/p>\n<h2><strong>\u5c06\u66f4\u65b0\u540e\u7684\u5b57\u5178\u5199\u56de\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;data.json&#39;, &#39;w&#39;) as file:<\/p>\n<p>    json.dump(my_dict, file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4ece\u540d\u4e3a<code>data.json<\/code>\u7684\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5b57\u5178\u6570\u636e\uff0c\u7136\u540e\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\uff0c\u6700\u540e\u5c06\u66f4\u65b0\u540e\u7684\u5b57\u5178\u5199\u56de\u5230\u6587\u4ef6\u4e2d\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u5904\u7406\u8f83\u5927\u89c4\u6a21\u7684\u6570\u636e\uff0c\u5e76\u4e14\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u7684\u6301\u4e45\u5316\u5b58\u50a8\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5e93\u8fdb\u884c\u6570\u636e\u5bfc\u5165<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u4e24\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u6765\u5bfc\u5165\u548c\u5904\u7406\u5b57\u5178\u6570\u636e\u3002\u4f8b\u5982\uff0c<code>pandas<\/code>\u5e93\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\uff0c\u5c24\u5176\u662f\u8868\u683c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u4eceCSV\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u5e76\u8f6c\u6362\u4e3a\u5b57\u5178<\/strong><\/h2>\n<p>df = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<p>my_dict = df.to_dict()<\/p>\n<h2><strong>\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>my_dict[&#39;new_column&#39;] = &#39;new_value&#39;<\/p>\n<h2><strong>\u5c06\u66f4\u65b0\u540e\u7684\u5b57\u5178\u8f6c\u6362\u4e3aDataFrame\u5e76\u5199\u56deCSV\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.DataFrame.from_dict(my_dict)<\/p>\n<p>df.to_csv(&#39;updated_data.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>pandas<\/code>\u5e93\u4eceCSV\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u5178\uff0c\u7136\u540e\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\uff0c\u6700\u540e\u5c06\u66f4\u65b0\u540e\u7684\u5b57\u5178\u8f6c\u6362\u4e3aDataFrame\u5e76\u5199\u56de\u5230CSV\u6587\u4ef6\u4e2d\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u5904\u7406\u8868\u683c\u6570\u636e\uff0c\u5e76\u4e14\u53ef\u4ee5\u5229\u7528<code>pandas<\/code>\u5e93\u7684\u5f3a\u5927\u529f\u80fd\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5728\u4f01\u4e1a\u7ea7\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u901a\u5e38\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>sqlite3<\/code>\u5e93\u6216\u8005\u5176\u4ed6\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff08\u5982<code>pymysql<\/code>\u3001<code>psycopg2<\/code>\u7b49\uff09\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u5b57\u5178\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5\u5230SQLite\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u6267\u884c\u67e5\u8be2\u5e76\u83b7\u53d6\u6570\u636e<\/strong><\/h2>\n<p>cursor.execute(&quot;SELECT * FROM users&quot;)<\/p>\n<p>rows = cursor.fetchall()<\/p>\n<h2><strong>\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = {row[0]: {&#39;name&#39;: row[1], &#39;age&#39;: row[2]} for row in rows}<\/p>\n<h2><strong>\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>my_dict[&#39;new_user&#39;] = {&#39;name&#39;: &#39;Bob&#39;, &#39;age&#39;: 30}<\/p>\n<h2><strong>\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u8fde\u63a5\u5230SQLite\u6570\u636e\u5e93\u5e76\u6267\u884c\u67e5\u8be2\uff0c\u7136\u540e\u5c06\u67e5\u8be2\u7ed3\u679c\u8f6c\u6362\u4e3a\u5b57\u5178\uff0c\u6700\u540e\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u5904\u7406\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\uff0c\u5e76\u4e14\u53ef\u4ee5\u5229\u7528SQL\u8bed\u8a00\u8fdb\u884c\u590d\u6742\u7684\u67e5\u8be2\u548c\u6570\u636e\u64cd\u4f5c\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528API\u83b7\u53d6\u5b57\u5178\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u73b0\u4ee3\u5e94\u7528\u4e2d\uff0c\u5f88\u591a\u6570\u636e\u53ef\u4ee5\u901a\u8fc7API\u83b7\u53d6\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>requests<\/code>\u5e93\u6765\u53d1\u9001HTTP\u8bf7\u6c42\u5e76\u83b7\u53d6\u6570\u636e\uff0c\u7136\u540e\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u53d1\u9001HTTP\u8bf7\u6c42\u83b7\u53d6\u6570\u636e<\/strong><\/h2>\n<p>response = requests.get(&#39;https:\/\/api.example.com\/data&#39;)<\/p>\n<p>data = response.json()<\/p>\n<h2><strong>\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = {item[&#39;id&#39;]: item for item in data}<\/p>\n<h2><strong>\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>my_dict[&#39;new_item&#39;] = {&#39;name&#39;: &#39;Charlie&#39;, &#39;age&#39;: 28}<\/p>\n<h2><strong>\u6253\u5370\u5b57\u5178<\/strong><\/h2>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>requests<\/code>\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\u5e76\u83b7\u53d6JSON\u6570\u636e\uff0c\u7136\u540e\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u5178\uff0c\u6700\u540e\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u5904\u7406\u5728\u7ebf\u6570\u636e\uff0c\u5e76\u4e14\u53ef\u4ee5\u65b9\u4fbf\u5730\u4e0e\u5404\u79cdAPI\u8fdb\u884c\u96c6\u6210\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5b57\u5178\u7684\u6df1\u5ea6\u5408\u5e76<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5c06\u591a\u4e2a\u5b57\u5178\u5408\u5e76\u4e3a\u4e00\u4e2a\u5b57\u5178\u3002Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u5b57\u5178\u7684\u5408\u5e76\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e24\u4e2a\u5b57\u5178<\/p>\n<p>dict1 = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<p>dict2 = {&#39;city&#39;: &#39;New York&#39;, &#39;country&#39;: &#39;USA&#39;}<\/p>\n<h2><strong>\u4f7f\u7528update\u65b9\u6cd5\u5408\u5e76\u5b57\u5178<\/strong><\/h2>\n<p>dict1.update(dict2)<\/p>\n<h2><strong>\u6253\u5370\u5408\u5e76\u540e\u7684\u5b57\u5178<\/strong><\/h2>\n<p>print(dict1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>update<\/code>\u65b9\u6cd5\u5c06<code>dict2<\/code>\u4e2d\u7684\u952e\u503c\u5bf9\u5408\u5e76\u5230<code>dict1<\/code>\u4e2d\u3002<strong>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u4e24\u4e2a\u5b57\u5178\u4e2d\u6709\u76f8\u540c\u7684\u952e\uff0c<code>update<\/code>\u65b9\u6cd5\u4f1a\u8986\u76d6\u539f\u5b57\u5178\u4e2d\u7684\u503c\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5b57\u5178\u7684\u6df1\u5ea6\u62f7\u8d1d<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u5b57\u5178\u8fdb\u884c\u6df1\u5ea6\u62f7\u8d1d\uff0c\u4ee5\u907f\u514d\u4fee\u6539\u539f\u5b57\u5178\u7684\u6570\u636e\u3002Python\u63d0\u4f9b\u4e86<code>copy<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u6df1\u5ea6\u62f7\u8d1d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import copy<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5d4c\u5957\u5b57\u5178<\/strong><\/h2>\n<p>original_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;det<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ls&#39;: {&#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}}<\/p>\n<h2><strong>\u4f7f\u7528deepcopy\u65b9\u6cd5\u8fdb\u884c\u6df1\u5ea6\u62f7\u8d1d<\/strong><\/h2>\n<p>copied_dict = copy.deepcopy(original_dict)<\/p>\n<h2><strong>\u4fee\u6539\u62f7\u8d1d\u540e\u7684\u5b57\u5178<\/strong><\/h2>\n<p>copied_dict[&#39;details&#39;][&#39;age&#39;] = 30<\/p>\n<h2><strong>\u6253\u5370\u539f\u5b57\u5178\u548c\u62f7\u8d1d\u540e\u7684\u5b57\u5178<\/strong><\/h2>\n<p>print(&quot;Original dictionary:&quot;, original_dict)<\/p>\n<p>print(&quot;Copied dictionary:&quot;, copied_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>copy<\/code>\u6a21\u5757\u7684<code>deepcopy<\/code>\u65b9\u6cd5\u5bf9\u5d4c\u5957\u5b57\u5178\u8fdb\u884c\u6df1\u5ea6\u62f7\u8d1d\uff0c\u7136\u540e\u4fee\u6539\u62f7\u8d1d\u540e\u7684\u5b57\u5178\u3002<strong>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6df1\u5ea6\u62f7\u8d1d\u4f1a\u9012\u5f52\u5730\u590d\u5236\u6240\u6709\u5d4c\u5957\u7684\u5bf9\u8c61\uff0c\u56e0\u6b64\u53ef\u4ee5\u907f\u514d\u4fee\u6539\u62f7\u8d1d\u540e\u7684\u5b57\u5178\u65f6\u5f71\u54cd\u539f\u5b57\u5178\u7684\u6570\u636e\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5b57\u5178\u7684\u952e\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u5b57\u5178\u7684\u952e\u8fdb\u884c\u6392\u5e8f\u4ee5\u4fbf\u4e8e\u5c55\u793a\u6216\u8fdb\u4e00\u6b65\u5904\u7406\u3002Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u5b57\u5178\u952e\u7684\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>my_dict = {&#39;b&#39;: 2, &#39;a&#39;: 1, &#39;c&#39;: 3}<\/p>\n<h2><strong>\u4f7f\u7528sorted\u65b9\u6cd5\u5bf9\u5b57\u5178\u7684\u952e\u8fdb\u884c\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_dict = {key: my_dict[key] for key in sorted(my_dict)}<\/p>\n<h2><strong>\u6253\u5370\u6392\u5e8f\u540e\u7684\u5b57\u5178<\/strong><\/h2>\n<p>print(sorted_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>sorted<\/code>\u65b9\u6cd5\u5bf9\u5b57\u5178\u7684\u952e\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u5178<code>sorted_dict<\/code>\u3002<strong>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>sorted<\/code>\u65b9\u6cd5\u8fd4\u56de\u7684\u662f\u6392\u5e8f\u540e\u7684\u952e\u5217\u8868\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u6765\u521b\u5efa\u6392\u5e8f\u540e\u7684\u5b57\u5178\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5b57\u5178\u7684\u503c\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5bf9\u5b57\u5178\u7684\u952e\u8fdb\u884c\u6392\u5e8f\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5bf9\u5b57\u5178\u7684\u503c\u8fdb\u884c\u6392\u5e8f\u3002Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u5b57\u5178\u503c\u7684\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>my_dict = {&#39;b&#39;: 2, &#39;a&#39;: 1, &#39;c&#39;: 3}<\/p>\n<h2><strong>\u4f7f\u7528sorted\u65b9\u6cd5\u5bf9\u5b57\u5178\u7684\u503c\u8fdb\u884c\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_dict = {key: value for key, value in sorted(my_dict.items(), key=lambda item: item[1])}<\/p>\n<h2><strong>\u6253\u5370\u6392\u5e8f\u540e\u7684\u5b57\u5178<\/strong><\/h2>\n<p>print(sorted_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>sorted<\/code>\u65b9\u6cd5\u5bf9\u5b57\u5178\u7684\u503c\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u5178<code>sorted_dict<\/code>\u3002<strong>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6211\u4eec\u4f7f\u7528<code>items<\/code>\u65b9\u6cd5\u83b7\u53d6\u5b57\u5178\u7684\u952e\u503c\u5bf9\u5217\u8868\uff0c\u5e76\u4f7f\u7528<code>lambda<\/code>\u51fd\u6570\u6307\u5b9a\u6309\u503c\u6392\u5e8f\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u5341\u3001\u5b57\u5178\u7684\u8fc7\u6ee4<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u6839\u636e\u67d0\u4e9b\u6761\u4ef6\u5bf9\u5b57\u5178\u8fdb\u884c\u8fc7\u6ee4\u3002Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u5b57\u5178\u7684\u8fc7\u6ee4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;, &#39;country&#39;: &#39;USA&#39;}<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u5bf9\u5b57\u5178\u8fdb\u884c\u8fc7\u6ee4<\/strong><\/h2>\n<p>filtered_dict = {key: value for key, value in my_dict.items() if key != &#39;age&#39;}<\/p>\n<h2><strong>\u6253\u5370\u8fc7\u6ee4\u540e\u7684\u5b57\u5178<\/strong><\/h2>\n<p>print(filtered_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u5bf9\u5b57\u5178\u8fdb\u884c\u8fc7\u6ee4\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u5178<code>filtered_dict<\/code>\u3002<strong>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6211\u4eec\u5728\u63a8\u5bfc\u5f0f\u4e2d\u6307\u5b9a\u4e86\u8fc7\u6ee4\u6761\u4ef6\uff0c\u5373\u952e\u4e0d\u7b49\u4e8e<code>age<\/code>\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u5b57\u5178\u7684\u904d\u5386<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u904d\u5386\u5b57\u5178\u7684\u952e\u503c\u5bf9\u3002Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u904d\u5386\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\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\u7528for\u5faa\u73af\u904d\u5386\u5b57\u5178\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>for key, value in my_dict.items():<\/p>\n<p>    print(f&#39;{key}: {value}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>items<\/code>\u65b9\u6cd5\u83b7\u53d6\u5b57\u5178\u7684\u952e\u503c\u5bf9\u5217\u8868\uff0c\u5e76\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u904d\u5386\u5b57\u5178\u7684\u952e\u503c\u5bf9\u3002<strong>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6211\u4eec\u5728<code>for<\/code>\u5faa\u73af\u4e2d\u4f7f\u7528\u4e86\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6765\u6253\u5370\u952e\u503c\u5bf9\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u5b57\u5178\u7684\u5408\u5e76<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5c06\u591a\u4e2a\u5b57\u5178\u5408\u5e76\u4e3a\u4e00\u4e2a\u5b57\u5178\u3002Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u5b57\u5178\u7684\u5408\u5e76\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e24\u4e2a\u5b57\u5178<\/p>\n<p>dict1 = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<p>dict2 = {&#39;city&#39;: &#39;New York&#39;, &#39;country&#39;: &#39;USA&#39;}<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u5408\u5e76\u5b57\u5178<\/strong><\/h2>\n<p>merged_dict = {&lt;strong&gt;dict1, &lt;\/strong&gt;dict2}<\/p>\n<h2><strong>\u6253\u5370\u5408\u5e76\u540e\u7684\u5b57\u5178<\/strong><\/h2>\n<p>print(merged_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u5c06<code>dict1<\/code>\u548c<code>dict2<\/code>\u5408\u5e76\u4e3a\u4e00\u4e2a\u65b0\u7684\u5b57\u5178<code>merged_dict<\/code>\u3002<strong>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u4e24\u4e2a\u5b57\u5178\u4e2d\u6709\u76f8\u540c\u7684\u952e\uff0c\u540e\u9762\u7684\u5b57\u5178\u4e2d\u7684\u503c\u4f1a\u8986\u76d6\u524d\u9762\u7684\u5b57\u5178\u4e2d\u7684\u503c\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u5b57\u5178\u7684\u66f4\u65b0<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5bf9\u5b57\u5178\u8fdb\u884c\u66f4\u65b0\u3002Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u5b57\u5178\u7684\u66f4\u65b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>\u4f7f\u7528update\u65b9\u6cd5\u66f4\u65b0\u5b57\u5178<\/strong><\/h2>\n<p>my_dict.update({&#39;city&#39;: &#39;New York&#39;, &#39;age&#39;: 26})<\/p>\n<h2><strong>\u6253\u5370\u66f4\u65b0\u540e\u7684\u5b57\u5178<\/strong><\/h2>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>update<\/code>\u65b9\u6cd5\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u5e76\u66f4\u65b0\u5df2\u6709\u7684\u952e\u503c\u5bf9\u3002<strong>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>update<\/code>\u65b9\u6cd5\u4f1a\u8986\u76d6\u5df2\u6709\u7684\u952e\u503c\u5bf9\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u5b57\u5178\u7684\u5220\u9664<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u4ece\u5b57\u5178\u4e2d\u5220\u9664\u67d0\u4e9b\u952e\u503c\u5bf9\u3002Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u5b57\u5178\u7684\u5220\u9664\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\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\u7528del\u5173\u952e\u5b57\u5220\u9664\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>del my_dict[&#39;age&#39;]<\/p>\n<h2><strong>\u6253\u5370\u5220\u9664\u540e\u7684\u5b57\u5178<\/strong><\/h2>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>del<\/code>\u5173\u952e\u5b57\u4ece\u5b57\u5178\u4e2d\u5220\u9664\u6307\u5b9a\u7684\u952e\u503c\u5bf9\u3002<strong>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5220\u9664\u4e0d\u5b58\u5728\u7684\u952e\u4f1a\u5f15\u53d1<code>KeyError<\/code>\u5f02\u5e38\uff0c\u56e0\u6b64\u5728\u5220\u9664\u4e4b\u524d\u6700\u597d\u5148\u68c0\u67e5\u952e\u662f\u5426\u5b58\u5728\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u5b57\u5178\u7684\u9ed8\u8ba4\u503c<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4e3a\u5b57\u5178\u4e2d\u7684\u67d0\u4e9b\u952e\u8bbe\u7f6e\u9ed8\u8ba4\u503c\u3002Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u5b57\u5178\u7684\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>\u4f7f\u7528get\u65b9\u6cd5\u83b7\u53d6\u5b57\u5178\u4e2d\u7684\u503c\uff0c\u5982\u679c\u952e\u4e0d\u5b58\u5728\u5219\u8fd4\u56de\u9ed8\u8ba4\u503c<\/strong><\/h2>\n<p>city = my_dict.get(&#39;city&#39;, &#39;Unknown&#39;)<\/p>\n<h2><strong>\u6253\u5370\u83b7\u53d6\u7684\u503c<\/strong><\/h2>\n<p>print(city)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>get<\/code>\u65b9\u6cd5\u83b7\u53d6\u5b57\u5178\u4e2d\u7684\u503c\uff0c\u5982\u679c\u952e\u4e0d\u5b58\u5728\u5219\u8fd4\u56de\u9ed8\u8ba4\u503c<code>&#39;Unknown&#39;<\/code>\u3002<strong>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>get<\/code>\u65b9\u6cd5\u4e0d\u4f1a\u6539\u53d8\u5b57\u5178\u672c\u8eab\uff0c\u53ea\u662f\u8fd4\u56de\u6307\u5b9a\u952e\u7684\u503c\u6216\u9ed8\u8ba4\u503c\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u5341\u516d\u3001\u5b57\u5178\u7684\u952e\u548c\u503c\u4e92\u6362<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u5b57\u5178\u7684\u952e\u548c\u503c\u4e92\u6362\u3002Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u5b57\u5178\u7684\u952e\u548c\u503c\u4e92\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u5c06\u5b57\u5178\u7684\u952e\u548c\u503c\u4e92\u6362<\/strong><\/h2>\n<p>swapped_dict = {value: key for key, value in my_dict.items()}<\/p>\n<h2><strong>\u6253\u5370\u4e92\u6362\u540e\u7684\u5b57\u5178<\/strong><\/h2>\n<p>print(swapped_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u5c06\u5b57\u5178\u7684\u952e\u548c\u503c\u4e92\u6362\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u5178<code>swapped_dict<\/code>\u3002<strong>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b57\u5178\u4e2d\u7684\u503c\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u5426\u5219\u4f1a\u51fa\u73b0\u952e\u51b2\u7a81\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u5341\u4e03\u3001\u5b57\u5178\u7684\u5d4c\u5957<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u4f7f\u7528\u5d4c\u5957\u5b57\u5178\u6765\u5b58\u50a8\u591a\u5c42\u6b21\u7684\u6570\u636e\u3002Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u521b\u5efa\u548c\u64cd\u4f5c\u5d4c\u5957\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5d4c\u5957\u5b57\u5178<\/p>\n<p>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;details&#39;: {&#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}}<\/p>\n<h2><strong>\u8bbf\u95ee\u5d4c\u5957\u5b57\u5178\u4e2d\u7684\u503c<\/strong><\/h2>\n<p>age = my_dict[&#39;details&#39;][&#39;age&#39;]<\/p>\n<h2><strong>\u66f4\u65b0\u5d4c\u5957\u5b57\u5178\u4e2d\u7684\u503c<\/strong><\/h2>\n<p>my_dict[&#39;details&#39;][&#39;city&#39;] = &#39;Los Angeles&#39;<\/p>\n<h2><strong>\u6253\u5370\u5d4c\u5957\u5b57\u5178<\/strong><\/h2>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5d4c\u5957\u5b57\u5178<code>my_dict<\/code>\uff0c\u5e76\u8bbf\u95ee\u548c\u66f4\u65b0\u5d4c\u5957\u5b57\u5178\u4e2d\u7684\u503c\u3002<strong>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5d4c\u5957\u5b57\u5178\u53ef\u4ee5\u6709\u591a\u5c42\u6b21\uff0c\u64cd\u4f5c\u65f6\u9700\u8981\u6ce8\u610f\u5404\u5c42\u6b21\u7684\u952e\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u5341\u516b\u3001\u5b57\u5178\u7684\u6761\u4ef6\u66f4\u65b0<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u6839\u636e\u67d0\u4e9b\u6761\u4ef6\u5bf9\u5b57\u5178\u8fdb\u884c\u66f4\u65b0\u3002Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u5b57\u5178\u7684\u6761\u4ef6\u66f4\u65b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\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>\u6839\u636e\u6761\u4ef6\u66f4\u65b0\u5b57\u5178\u4e2d\u7684\u503c<\/strong><\/h2>\n<p>if my_dict[&#39;age&#39;] &gt; 20:<\/p>\n<p>    my_dict[&#39;age&#39;] = 30<\/p>\n<h2><strong>\u6253\u5370\u66f4\u65b0\u540e\u7684\u5b57\u5178<\/strong><\/h2>\n<p>print(my_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6839\u636e\u6761\u4ef6\u5bf9\u5b57\u5178\u4e2d\u7684\u503c\u8fdb\u884c\u66f4\u65b0\u3002<strong>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6761\u4ef6\u5224\u65ad\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8fdb\u884c\u8c03\u6574\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u5341\u4e5d\u3001\u5b57\u5178\u7684\u62f7\u8d1d<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5bf9\u5b57\u5178\u8fdb\u884c\u62f7\u8d1d\u4ee5\u907f\u514d\u4fee\u6539\u539f\u5b57\u5178\u7684\u6570\u636e\u3002Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u5b57\u5178\u7684\u62f7\u8d1d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>original_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>\u4f7f\u7528copy\u65b9\u6cd5\u8fdb\u884c\u6d45\u62f7\u8d1d<\/strong><\/h2>\n<p>copied_dict = original_dict.copy()<\/p>\n<h2><strong>\u4fee\u6539\u62f7\u8d1d\u540e\u7684\u5b57\u5178<\/strong><\/h2>\n<p>copied_dict[&#39;age&#39;] = 30<\/p>\n<h2><strong>\u6253\u5370\u539f\u5b57\u5178\u548c\u62f7\u8d1d\u540e\u7684\u5b57\u5178<\/strong><\/h2>\n<p>print(&quot;Original dictionary:&quot;, original_dict)<\/p>\n<p>print(&quot;Copied dictionary:&quot;, copied_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>copy<\/code>\u65b9\u6cd5\u5bf9\u5b57\u5178\u8fdb\u884c\u6d45\u62f7\u8d1d\uff0c\u7136\u540e\u4fee\u6539\u62f7\u8d1d\u540e\u7684\u5b57\u5178\u3002<strong>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6d45\u62f7\u8d1d\u53ea\u590d\u5236\u5b57\u5178\u7684\u7b2c\u4e00\u5c42\uff0c\u5bf9\u4e8e\u5d4c\u5957\u5b57\u5178\u9700\u8981\u4f7f\u7528\u6df1\u62f7\u8d1d\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e8c\u5341\u3001\u5b57\u5178\u7684\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u6bd4\u8f83\u4e24\u4e2a\u5b57\u5178\u662f\u5426\u76f8\u7b49\u3002Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u5b57\u5178\u7684\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e24\u4e2a\u5b57\u5178<\/p>\n<p>dict1 = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<p>dict2 = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<h2><strong>\u4f7f\u7528==\u8fd0\u7b97\u7b26\u6bd4\u8f83\u5b57\u5178<\/strong><\/h2>\n<p>are_equal = dict1 == dict2<\/p>\n<h2><strong>\u6253\u5370\u6bd4\u8f83\u7ed3\u679c<\/strong><\/h2>\n<p>print(&quot;Are dictionaries equal:&quot;, are_equal)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>==<\/code>\u8fd0\u7b97\u7b26\u6bd4\u8f83\u4e24\u4e2a\u5b57\u5178\u662f\u5426\u76f8\u7b49\u3002<strong>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b57\u5178\u7684\u6bd4\u8f83\u4e0d\u4ec5\u6bd4\u8f83\u952e\u503c\u5bf9\u7684\u6570\u91cf\uff0c\u8fd8\u6bd4\u8f83\u952e\u548c\u503c\u662f\u5426\u76f8\u7b49\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e8c\u5341\u4e00\u3001\u5b57\u5178\u7684\u952e\u503c\u5bf9\u8f6c\u6362\u4e3a\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u5b57\u5178\u7684\u952e\u503c\u5bf9\u8f6c\u6362<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u5b57\u5178\u6570\u636e\u5bfc\u5165\u5e76\u4f7f\u7528append\u65b9\u6cd5\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b57\u5178\u662f\u4e00\u4e2a\u7531\u952e\u503c\u5bf9\u6784\u6210\u7684\u96c6\u5408\uff0c\u56e0\u6b64\u5e76\u4e0d\u76f4\u63a5\u652f\u6301append\u65b9\u6cd5\u3002\u76f8\u53cd\uff0c\u53ef\u4ee5\u4f7f\u7528update\u65b9\u6cd5\u6216\u8005\u76f4\u63a5\u901a\u8fc7\u952e\u6765\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002\u5982\u679c\u4f60\u60f3\u5c06\u5b57\u5178\u6570\u636e\u5bfc\u5165\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u5e76\u4f7f\u7528append\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u5b57\u5178\u4f5c\u4e3a\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u91cc\u3002<\/p>\n<p><strong>\u4f7f\u7528\u5b57\u5178\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\u53ef\u4ee5\u6dfb\u52a0\u6216\u4fee\u6539\u6570\u636e\uff1f<\/strong><br \/>\u9664\u4e86update\u65b9\u6cd5\uff0cPython\u5b57\u5178\u8fd8\u652f\u6301\u901a\u8fc7\u6307\u5b9a\u952e\u6765\u4fee\u6539\u6216\u6dfb\u52a0\u65b0\u503c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>my_dict[&#39;new_key&#39;] = &#39;new_value&#39;<\/code>\u53ef\u4ee5\u76f4\u63a5\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u952e\u503c\u5bf9\u3002\u5982\u679c\u9700\u8981\u6dfb\u52a0\u591a\u4e2a\u952e\u503c\u5bf9\uff0c\u53ef\u4ee5\u4f7f\u7528update\u65b9\u6cd5\uff0c\u6bd4\u5982<code>my_dict.update({&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;})<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u4ece\u6587\u4ef6\u5bfc\u5165\u5b57\u5178\u6570\u636e\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528json\u6a21\u5757\u5bfc\u5165\u5b57\u5178\u6570\u636e\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684\u5b57\u5178\u6570\u636e\u4ee5JSON\u683c\u5f0f\u5b58\u50a8\u5728\u6587\u4ef6\u4e2d\u3002\u4f7f\u7528<code>json.load()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8f7b\u677e\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aPython\u5b57\u5178\u3002\u4f8b\u5982\uff0c<code>with open(&#39;data.json&#39;) as f: my_dict = json.load(f)<\/code>\uff0c\u8fd9\u6837\u5c31\u80fd\u5c06\u6587\u4ef6\u4e2d\u7684\u5b57\u5178\u6570\u636e\u5bfc\u5165\u5230Python\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5b57\u5178\u5bfc\u5165Python\u5e76\u4f7f\u7528append\u7684\u65b9\u6cd5\u6709\u591a\u79cd\u65b9\u5f0f\uff0c\u5305\u62ec\u76f4\u63a5\u5728\u4ee3\u7801\u4e2d\u58f0\u660e\u5b57\u5178\u3001\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5b57\u5178\u6570\u636e\u3001\u4f7f\u7528\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1149092,"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\/1149086"}],"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=1149086"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149086\/revisions"}],"predecessor-version":[{"id":1149093,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149086\/revisions\/1149093"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1149092"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1149086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1149086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1149086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}