{"id":2741,"date":"2025-03-14T22:22:03","date_gmt":"2025-03-14T14:22:03","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2741"},"modified":"2025-03-14T22:22:03","modified_gmt":"2025-03-14T14:22:03","slug":"python-3-%e5%ad%97%e5%85%b8%ef%bc%88dictionary%ef%bc%89%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/14\/python-3-%e5%ad%97%e5%85%b8%ef%bc%88dictionary%ef%bc%89%e8%af%a6%e8%a7%a3\/","title":{"rendered":"Python 3 \u5b57\u5178\uff08Dictionary\uff09\u8be6\u89e3"},"content":{"rendered":"\n<p>\u5728 Python 3 \u4e2d\uff0c<strong>\u5b57\u5178\uff08Dictionary\uff09\u662f\u4e00\u4e2a\u952e\u503c\u5bf9\uff08key-value pair\uff09\u6570\u636e\u7ed3\u6784<\/strong>\uff0c\u7c7b\u4f3c\u4e8e JavaScript \u7684\u5bf9\u8c61\uff08Object\uff09\u6216 JSON\u3002\u5b57\u5178\u662f<strong>\u65e0\u5e8f\u3001\u53ef\u53d8\u3001\u4e14\u952e\u552f\u4e00<\/strong>\u7684\u6570\u636e\u7ed3\u6784\uff0c\u9002\u7528\u4e8e\u5feb\u901f\u67e5\u627e\u548c\u5b58\u50a8\u6570\u636e\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u5b57\u5178\u7684\u5b9a\u4e49<\/strong><\/h2>\n\n\n\n<p>\u5b57\u5178\u4f7f\u7528 <strong>\u82b1\u62ec\u53f7 <code>{}<\/code><\/strong> \u521b\u5efa\uff0c\u6bcf\u4e2a\u952e\u503c\u5bf9\u7528 <code>key: value<\/code> \u8868\u793a\uff0c\u952e\u503c\u5bf9\u4e4b\u95f4\u7528 <strong>\u9017\u53f7 <code>,<\/code><\/strong> \u5206\u9694\u3002<\/p>\n\n\n\n<p>\ud83d\udccc <strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178\nperson = {\n    \"name\": \"Alice\",\n    \"age\": 25,\n    \"city\": \"New York\"\n}\nprint(person)\n# \u8f93\u51fa: {'name': 'Alice', 'age': 25, 'city': 'New York'}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u952e\uff08Key\uff09\u5fc5\u987b\u662f\u4e0d\u53ef\u53d8\u7c7b\u578b<\/strong>\uff08\u5982\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5143\u7ec4\uff09\u3002<\/li>\n\n\n\n<li><strong>\u503c\uff08Value\uff09\u53ef\u4ee5\u662f\u4efb\u4f55\u6570\u636e\u7c7b\u578b<\/strong>\uff08\u5982\u5217\u8868\u3001\u5b57\u5178\u3001\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u7b49\uff09\u3002<\/li>\n\n\n\n<li><strong>\u952e\u552f\u4e00\uff0c\u4e0d\u5141\u8bb8\u91cd\u590d<\/strong>\uff0c\u5982\u679c\u91cd\u590d\uff0c\u540e\u9762\u7684\u503c\u4f1a\u8986\u76d6\u524d\u9762\u7684\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<p>\ud83d\udccc <strong>\u793a\u4f8b\u4ee3\u7801\uff08\u952e\u552f\u4e00\u6027\uff09\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>person = {\"name\": \"Alice\", \"age\": 25, \"name\": \"Bob\"}\nprint(person)  # {'name': 'Bob', 'age': 25}\uff0c\u524d\u9762\u7684 \"Alice\" \u88ab\u8986\u76d6<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u8bbf\u95ee\u5b57\u5178\u5143\u7d20<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff081\uff09\u901a\u8fc7 <code>key<\/code> \u8bbf\u95ee<\/strong><\/h3>\n\n\n\n<p>\ud83d\udccc <strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>person = {\"name\": \"Alice\", \"age\": 25, \"city\": \"New York\"}\nprint(person&#91;\"name\"])  # Alice\nprint(person&#91;\"age\"])   # 25<\/code><\/pre>\n\n\n\n<p>\u26a0 <strong>\u6ce8\u610f\uff1a\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u4f1a\u62a5 KeyError<\/strong>\uff01<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff082\uff09\u4f7f\u7528 <code>get()<\/code> \u65b9\u6cd5<\/strong><\/h3>\n\n\n\n<p>\ud83d\udccc <strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(person.get(\"city\"))      # New York\nprint(person.get(\"country\"))   # None\nprint(person.get(\"country\", \"Unknown\"))  # \u4f7f\u7528\u9ed8\u8ba4\u503c Unknown<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u4fee\u6539\u5b57\u5178<\/strong><\/h2>\n\n\n\n<p>\u53ef\u4ee5\u901a\u8fc7 <strong><code>key<\/code> \u76f4\u63a5\u4fee\u6539\u503c<\/strong>\uff0c\u5982\u679c <code>key<\/code> \u5b58\u5728\uff0c\u5219\u66f4\u65b0\u503c\uff1b\u5982\u679c <code>key<\/code> \u4e0d\u5b58\u5728\uff0c\u5219\u65b0\u589e\u952e\u503c\u5bf9\u3002<\/p>\n\n\n\n<p>\ud83d\udccc <strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>person = {\"name\": \"Alice\", \"age\": 25}\nperson&#91;\"age\"] = 26   # \u4fee\u6539\u5df2\u6709\u952e\u7684\u503c\nperson&#91;\"city\"] = \"New York\"  # \u65b0\u589e\u952e\u503c\u5bf9\nprint(person)\n# \u8f93\u51fa: {'name': 'Alice', 'age': 26, 'city': 'New York'}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u5220\u9664\u5b57\u5178\u5143\u7d20<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff081\uff09\u4f7f\u7528 <code>del<\/code> \u5173\u952e\u5b57<\/strong><\/h3>\n\n\n\n<p>\ud83d\udccc <strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>person = {\"name\": \"Alice\", \"age\": 25, \"city\": \"New York\"}\ndel person&#91;\"age\"]  # \u5220\u9664\u952e \"age\"\nprint(person)  # {'name': 'Alice', 'city': 'New York'}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff082\uff09\u4f7f\u7528 <code>pop()<\/code> \u65b9\u6cd5<\/strong><\/h3>\n\n\n\n<p>\ud83d\udccc <strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>age = person.pop(\"age\", \"Key Not Found\")  # \u79fb\u9664 \"age\"\uff0c\u8fd4\u56de\u503c 25\nprint(age)  # 25\nprint(person)  # {'name': 'Alice', 'city': 'New York'}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff083\uff09\u4f7f\u7528 <code>popitem()<\/code> \u65b9\u6cd5<\/strong><\/h3>\n\n\n\n<p>\ud83d\udccc <strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>person = {\"name\": \"Alice\", \"age\": 25}\nitem = person.popitem()  # \u5220\u9664\u5e76\u8fd4\u56de\u6700\u540e\u4e00\u4e2a\u952e\u503c\u5bf9\nprint(item)  # ('age', 25)\nprint(person)  # {'name': 'Alice'}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff084\uff09\u6e05\u7a7a\u5b57\u5178 <code>clear()<\/code><\/strong><\/h3>\n\n\n\n<p>\ud83d\udccc <strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>person.clear()\nprint(person)  # {}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u904d\u5386\u5b57\u5178<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff081\uff09\u904d\u5386\u6240\u6709\u952e<\/strong><\/h3>\n\n\n\n<p>\ud83d\udccc <strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>person = {\"name\": \"Alice\", \"age\": 25, \"city\": \"New York\"}\nfor key in person:\n    print(key)  # name, age, city<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff082\uff09\u904d\u5386\u6240\u6709\u503c<\/strong><\/h3>\n\n\n\n<p>\ud83d\udccc <strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for value in person.values():\n    print(value)  # Alice, 25, New York<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\uff083\uff09\u904d\u5386\u952e\u503c\u5bf9<\/strong><\/h3>\n\n\n\n<p>\ud83d\udccc <strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for key, value in person.items():\n    print(f\"{key}: {value}\")\n# name: Alice\n# age: 25\n# city: New York<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. \u5b57\u5178\u5e38\u7528\u65b9\u6cd5<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6cd5<\/th><th>\u8bf4\u660e<\/th><th>\u793a\u4f8b<\/th><\/tr><\/thead><tbody><tr><td><code>dict.keys()<\/code><\/td><td>\u83b7\u53d6\u6240\u6709\u952e<\/td><td><code>person.keys()<\/code><\/td><\/tr><tr><td><code>dict.values()<\/code><\/td><td>\u83b7\u53d6\u6240\u6709\u503c<\/td><td><code>person.values()<\/code><\/td><\/tr><tr><td><code>dict.items()<\/code><\/td><td>\u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9<\/td><td><code>person.items()<\/code><\/td><\/tr><tr><td><code>dict.update(new_dict)<\/code><\/td><td>\u5408\u5e76\u5b57\u5178<\/td><td><code>person.update({\"country\": \"USA\"})<\/code><\/td><\/tr><tr><td><code>dict.copy()<\/code><\/td><td>\u590d\u5236\u5b57\u5178<\/td><td><code>new_dict = person.copy()<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\ud83d\udccc <strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>person = {\"name\": \"Alice\", \"age\": 25}\nnew_dict = person.copy()  # \u590d\u5236\u5b57\u5178\nprint(new_dict)  # {'name': 'Alice', 'age': 25}\n\nperson.update({\"city\": \"New York\", \"age\": 26})  # \u66f4\u65b0\u5b57\u5178\nprint(person)  # {'name': 'Alice', 'age': 26, 'city': 'New York'}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. \u5b57\u5178\u5408\u5e76<\/strong><\/h2>\n\n\n\n<p>Python 3.9+ \u63d0\u4f9b <code>|<\/code> \u8fd0\u7b97\u7b26\u7528\u4e8e\u5408\u5e76\u5b57\u5178\u3002<\/p>\n\n\n\n<p>\ud83d\udccc <strong>\u793a\u4f8b\u4ee3\u7801\uff08Python 3.9+\uff09\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dict1 = {\"a\": 1, \"b\": 2}\ndict2 = {\"b\": 3, \"c\": 4}\nmerged_dict = dict1 | dict2  # \u5408\u5e76\uff0cdict2 \u7684 \"b\" \u8986\u76d6\u4e86 dict1 \u7684 \"b\"\nprint(merged_dict)  # {'a': 1, 'b': 3, 'c': 4}<\/code><\/pre>\n\n\n\n<p>\u5bf9\u4e8e <strong>Python 3.8 \u53ca\u4ee5\u4e0b<\/strong>\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>update()<\/code> \u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dict1.update(dict2)\nprint(dict1)  # {'a': 1, 'b': 3, 'c': 4}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>8. \u5b57\u5178\u63a8\u5bfc\u5f0f\uff08Dictionary Comprehension\uff09<\/strong><\/h2>\n\n\n\n<p>Python \u652f\u6301<strong>\u5b57\u5178\u63a8\u5bfc\u5f0f<\/strong>\uff0c\u7528\u4e8e\u5feb\u901f\u521b\u5efa\u5b57\u5178\u3002<\/p>\n\n\n\n<p>\ud83d\udccc <strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>squares = {x: x**2 for x in range(1, 6)}\nprint(squares)  # {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>9. \u5b57\u5178\u4e0e JSON<\/strong><\/h2>\n\n\n\n<p>Python \u7684 <code>dict<\/code> \u4e0e <code>JSON<\/code> \u7ed3\u6784\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>json<\/code> \u6a21\u5757\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n\n\n\n<p>\ud83d\udccc <strong>\u5b57\u5178\u8f6c\u6362\u4e3a JSON<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\n\nperson = {\"name\": \"Alice\", \"age\": 25, \"city\": \"New York\"}\njson_data = json.dumps(person)  # \u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\nprint(json_data)  # {\"name\": \"Alice\", \"age\": 25, \"city\": \"New York\"}<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>JSON \u8f6c\u6362\u4e3a\u5b57\u5178<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dict_data = json.loads(json_data)\nprint(dict_data)  # {'name': 'Alice', 'age': 25, 'city': 'New York'}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p>\u2705 <strong>\u5b57\u5178\u662f\u952e\u503c\u5bf9\u6570\u636e\u7ed3\u6784\uff0c\u952e\u5fc5\u987b\u552f\u4e00\u4e14\u4e0d\u53ef\u53d8<\/strong><br>\u2705 <strong>\u652f\u6301 <code>keys()<\/code>\u3001<code>values()<\/code>\u3001<code>items()<\/code> \u8fdb\u884c\u904d\u5386<\/strong><br>\u2705 <strong>\u53ef\u4ee5\u4f7f\u7528 <code>get()<\/code> \u65b9\u6cd5\u83b7\u53d6\u503c\uff0c\u907f\u514d <code>KeyError<\/code><\/strong><br>\u2705 <strong>\u652f\u6301\u5408\u5e76\u3001\u4fee\u6539\u3001\u5220\u9664\u548c\u5b57\u5178\u63a8\u5bfc\u5f0f<\/strong><br>\u2705 <strong>\u9002\u7528\u4e8e\u5feb\u901f\u67e5\u627e\u6570\u636e\u3001\u5b58\u50a8\u914d\u7f6e\u4fe1\u606f\u3001\u89e3\u6790 JSON \u6570\u636e<\/strong><\/p>\n\n\n\n<p><strong>\u5b57\u5178\u9002\u7528\u4e8e\u5b58\u50a8\u7ed3\u6784\u5316\u6570\u636e\uff0c\u8bbf\u95ee\u901f\u5ea6\u6bd4\u5217\u8868\u5feb\uff01<\/strong> \u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python 3 \u4e2d\uff0c\u5b57\u5178\uff08Dictionary\uff09\u662f\u4e00\u4e2a\u952e\u503c\u5bf9\uff08key-value pair\uff09\u6570\u636e\u7ed3\u6784\uff0c\u7c7b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2742,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-2741","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2741","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2741"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2741\/revisions"}],"predecessor-version":[{"id":2743,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2741\/revisions\/2743"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2742"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}