{"id":353,"date":"2025-03-12T10:32:06","date_gmt":"2025-03-12T10:32:06","guid":{"rendered":"https:\/\/news.techjunior.vn\/?p=353"},"modified":"2025-03-12T10:32:06","modified_gmt":"2025-03-12T10:32:06","slug":"huong-dan-su-dung-queue-thay-cho-list-trong-python","status":"publish","type":"post","link":"https:\/\/news.techjunior.vn\/?p=353","title":{"rendered":"H\u01b0\u1edbng d\u1eabn s\u1eed d\u1ee5ng Queue thay cho List trong Python"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"741\" height=\"272\" src=\"https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/03\/image-22.png\" alt=\"\" class=\"wp-image-354\" srcset=\"https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/03\/image-22.png 741w, https:\/\/news.techjunior.vn\/wp-content\/uploads\/2025\/03\/image-22-300x110.png 300w\" sizes=\"auto, (max-width: 741px) 100vw, 741px\" \/><\/figure>\n\n\n\n<p>Trong Python, c\u1ea3 <code>list<\/code> v\u00e0 <code>queue<\/code> \u0111\u1ec1u c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c d\u00f9ng \u0111\u1ec3 l\u01b0u tr\u1eef d\u1eef li\u1ec7u, tuy nhi\u00ean m\u1ed7i c\u1ea5u tr\u00fac c\u00f3 nh\u1eefng \u01b0u \u0111i\u1ec3m v\u00e0 tr\u01b0\u1eddng h\u1ee3p s\u1eed d\u1ee5ng ri\u00eang. \u0110\u1eb7c bi\u1ec7t, khi x\u1eed l\u00fd d\u1eef li\u1ec7u theo d\u1ea1ng FIFO (First In, First Out \u2013 V\u00e0o tr\u01b0\u1edbc, ra tr\u01b0\u1edbc), th\u00ec vi\u1ec7c s\u1eed d\u1ee5ng Queue s\u1ebd hi\u1ec7u qu\u1ea3 v\u00e0 an to\u00e0n h\u01a1n.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">V\u00ec sao n\u00ean d\u00f9ng Queue thay cho List?<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Hi\u1ec7u n\u0103ng:<\/strong> Queue t\u1ed1i \u01b0u h\u00f3a t\u1ed1t h\u01a1n cho thao t\u00e1c FIFO (enqueue v\u00e0 dequeue).<\/li>\n\n\n\n<li><strong>Thread-safe:<\/strong> Queue h\u1ed7 tr\u1ee3 t\u1ed1t trong c\u00e1c \u1ee9ng d\u1ee5ng \u0111a lu\u1ed3ng, gi\u00fap tr\u00e1nh c\u00e1c v\u1ea5n \u0111\u1ec1 xung \u0111\u1ed9t d\u1eef li\u1ec7u khi nhi\u1ec1u lu\u1ed3ng c\u00f9ng truy c\u1eadp.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">C\u00e1ch s\u1eed d\u1ee5ng Queue trong Python:<\/h3>\n\n\n\n<p><strong>1. Khai b\u00e1o v\u00e0 s\u1eed d\u1ee5ng Queue c\u01a1 b\u1ea3n:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import queue\n\n# T\u1ea1o queue m\u1edbi\nq = queue.Queue()\n\n# Th\u00eam ph\u1ea7n t\u1eed v\u00e0o Queue\nq.put(\"A\")\nq.put(\"B\")\nq.put(\"C\")\n\n# L\u1ea5y v\u00e0 x\u00f3a ph\u1ea7n t\u1eed kh\u1ecfi Queue\nfirst_element = q.get()\nprint(first_element)  # K\u1ebft qu\u1ea3: A<\/code><\/pre>\n\n\n\n<p><strong>2. Ki\u1ec3m tra Queue c\u00f3 tr\u1ed1ng hay kh\u00f4ng:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if q.empty():\n    print(\"Queue tr\u1ed1ng\")\nelse:\n    print(\"Queue kh\u00f4ng tr\u1ed1ng\")<\/code><\/pre>\n\n\n\n<p><strong>3. \u0110\u1eb7t gi\u1edbi h\u1ea1n k\u00edch th\u01b0\u1edbc cho Queue:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>q_limited = queue.Queue(maxsize=3)\nq_limited.put(\"X\")\nq_limited.put(\"Y\")\nq_limited.put(\"Z\")\n\n# Queue s\u1ebd ch\u1edd n\u1ebfu v\u01b0\u1ee3t qu\u00e1 k\u00edch th\u01b0\u1edbc gi\u1edbi h\u1ea1n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">So s\u00e1nh nhanh Queue v\u00e0 List:<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>\u0110\u1eb7c \u0111i\u1ec3m<\/th><th>Queue<\/th><th>List<\/th><\/tr><tr><td>Ki\u1ec3u d\u1eef li\u1ec7u<\/td><td>FIFO (\u01b0u ti\u00ean ph\u1ea7n t\u1eed \u0111\u1ea7u)<\/td><td>\u0110a n\u0103ng (c\u00f3 th\u1ec3 d\u00f9ng FIFO, LIFO)<\/td><\/tr><tr><td>Hi\u1ec7u su\u1ea5t<\/td><td>T\u1ed1i \u01b0u cho FIFO<\/td><td>K\u00e9m h\u01a1n cho FIFO<\/td><\/tr><tr><td>Thread-safe<\/td><td>C\u00f3<\/td><td>Kh\u00f4ng<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Khi n\u00e0o d\u00f9ng Queue?<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>X\u1eed l\u00fd c\u00e1c t\u00e1c v\u1ee5 b\u1ea5t \u0111\u1ed3ng b\u1ed9, \u0111a lu\u1ed3ng.<\/li>\n\n\n\n<li>Khi c\u1ea7n \u0111\u1ea3m b\u1ea3o th\u1ee9 t\u1ef1 x\u1eed l\u00fd c\u00e1c ph\u1ea7n t\u1eed.<\/li>\n\n\n\n<li>Khi c\u1ea7n hi\u1ec7u su\u1ea5t t\u1ed1i \u01b0u v\u1edbi c\u00e1c t\u00e1c v\u1ee5 FIFO.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">K\u1ebft lu\u1eadn:<\/h3>\n\n\n\n<p>Queue l\u00e0 l\u1ef1a ch\u1ecdn t\u1ed1t h\u01a1n so v\u1edbi List khi b\u1ea1n c\u1ea7n x\u1eed l\u00fd c\u00e1c t\u00e1c v\u1ee5 theo d\u1ea1ng FIFO, nh\u1ea5t l\u00e0 trong c\u00e1c ch\u01b0\u01a1ng tr\u00ecnh \u0111a lu\u1ed3ng. Vi\u1ec7c hi\u1ec3u r\u00f5 \u0111\u1eb7c \u0111i\u1ec3m v\u00e0 c\u00e1ch s\u1eed d\u1ee5ng Queue gi\u00fap t\u1ed1i \u01b0u h\u00f3a m\u00e3 ngu\u1ed3n Python c\u1ee7a b\u1ea1n.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Trong Python, c\u1ea3 list v\u00e0 queue \u0111\u1ec1u c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c d\u00f9ng \u0111\u1ec3 l\u01b0u tr\u1eef d\u1eef li\u1ec7u, tuy nhi\u00ean m\u1ed7i c\u1ea5u tr\u00fac c\u00f3 nh\u1eefng \u01b0u \u0111i\u1ec3m v\u00e0 tr\u01b0\u1eddng h\u1ee3p s\u1eed d\u1ee5ng ri\u00eang. \u0110\u1eb7c&#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-353","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/353","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=353"}],"version-history":[{"count":1,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/353\/revisions"}],"predecessor-version":[{"id":355,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=\/wp\/v2\/posts\/353\/revisions\/355"}],"wp:attachment":[{"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/news.techjunior.vn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}