{"id":186,"date":"2025-02-23T09:17:43","date_gmt":"2025-02-23T09:17:43","guid":{"rendered":"https:\/\/news.techjunior.vn\/?p=186"},"modified":"2025-02-23T09:17:43","modified_gmt":"2025-02-23T09:17:43","slug":"huong-dan-xu-ly-va-truy-cap-du-lieu-trong-nested-dictionary-python","status":"publish","type":"post","link":"https:\/\/news.techjunior.vn\/?p=186","title":{"rendered":"H\u01b0\u1edbng D\u1eabn X\u1eed L\u00fd V\u00e0 Truy C\u1eadp D\u1eef Li\u1ec7u Trong Nested Dictionary Python"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\"><\/h1>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"482\" src=\"https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/02\/image-50-1024x482.png\" alt=\"\" class=\"wp-image-187\" srcset=\"https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/02\/image-50-1024x482.png 1024w, https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/02\/image-50-300x141.png 300w, https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/02\/image-50-768x362.png 768w, https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/02\/image-50-850x400.png 850w, https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/02\/image-50.png 1077w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. Nested Dictionary L\u00e0 G\u00ec?<\/strong><\/h2>\n\n\n\n<p>Trong Python, <strong>nested dictionary<\/strong> (t\u1eeb \u0111i\u1ec3n l\u1ed3ng nhau) l\u00e0 m\u1ed9t dictionary ch\u1ee9a m\u1ed9t ho\u1eb7c nhi\u1ec1u dictionary b\u00ean trong n\u00f3. \u0110i\u1ec1u n\u00e0y gi\u00fap t\u1ed5 ch\u1ee9c d\u1eef li\u1ec7u c\u00f3 c\u1ea5u tr\u00fac ph\u1ee9c t\u1ea1p h\u01a1n, t\u01b0\u01a1ng t\u1ef1 nh\u01b0 m\u1ed9t b\u1ea3ng d\u1eef li\u1ec7u c\u00f3 nhi\u1ec1u c\u1ea5p \u0111\u1ed9.<\/p>\n\n\n\n<p>V\u00ed d\u1ee5 v\u1ec1 m\u1ed9t <strong>nested dictionary<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sinh_vien = {<br>    \"SV001\": {<br>        \"t\u00ean\": \"Nguy\u1ec5n V\u0103n A\",<br>        \"tu\u1ed5i\": 20,<br>        \"\u0111i\u1ec3m\": {\"to\u00e1n\": 8.5, \"v\u0103n\": 7.0, \"anh\": 9.0}<br>    },<br>    \"SV002\": {<br>        \"t\u00ean\": \"Tr\u1ea7n Th\u1ecb B\",<br>        \"tu\u1ed5i\": 21,<br>        \"\u0111i\u1ec3m\": {\"to\u00e1n\": 6.5, \"v\u0103n\": 8.0, \"anh\": 7.5}<br>    }<br>}<br><\/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. C\u00e1ch Truy C\u1eadp D\u1eef Li\u1ec7u Trong Nested Dictionary<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1. Truy C\u1eadp Gi\u00e1 Tr\u1ecb Tr\u1ef1c Ti\u1ebfp<\/strong><\/h3>\n\n\n\n<p>B\u1ea1n c\u00f3 th\u1ec3 truy c\u1eadp d\u1eef li\u1ec7u b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng c\u00e1c <strong>key<\/strong> c\u1ee7a dictionary.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>print(sinh_vien[\"SV001\"][\"t\u00ean\"])   # K\u1ebft qu\u1ea3: Nguy\u1ec5n V\u0103n A<br>print(sinh_vien[\"SV002\"][\"\u0111i\u1ec3m\"][\"to\u00e1n\"])  # K\u1ebft qu\u1ea3: 6.5<br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2. Tr\u00e1nh L\u1ed7i KeyError Khi Truy C\u1eadp Key Kh\u00f4ng T\u1ed3n T\u1ea1i<\/strong><\/h3>\n\n\n\n<p>S\u1eed d\u1ee5ng ph\u01b0\u01a1ng th\u1ee9c <code>.get()<\/code> \u0111\u1ec3 tr\u00e1nh l\u1ed7i khi key kh\u00f4ng t\u1ed3n t\u1ea1i.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>print(sinh_vien.get(\"SV003\", \"Kh\u00f4ng t\u00ecm th\u1ea5y sinh vi\u00ean\"))  # K\u1ebft qu\u1ea3: Kh\u00f4ng t\u00ecm th\u1ea5y sinh vi\u00ean<br>print(sinh_vien[\"SV001\"].get(\"\u0111i\u1ec3m\", {}).get(\"h\u00f3a\", \"Kh\u00f4ng c\u00f3 \u0111i\u1ec3m m\u00f4n H\u00f3a\"))  # K\u1ebft qu\u1ea3: Kh\u00f4ng c\u00f3 \u0111i\u1ec3m m\u00f4n H\u00f3a<br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.3. S\u1eed D\u1ee5ng V\u00f2ng L\u1eb7p \u0110\u1ec3 Duy\u1ec7t T\u1eebng Ph\u1ea7n T\u1eed<\/strong><\/h3>\n\n\n\n<p>D\u00f9ng v\u00f2ng l\u1eb7p <code>for<\/code> \u0111\u1ec3 duy\u1ec7t qua c\u00e1c dictionary l\u1ed3ng nhau.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>for ma_sv, thong_tin in sinh_vien.items():<br>    print(f\"M\u00e3 SV: {ma_sv}, T\u00ean: {thong_tin['t\u00ean']}, Tu\u1ed5i: {thong_tin['tu\u1ed5i']}\")<br><\/code><\/pre>\n\n\n\n<p><strong>K\u1ebft qu\u1ea3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>M\u00e3 SV: SV001, T\u00ean: Nguy\u1ec5n V\u0103n A, Tu\u1ed5i: 20<br>M\u00e3 SV: SV002, T\u00ean: Tr\u1ea7n Th\u1ecb B, Tu\u1ed5i: 21<br><\/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. C\u1eadp Nh\u1eadt V\u00e0 Th\u00eam D\u1eef Li\u1ec7u V\u00e0o Nested Dictionary<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.1. Th\u00eam M\u1ed9t Sinh Vi\u00ean M\u1edbi<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sinh_vien[\"SV003\"] = {<br>    \"t\u00ean\": \"Ph\u1ea1m V\u0103n C\",<br>    \"tu\u1ed5i\": 19,<br>    \"\u0111i\u1ec3m\": {\"to\u00e1n\": 9.0, \"v\u0103n\": 6.5, \"anh\": 8.0}<br>}<br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.2. C\u1eadp Nh\u1eadt D\u1eef Li\u1ec7u<\/strong><\/h3>\n\n\n\n<p>Gi\u1ea3 s\u1eed c\u1ea7n c\u1eadp nh\u1eadt \u0111i\u1ec3m m\u00f4n To\u00e1n c\u1ee7a sinh vi\u00ean <strong>SV002<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sinh_vien[\"SV002\"][\"\u0111i\u1ec3m\"][\"to\u00e1n\"] = 7.5<br><\/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. X\u00f3a Ph\u1ea7n T\u1eed Trong Nested Dictionary<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.1. X\u00f3a M\u1ed9t Key B\u00ean Trong Dictionary<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>del sinh_vien[\"SV001\"][\"\u0111i\u1ec3m\"][\"v\u0103n\"]  # X\u00f3a \u0111i\u1ec3m m\u00f4n V\u0103n c\u1ee7a SV001<br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.2. X\u00f3a M\u1ed9t Sinh Vi\u00ean<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sinh_vien.pop(\"SV002\", \"Kh\u00f4ng t\u00ecm th\u1ea5y sinh vi\u00ean\")  # X\u00f3a sinh vi\u00ean SV002<br><\/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. X\u1eed L\u00fd Nested Dictionary V\u1edbi V\u00f2ng L\u1eb7p \u0110\u1ec7 Quy<\/strong><\/h2>\n\n\n\n<p>Khi b\u1ea1n kh\u00f4ng bi\u1ebft tr\u01b0\u1edbc s\u1ed1 c\u1ea5p l\u1ed3ng nhau c\u1ee7a dictionary, b\u1ea1n c\u00f3 th\u1ec3 d\u00f9ng \u0111\u1ec7 quy \u0111\u1ec3 duy\u1ec7t h\u1ebft d\u1eef li\u1ec7u.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>def duy\u1ec7t_dictionary(dic, c\u1ea5p=0):<br>    for key, value in dic.items():<br>        print(\" \" * c\u1ea5p * 4 + f\"{key}: {value}\" if not isinstance(value, dict) else f\"{key}:\")<br>        if isinstance(value, dict):<br>            duy\u1ec7t_dictionary(value, c\u1ea5p + 1)<br><br>duy\u1ec7t_dictionary(sinh_vien)<br><\/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>T\u1ed5ng K\u1ebft<\/strong><\/h2>\n\n\n\n<p>\u2714 <strong>Truy c\u1eadp d\u1eef li\u1ec7u:<\/strong> D\u00f9ng <code>dict[\"key\"]<\/code>, <code>.get()<\/code> \u0111\u1ec3 tr\u00e1nh l\u1ed7i.<br>\u2714 <strong>Duy\u1ec7t d\u1eef li\u1ec7u:<\/strong> S\u1eed d\u1ee5ng v\u00f2ng l\u1eb7p <code>for<\/code>.<br>\u2714 <strong>C\u1eadp nh\u1eadt &amp; th\u00eam d\u1eef li\u1ec7u:<\/strong> G\u00e1n gi\u00e1 tr\u1ecb m\u1edbi tr\u1ef1c ti\u1ebfp.<br>\u2714 <strong>X\u00f3a d\u1eef li\u1ec7u:<\/strong> D\u00f9ng <code>del<\/code> ho\u1eb7c <code>.pop()<\/code>.<br>\u2714 <strong>Duy\u1ec7t t\u1ea5t c\u1ea3 d\u1eef li\u1ec7u:<\/strong> D\u00f9ng v\u00f2ng l\u1eb7p \u0111\u1ec7 quy n\u1ebfu dictionary l\u1ed3ng nhau ph\u1ee9c t\u1ea1p.<\/p>\n\n\n\n<p>V\u1edbi c\u00e1c ki\u1ebfn th\u1ee9c tr\u00ean, b\u1ea1n c\u00f3 th\u1ec3 d\u1ec5 d\u00e0ng l\u00e0m vi\u1ec7c v\u1edbi <strong>nested dictionary<\/strong> trong Python m\u1ed9t c\u00e1ch hi\u1ec7u qu\u1ea3! \ud83d\ude80<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Nested Dictionary L\u00e0 G\u00ec? Trong Python, nested dictionary (t\u1eeb \u0111i\u1ec3n l\u1ed3ng nhau) l\u00e0 m\u1ed9t dictionary ch\u1ee9a m\u1ed9t ho\u1eb7c nhi\u1ec1u dictionary b\u00ean trong n\u00f3. \u0110i\u1ec1u n\u00e0y gi\u00fap t\u1ed5 ch\u1ee9c d\u1eef li\u1ec7u c\u00f3&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-186","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=186"}],"version-history":[{"count":1,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/186\/revisions"}],"predecessor-version":[{"id":188,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/186\/revisions\/188"}],"wp:attachment":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}