{"id":1618,"date":"2025-09-29T21:34:21","date_gmt":"2025-09-29T14:34:21","guid":{"rendered":"https:\/\/kienthucmo.com\/?p=1618"},"modified":"2025-11-05T23:43:21","modified_gmt":"2025-11-05T16:43:21","slug":"tim-kiem-phan-tu-trong-set-voi-python","status":"publish","type":"post","link":"https:\/\/kienthucmo.com\/vi\/tim-kiem-phan-tu-trong-set-voi-python\/","title":{"rendered":"T\u00ecm ki\u1ebfm ph\u1ea7n t\u1eed trong Set v\u1edbi Python"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Trong Python, <strong>set<\/strong> l\u00e0 m\u1ed9t c\u1ea5u tr\u00fac d\u1eef li\u1ec7u m\u1ea1nh m\u1ebd d\u00e0nh cho c\u00e1c b\u00e0i to\u00e1n li\u00ean quan t\u1edbi ki\u1ec3m tra th\u00e0nh vi\u00ean, lo\u1ea1i b\u1ecf ph\u1ea7n t\u1eed tr\u00f9ng l\u1eafp v\u00e0 c\u00e1c ph\u00e9p to\u00e1n t\u1eadp h\u1ee3p (union, intersection, difference&#8230;). N\u1ebfu m\u1ee5c ti\u00eau c\u1ee7a b\u1ea1n l\u00e0 <strong>t\u00ecm ki\u1ebfm ph\u1ea7n t\u1eed nhanh<\/strong> (membership queries) ho\u1eb7c ki\u1ec3m tra t\u1ed3n t\u1ea1i c\u1ee7a nhi\u1ec1u gi\u00e1 tr\u1ecb c\u00f9ng l\u00fac, set th\u01b0\u1eddng l\u00e0 l\u1ef1a ch\u1ecdn hi\u1ec7u qu\u1ea3 nh\u1ea5t. Tuy nhi\u00ean do b\u1ea3n ch\u1ea5t <strong>kh\u00f4ng c\u00f3 th\u1ee9 t\u1ef1<\/strong> v\u00e0 <strong>y\u00eau c\u1ea7u ph\u1ea7n t\u1eed ph\u1ea3i hashable<\/strong>, c\u00e1ch ti\u1ebfp c\u1eadn khi t\u00ecm ki\u1ebfm trong set kh\u00e1c m\u1ed9t ch\u00fat so v\u1edbi list hay tuple.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Trong b\u00e0i vi\u1ebft n\u00e0y, h\u00e3y c\u00f9ng m\u00ecnh t\u00ecm hi\u1ec3u c\u1ea5u tr\u00fac b\u00ean trong, c\u00e1c ph\u01b0\u01a1ng ph\u00e1p t\u00ecm (t\u00ecm m\u1ed9t gi\u00e1 tr\u1ecb, t\u00ecm theo \u0111i\u1ec1u ki\u1ec7n, t\u00ecm nhi\u1ec1u gi\u00e1 tr\u1ecb), ph\u00e2n t\u00edch \u0111\u1ed9 ph\u1ee9c t\u1ea1p, \u0111i\u1ec3m y\u1ebfu\/caveats v\u00e0 nh\u1eefng t\u00ecnh hu\u1ed1ng n\u00ean\/kh\u00f4ng n\u00ean d\u00f9ng set. B\u1eaft \u0111\u1ea7u n\u00e0o&#8230;!!!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. T\u1ed5ng quan v\u1ec1 <code>set<\/code> trong Python<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>set<\/code> trong Python l\u00e0 m\u1ed9t t\u1eadp h\u1ee3p c\u00e1c ph\u1ea7n t\u1eed <strong>kh\u00f4ng tr\u00f9ng l\u1eb7p<\/strong> v\u00e0 <strong>kh\u00f4ng c\u00f3 th\u1ee9 t\u1ef1<\/strong>. M\u1ed7i ph\u1ea7n t\u1eed ph\u1ea3i l\u00e0 <strong>hashable<\/strong> (v\u00ed d\u1ee5: s\u1ed1, chu\u1ed7i, tuple ch\u1ee9a c\u00e1c ph\u1ea7n t\u1eed hashable). <code>set<\/code> \u0111\u01b0\u1ee3c tri\u1ec3n khai tr\u00ean c\u01a1 s\u1edf <strong>hash table<\/strong>, v\u00ec v\u1eady thao t\u00e1c ki\u1ec3m tra th\u00e0nh vi\u00ean (<code>x in my_set<\/code>) th\u01b0\u1eddng c\u1ef1c nhanh \u2014 trung b\u00ecnh l\u00e0 O(1).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">V\u00ed d\u1ee5 kh\u1edfi t\u1ea1o:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly># Create a set of integers\nmy_set = {10, 20, 30, 40}\n\n# Create an empty set\nempty_set = set()  # {} would create an empty dict, not set\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\"># Create a set of integers<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">my_set <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #B48EAD\">10<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">20<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">30<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">40<\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Create an empty set<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">empty_set <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">set<\/span><span style=\"color: #ECEFF4\">()<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># {} would create an empty dict, not set<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">L\u01b0u \u00fd: n\u1ebfu b\u1ea1n c\u1ea7n m\u1ed9t t\u1eadp b\u1ea5t bi\u1ebfn (hashable) \u0111\u1ec3 d\u00f9ng l\u00e0m key trong dict ho\u1eb7c l\u00e0m ph\u1ea7n t\u1eed c\u1ee7a set kh\u00e1c, d\u00f9ng <code>frozenset<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Ki\u1ec3m tra th\u00e0nh vi\u00ean (Membership test) \u2014 ph\u01b0\u01a1ng ph\u00e1p c\u01a1 b\u1ea3n nh\u1ea5t<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Ki\u1ec3m tra <code>x in my_set<\/code> l\u00e0 c\u00e1ch t\u00ecm ph\u1ea7n t\u1eed ph\u1ed5 bi\u1ebfn nh\u1ea5t. V\u00ec set d\u00f9ng hash table, thao t\u00e1c n\u00e0y <strong>r\u1ea5t nhanh<\/strong> tr\u00ean h\u1ea7u h\u1ebft d\u1eef li\u1ec7u.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">V\u00ed d\u1ee5:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly># Example: membership test\nvalues = {100, 200, 300, 400}\ntarget = 300\n\nif target in values:\n    print(\"Found\", target)\nelse:\n    print(\"Not found\")\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\"># Example: membership test<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">values <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #B48EAD\">100<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">200<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">300<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">400<\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">target <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">300<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">if<\/span><span style=\"color: #D8DEE9FF\"> target <\/span><span style=\"color: #81A1C1\">in<\/span><span style=\"color: #D8DEE9FF\"> values<\/span><span style=\"color: #ECEFF4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Found<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> target<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">else<\/span><span style=\"color: #ECEFF4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Not found<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Gi\u1ea3i th\u00edch k\u1ef9 thu\u1eadt<\/strong>: khi d\u00f9ng <code>in<\/code>, Python t\u00ednh <code>hash(target)<\/code> r\u1ed3i \u0111i th\u1eb3ng t\u1edbi bucket t\u01b0\u01a1ng \u1ee9ng trong hash table. N\u1ebfu c\u00f3 collision (hai gi\u00e1 tr\u1ecb kh\u00e1c nhau c\u00f9ng bucket), Python so s\u00e1nh b\u1eb1ng <code>==<\/code> c\u00e1c ph\u1ea7n t\u1eed trong bucket. Trung b\u00ecnh O(1), nh\u01b0ng trong tr\u01b0\u1eddng h\u1ee3p x\u1ea5u \u2014 r\u1ea5t hi\u1ebfm \u2014 \u0111\u1ed9 ph\u1ee9c t\u1ea1p c\u00f3 th\u1ec3 tr\u1edf th\u00e0nh O(n) n\u1ebfu nhi\u1ec1u collision ho\u1eb7c t\u1ea5n c\u00f4ng hash.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. T\u00ecm m\u1ed9t ph\u1ea7n t\u1eed c\u1ee5 th\u1ec3 \u2014 c\u00e1c ph\u01b0\u01a1ng th\u1ee9c h\u1eefu d\u1ee5ng<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>in<\/code> \/ <code>not in<\/code>: nhanh, d\u00f9ng trong \u0111i\u1ec1u ki\u1ec7n.<\/li>\n\n\n\n<li><code>set.remove(x)<\/code> vs <code>set.discard(x)<\/code>: n\u1ebfu mu\u1ed1n x\u00f3a ph\u1ea7n t\u1eed sau khi t\u00ecm th\u1ea5y; <code>remove<\/code> s\u1ebd raise <code>KeyError<\/code> n\u1ebfu kh\u00f4ng t\u1ed3n t\u1ea1i, <code>discard<\/code> th\u00ec im l\u1eb7ng.<\/li>\n\n\n\n<li><code>set.pop()<\/code>: l\u1ea5y v\u00e0 x\u00f3a m\u1ed9t ph\u1ea7n t\u1eed <strong>ng\u1eabu nhi\u00ean<\/strong> (kh\u00f4ng theo th\u1ee9 t\u1ef1).<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">V\u00ed d\u1ee5:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly># Remove element safely\nitems = {\"apple\", \"banana\", \"cherry\"}\ntarget = \"banana\"\n\n# safe removal\nitems.discard(target)  # does nothing if target not in set\n\n# removal that raises if missing\ntry:\n    items.remove(\"durian\")\nexcept KeyError:\n    print(\"durian not in set\")\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\"># Remove element safely<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">items <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">apple<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">banana<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">cherry<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">target <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">banana<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># safe removal<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">items<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">discard<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">target<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># does nothing if target not in set<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># removal that raises if missing<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">try<\/span><span style=\"color: #ECEFF4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    items<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">remove<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">durian<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">except<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">KeyError<\/span><span style=\"color: #ECEFF4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">durian not in set<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">4. T\u00ecm ph\u1ea7n t\u1eed theo \u0111i\u1ec1u ki\u1ec7n (conditional search)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Set kh\u00f4ng h\u1ed7 tr\u1ee3 indexing hay truy v\u1ea5n theo \u0111i\u1ec1u ki\u1ec7n nhanh (kh\u00f4ng c\u00f3 ch\u1ec9 m\u1ee5c). N\u1ebfu b\u1ea1n mu\u1ed1n t\u00ecm ph\u1ea7n t\u1eed th\u1ecfa \u0111i\u1ec1u ki\u1ec7n (v\u00ed d\u1ee5: first element &gt; 10), b\u1eaft bu\u1ed9c <strong>duy\u1ec7t<\/strong> c\u00e1c ph\u1ea7n t\u1eed trong set \u2014 chi ph\u00ed O(n). Tuy nhi\u00ean v\u1eabn c\u00f3 nh\u1eefng idiom h\u1eefu \u00edch:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>D\u00f9ng <code>next()<\/code> v\u1edbi generator expression \u0111\u1ec3 tr\u1ea3 v\u1ec1 ph\u1ea7n t\u1eed \u0111\u1ea7u ti\u00ean th\u1ecfa \u0111i\u1ec1u ki\u1ec7n (ho\u1eb7c None n\u1ebfu kh\u00f4ng c\u00f3).<\/li>\n\n\n\n<li>D\u00f9ng set comprehension \u0111\u1ec3 l\u1ecdc ra m\u1ed9t t\u1eadp con th\u1ecfa \u0111i\u1ec1u ki\u1ec7n.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">V\u00ed d\u1ee5:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly># Find first even number in set (if any)\nnumbers = {3, 7, 10, 13, 22}\n\nfirst_even = next((x for x in numbers if x % 2 == 0), None)\n# first_even is 10 or 22 (depends on iteration order, which is arbitrary)\nprint(\"First even:\", first_even)\n\n# Find all numbers greater than 10\ngreater_than_10 = {x for x in numbers if x > 10}\nprint(\"Greater than 10:\", greater_than_10)\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\"># Find first even number in set (if any)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">numbers <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #B48EAD\">3<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">7<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">10<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">13<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">22<\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">first_even <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">next<\/span><span style=\"color: #ECEFF4\">((<\/span><span style=\"color: #D8DEE9FF\">x <\/span><span style=\"color: #81A1C1\">for<\/span><span style=\"color: #D8DEE9FF\"> x <\/span><span style=\"color: #81A1C1\">in<\/span><span style=\"color: #D8DEE9FF\"> numbers <\/span><span style=\"color: #81A1C1\">if<\/span><span style=\"color: #D8DEE9FF\"> x <\/span><span style=\"color: #81A1C1\">%<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">==<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">0<\/span><span style=\"color: #ECEFF4\">),<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">None<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># first_even is 10 or 22 (depends on iteration order, which is arbitrary)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">First even:<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> first_even<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Find all numbers greater than 10<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">greater_than_10 <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #D8DEE9FF\">x <\/span><span style=\"color: #81A1C1\">for<\/span><span style=\"color: #D8DEE9FF\"> x <\/span><span style=\"color: #81A1C1\">in<\/span><span style=\"color: #D8DEE9FF\"> numbers <\/span><span style=\"color: #81A1C1\">if<\/span><span style=\"color: #D8DEE9FF\"> x <\/span><span style=\"color: #81A1C1\">&gt;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">10<\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Greater than 10:<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> greater_than_10<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Ghi ch\u00fa:<\/strong> v\u00ec set kh\u00f4ng c\u00f3 th\u1ee9 t\u1ef1 c\u1ed1 \u0111\u1ecbnh, \u201cfirst\u201d ch\u1ec9 c\u00f3 \u00fd ngh\u0129a theo th\u1ee9 t\u1ef1 l\u1eb7p hi\u1ec7n t\u1ea1i \u2014 n\u1ebfu b\u1ea1n c\u1ea7n th\u1ee9 t\u1ef1 x\u00e1c \u0111\u1ecbnh, h\u00e3y chuy\u1ec3n sang list ho\u1eb7c sort k\u1ebft qu\u1ea3.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. T\u00ecm nhi\u1ec1u ph\u1ea7n t\u1eed c\u00f9ng l\u00fac \u2014 t\u1eadn d\u1ee5ng ph\u00e9p to\u00e1n t\u1eadp h\u1ee3p<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Khi b\u1ea1n mu\u1ed1n bi\u1ebft trong m\u1ed9t danh s\u00e1ch (ho\u1eb7c t\u1eadp) c\u00e1c gi\u00e1 tr\u1ecb n\u00e0o t\u1ed3n t\u1ea1i trong set hi\u1ec7n c\u00f3, <strong>ph\u00e9p giao (intersection)<\/strong> l\u00e0 r\u1ea5t hi\u1ec7u qu\u1ea3: <code>requested &amp; existing<\/code>. \u0110\u00e2y l\u00e0 thao t\u00e1c O(min(len(requested), len(existing))) trung b\u00ecnh.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">V\u00ed d\u1ee5:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly># Find which requested IDs exist in database_set\ndatabase_set = {101, 102, 103, 150, 200}\nrequested_ids = {100, 102, 150, 300}\n\npresent = requested_ids &amp; database_set  # intersection\nprint(\"Present IDs:\", present)  # {102, 150}\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\"># Find which requested IDs exist in database_set<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">database_set <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #B48EAD\">101<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">102<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">103<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">150<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">200<\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">requested_ids <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #B48EAD\">100<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">102<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">150<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">300<\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">present <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> requested_ids <\/span><span style=\"color: #81A1C1\">&amp;<\/span><span style=\"color: #D8DEE9FF\"> database_set  <\/span><span style=\"color: #616E88\"># intersection<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Present IDs:<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> present<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># {102, 150}<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">N\u1ebfu <code>requested_ids<\/code> l\u00e0 list d\u00e0i, b\u1ea1n c\u00f3 th\u1ec3 chuy\u1ec3n n\u00f3 sang set tr\u01b0\u1edbc \u0111\u1ec3 t\u1ed1i \u01b0u.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. Khi c\u1ea7n th\u1ee9 t\u1ef1 ho\u1eb7c t\u00ecm ki\u1ebfm theo ph\u1ea1m vi: chuy\u1ec3n sang c\u1ea5u tr\u00fac c\u00f3 th\u1ee9 t\u1ef1<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Set <strong>kh\u00f4ng h\u1ed7 tr\u1ee3<\/strong> t\u00ecm ki\u1ebfm theo ph\u1ea1m vi (range queries) hay binary search v\u00ec kh\u00f4ng c\u00f3 th\u1ee9 t\u1ef1. N\u1ebfu b\u1ea1n c\u1ea7n t\u00ecm ph\u1ea7n t\u1eed theo ph\u1ea1m vi (v\u00ed d\u1ee5: &#8220;t\u00ecm gi\u00e1 tr\u1ecb nh\u1ecf nh\u1ea5t l\u1edbn h\u01a1n X&#8221;), b\u1ea1n ph\u1ea3i:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Chuy\u1ec3n set sang list.<\/li>\n\n\n\n<li>S\u1eafp x\u1ebfp list (O(n log n)).<\/li>\n\n\n\n<li>D\u00f9ng <code>bisect<\/code> \u0111\u1ec3 t\u00ecm v\u1ecb tr\u00ed (O(log n) cho m\u1ed7i truy v\u1ea5n).<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">V\u00ed d\u1ee5:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>import bisect\n\n# Range query example: find smallest number > target\nnumbers_set = {3, 7, 10, 13, 22}\nnumbers_list = sorted(numbers_set)  # O(n log n)\n\ntarget = 11\nindex = bisect.bisect_right(numbers_list, target)\nresult = numbers_list&#91;index&#93; if index &lt; len(numbers_list) else None\nprint(\"Smallest > target:\", result)\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">import<\/span><span style=\"color: #D8DEE9FF\"> bisect<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Range query example: find smallest number &gt; target<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">numbers_set <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #B48EAD\">3<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">7<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">10<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">13<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">22<\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">numbers_list <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">sorted<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">numbers_set<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># O(n log n)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">target <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">11<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">index <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> bisect<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">bisect_right<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">numbers_list<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> target<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">result <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> numbers_list<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #D8DEE9FF\">index<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">if<\/span><span style=\"color: #D8DEE9FF\"> index <\/span><span style=\"color: #81A1C1\">&lt;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">len<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">numbers_list<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">else<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">None<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Smallest &gt; target:<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> result<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>L\u01b0u \u00fd<\/strong>: n\u1ebfu b\u1ea1n ph\u1ea3i th\u1ef1c hi\u1ec7n nhi\u1ec1u truy v\u1ea5n ph\u1ea1m vi tr\u00ean c\u00f9ng dataset, vi\u1ec7c chuy\u1ec3n v\u00e0 s\u1eafp x\u1ebfp m\u1ed9t l\u1ea7n l\u00e0 h\u1ee3p l\u00fd. Nh\u01b0ng n\u1ebfu ch\u1ec9 m\u1ed9t truy v\u1ea5n duy nh\u1ea5t, kh\u00f4ng \u0111\u00e1ng \u0111\u1ec3 s\u1eafp x\u1ebfp \u2014 c\u00f3 th\u1ec3 qu\u00e9t O(n) \u0111\u1ec3 t\u00ecm min candidate.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">7. So s\u00e1nh set v\u1edbi c\u00e1c c\u1ea5u tr\u00fac kh\u00e1c khi t\u00ecm ki\u1ebfm<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Set<\/strong>: t\u1ed1t nh\u1ea5t cho membership tests nhi\u1ec1u l\u1ea7n, deduplication, ph\u00e9p to\u00e1n t\u1eadp h\u1ee3p. Trung b\u00ecnh O(1) cho <code>in<\/code>. T\u1ed1n b\u1ed9 nh\u1edb h\u01a1n list \u0111\u00f4i ch\u00fat (do hash table).<\/li>\n\n\n\n<li><strong>Dict<\/strong>: gi\u1ed1ng set v\u1ec1 hi\u1ec7u n\u0103ng, nh\u01b0ng l\u01b0u gi\u00e1 tr\u1ecb k\u00e8m key; d\u00f9ng khi c\u1ea7n mapping key\u2192value.<\/li>\n\n\n\n<li><strong>List\/Tuple<\/strong>: h\u1ed7 tr\u1ee3 indexing, s\u1eafp x\u1ebfp, range queries; membership l\u00e0 O(n) n\u1ebfu ch\u01b0a s\u1eafp x\u1ebfp. Tuy nhi\u00ean list c\u00f3 th\u1ee9 t\u1ef1 c\u1ed1 \u0111\u1ecbnh.<\/li>\n\n\n\n<li><strong>Sorted containers \/ bisect \/ sorted list<\/strong>: t\u1ed1t cho range queries v\u00e0 t\u00ecm ph\u1ea7n t\u1eed theo th\u1ee9 t\u1ef1 nh\u01b0ng th\u00eam\/ x\u00f3a c\u00f3 chi ph\u00ed cao h\u01a1n set trung b\u00ecnh.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Quy t\u1eafc ng\u1eafn: n\u1ebfu nhi\u1ec7m v\u1ee5 ch\u00ednh l\u00e0 <strong>ki\u1ec3m tra t\u1ed3n t\u1ea1i (yes\/no)<\/strong> nhi\u1ec1u l\u1ea7n \u2192 d\u00f9ng <code>set<\/code>. N\u1ebfu c\u1ea7n <strong>th\u1ee9 t\u1ef1<\/strong> ho\u1eb7c <strong>truy v\u1ea5n ph\u1ea1m vi<\/strong>, suy ngh\u0129 t\u1edbi list \u0111\u00e3 s\u1eafp x\u1ebfp ho\u1eb7c c\u1ea5u tr\u00fac chuy\u00ean d\u1ee5ng.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">8. C\u00e1c ph\u01b0\u01a1ng th\u1ee9c set h\u1eefu d\u1ee5ng li\u00ean quan t\u00ecm ki\u1ebfm v\u00e0 thao t\u00e1c<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">M\u1ed9t s\u1ed1 ph\u01b0\u01a1ng th\u1ee9c h\u1eefu \u00edch b\u1ea1n s\u1ebd d\u00f9ng th\u01b0\u1eddng xuy\u00ean:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>s.intersection(t)<\/code>, <code>s &amp; t<\/code> \u2014 ph\u1ea7n t\u1eed chung.<\/li>\n\n\n\n<li><code>s.union(t)<\/code>, <code>s | t<\/code> \u2014 h\u1ee3p t\u1eadp.<\/li>\n\n\n\n<li><code>s.difference(t)<\/code>, <code>s - t<\/code> \u2014 ph\u1ea7n t\u1eed c\u00f3 \u1edf s nh\u01b0ng kh\u00f4ng \u1edf t.<\/li>\n\n\n\n<li><code>s.isdisjoint(t)<\/code> \u2014 nhanh ki\u1ec3m tra kh\u00f4ng c\u00f3 ph\u1ea7n t\u1eed chung.<\/li>\n\n\n\n<li><code>s.issubset(t)<\/code> \/ <code>s.issuperset(t)<\/code> \u2014 ki\u1ec3m tra quan h\u1ec7 t\u1eadp.<\/li>\n\n\n\n<li><code>s.clear()<\/code>, <code>s.pop()<\/code> \u2014 thao t\u00e1c thay \u0111\u1ed5i t\u1eadp.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">V\u00ed d\u1ee5:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>a = {1, 2, 3}\nb = {3, 4, 5}\n\nprint(\"Intersection:\", a &amp; b)  # {3}\nprint(\"Is disjoint:\", a.isdisjoint(b))  # False\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">a <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">3<\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">b <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #B48EAD\">3<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">4<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">5<\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Intersection:<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> a <\/span><span style=\"color: #81A1C1\">&amp;<\/span><span style=\"color: #D8DEE9FF\"> b<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># {3}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Is disjoint:<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> a<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">isdisjoint<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">b<\/span><span style=\"color: #ECEFF4\">))<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># False<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">9. K\u1ebft lu\u1eadn<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Set l\u00e0 c\u00f4ng c\u1ee5 r\u1ea5t m\u1ea1nh khi m\u1ee5c ti\u00eau ch\u00ednh c\u1ee7a b\u1ea1n l\u00e0 <strong>t\u00ecm ki\u1ebfm nhanh<\/strong> (membership) v\u00e0 thao t\u00e1c t\u1eadp h\u1ee3p. Nh\u1edd tri\u1ec3n khai b\u1eb1ng hash table, <code>in<\/code> tr\u00ean set th\u01b0\u1eddng th\u1ef1c hi\u1ec7n trong th\u1eddi gian h\u1eb1ng s\u1ed1 trung b\u00ecnh \u2014 \u0111\u00e2y l\u00e0 l\u1ee3i th\u1ebf l\u1edbn so v\u1edbi list\/tuple khi ph\u1ea3i ki\u1ec3m tra m\u1ed9t gi\u00e1 tr\u1ecb nhi\u1ec1u l\u1ea7n.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Tuy nhi\u00ean, \u0111\u1ec3 d\u00f9ng set hi\u1ec7u qu\u1ea3 c\u1ea7n l\u01b0u \u00fd m\u1ed9t s\u1ed1 \u0111i\u1ec3m then ch\u1ed1t:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>X\u00e1c \u0111\u1ecbnh y\u00eau c\u1ea7u truy v\u1ea5n<\/strong>: n\u1ebfu b\u1ea1n ch\u1ec9 ki\u1ec3m tra t\u1ed3n t\u1ea1i \u2192 set l\u00e0 l\u1ef1a ch\u1ecdn h\u1ee3p l\u00fd. N\u1ebfu b\u1ea1n c\u1ea7n th\u1ee9 t\u1ef1, hay truy v\u1ea5n theo ph\u1ea1m vi (range queries) \u2192 set kh\u00f4ng ph\u00f9 h\u1ee3p, chuy\u1ec3n sang list \u0111\u00e3 s\u1eafp x\u1ebfp ho\u1eb7c c\u1ea5u tr\u00fac chuy\u00ean d\u1ee5ng.<\/li>\n\n\n\n<li><strong>T\u00ednh b\u1ea5t bi\u1ebfn c\u1ee7a ph\u1ea7n t\u1eed<\/strong>: set ch\u1ec9 ch\u1ee9a ph\u1ea7n t\u1eed hashable. Khi thi\u1ebft k\u1ebf d\u1eef li\u1ec7u, h\u00e3y ch\u1ecdn key\/types ph\u00f9 h\u1ee3p (s\u1eed d\u1ee5ng tuple, frozenset n\u1ebfu c\u1ea7n l\u00e0m ph\u1ea7n t\u1eed trong set).<\/li>\n\n\n\n<li><strong>Precompute &amp; reuse<\/strong>: n\u1ebfu \u1ee9ng d\u1ee5ng th\u1ef1c hi\u1ec7n h\u00e0ng ng\u00e0n hay tri\u1ec7u ki\u1ec3m tra membership, t\u1ea1o set m\u1ed9t l\u1ea7n v\u00e0 t\u00e1i s\u1eed d\u1ee5ng s\u1ebd ti\u1ebft ki\u1ec7m \u0111\u00e1ng k\u1ec3 th\u1eddi gian.<\/li>\n\n\n\n<li><strong>Ch\u00fa \u00fd b\u1ed9 nh\u1edb<\/strong>: hash table c\u1ee7a set ti\u00eau t\u1ed1n b\u1ed9 nh\u1edb h\u01a1n list t\u01b0\u01a1ng \u0111\u01b0\u01a1ng, n\u00ean c\u00e2n nh\u1eafc v\u1edbi data c\u1ef1c l\u1edbn.<\/li>\n\n\n\n<li><strong>K\u1ebft h\u1ee3p v\u1edbi c\u1ea5u tr\u00fac kh\u00e1c khi c\u1ea7n<\/strong>: \u0111\u00f4i khi c\u1ea7n k\u1ebft h\u1ee3p set \u0111\u1ec3 ki\u1ec3m tra membership nhanh v\u00e0 list \u0111\u1ec3 gi\u1eef th\u1ee9 t\u1ef1 (v\u00ed d\u1ee5: duy\u1ec7t list nh\u01b0ng d\u00f9ng set \u0111\u1ec3 ki\u1ec3m tra filter condition).<\/li>\n\n\n\n<li><strong>C\u1ea9n tr\u1ecdng v\u1edbi security<\/strong>: trong c\u00e1c \u1ee9ng d\u1ee5ng d\u1ec5 b\u1ecb t\u1ea5n c\u00f4ng (nh\u1eadn input t\u1eeb b\u00ean ngo\u00e0i), n\u1ebfu kh\u1ea3 n\u0103ng t\u1ea5n c\u00f4ng g\u00e2y nhi\u1ec1u hash-collisions l\u00e0 m\u1ed1i lo, c\u00e2n nh\u1eafc c\u01a1 ch\u1ebf ph\u00f2ng ng\u1eeba ho\u1eb7c c\u1ea5u tr\u00fac thay th\u1ebf (nh\u01b0ng th\u1ef1c t\u1ebf v\u1edbi Python hi\u1ec7n \u0111\u1ea1i \u0111\u00e2y hi\u1ebfm khi l\u00e0 v\u1ea5n \u0111\u1ec1).<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">T\u00f3m l\u1ea1i, khi nhi\u1ec7m v\u1ee5 ch\u00ednh l\u00e0 <strong>tra c\u1ee9u nhanh<\/strong> v\u00e0 <strong>lo\u1ea1i tr\u00f9ng<\/strong>, <code>set<\/code> l\u00e0 c\u00f4ng c\u1ee5 \u201cgo-to\u201d trong Python. Hi\u1ec3u r\u00f5 trade-offs (th\u1ee9 t\u1ef1, hashability, memory) s\u1ebd gi\u00fap b\u1ea1n \u1ee9ng d\u1ee5ng \u0111\u00fang n\u01a1i \u2014 l\u00e0m ch\u01b0\u01a1ng tr\u00ecnh nhanh, g\u1ecdn v\u00e0 an to\u00e0n h\u01a1n.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">L\u1eddi khuy\u00ean cho b\u1ea1n: n\u1ebfu b\u1ea1n th\u1ea5y nhi\u1ec1u ch\u1ed7 trong code g\u1ecdi <code><mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">if x in collection<\/mark><\/code> l\u1eb7p l\u1ea1i tr\u00ean c\u00f9ng m\u1ed9t collection \u2192 \u0111\u00f3 l\u00e0 l\u00fac r\u1ea5t n\u00ean chuy\u1ec3n collection \u0111\u00f3 sang <code>set<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">10. T\u00e0i li\u1ec7u tham kh\u1ea3o<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Python Software Foundation. <em>Built-in Types \u2014 set, frozenset<\/em>. Python Documentation. Truy c\u1eadp t\u1ea1i: <a>https:\/\/docs.python.org\/3\/library\/stdtypes.html#set<\/a><\/li>\n\n\n\n<li>Python Software Foundation. <em>Data model \u2014 object.<strong>hash<\/strong> and hashing<\/em>. Python Documentation. Truy c\u1eadp t\u1ea1i: <a>https:\/\/docs.python.org\/3\/reference\/datamodel.html#object.__hash<\/a>__<\/li>\n\n\n\n<li>Cormen, T. H., Leiserson, C. E., Rivest, R. L., &amp; Stein, C. (2009). <em>Introduction to Algorithms<\/em> (3rd ed.). MIT Press.<\/li>\n\n\n\n<li>Python for Professionals: Learning Python as a Second Language: <a href=\"https:\/\/click.linksynergy.com\/link?id=*C\/UgjGtUZ8&amp;offerid=1562891.3721710002222624882405978&amp;type=15&amp;murl=https%3A%2F%2Fwww.kobo.com%2Fus%2Fen%2Febook%2Fpython-for-professionals-3\" target=\"_blank\" rel=\"noopener\">https:\/\/www.kobo.com\/us\/en\/ebook\/python-for-professionals-3<\/a><\/li>\n\n\n\n<li>Python: Deeper Insights into Machine Learning: <a href=\"https:\/\/click.linksynergy.com\/link?id=*C\/UgjGtUZ8&amp;offerid=1562891.3721710015810095319857183&amp;type=15&amp;murl=https%3A%2F%2Fwww.kobo.com%2Fus%2Fen%2Febook%2Fpython-deeper-insights-into-machine-learning\" target=\"_blank\" rel=\"noopener\">https:\/\/www.kobo.com\/us\/en\/ebook\/python-deeper-insights-into-machine-learning<\/a><\/li>\n\n\n\n<li>DataFusion Python Bindings in Practice: The Complete Guide for Developers and Engineers: <a href=\"https:\/\/click.linksynergy.com\/link?id=*C\/UgjGtUZ8&amp;offerid=1562891.3721710049093362364820452&amp;type=15&amp;murl=https%3A%2F%2Fwww.kobo.com%2Fus%2Fen%2Febook%2Fdatafusion-python-bindings-in-practice\" target=\"_blank\" rel=\"noopener\">https:\/\/www.kobo.com\/us\/en\/ebook\/datafusion-python-bindings-in-practice<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Trong Python, set l\u00e0 m\u1ed9t c\u1ea5u tr\u00fac d\u1eef li\u1ec7u m\u1ea1nh m\u1ebd d\u00e0nh cho c\u00e1c b\u00e0i to\u00e1n li\u00ean quan t\u1edbi ki\u1ec3m tra th\u00e0nh vi\u00ean, lo\u1ea1i b\u1ecf ph\u1ea7n t\u1eed tr\u00f9ng l\u1eafp v\u00e0 c\u00e1c ph\u00e9p to\u00e1n t\u1eadp h\u1ee3p (union, intersection, difference&#8230;). N\u1ebfu m\u1ee5c ti\u00eau c\u1ee7a b\u1ea1n l\u00e0 t\u00ecm ki\u1ebfm ph\u1ea7n t\u1eed nhanh (membership queries) ho\u1eb7c ki\u1ec3m tra t\u1ed3n&#8230;<\/p>\n","protected":false},"author":1,"featured_media":824,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"googlesitekit_rrm_CAowieHDDA:productID":"","footnotes":""},"categories":[41],"tags":[40],"class_list":["post-1618","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kien-thuc-lap-trinh","tag-python-co-ban"],"_links":{"self":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts\/1618","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/comments?post=1618"}],"version-history":[{"count":3,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts\/1618\/revisions"}],"predecessor-version":[{"id":2463,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts\/1618\/revisions\/2463"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/media\/824"}],"wp:attachment":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/media?parent=1618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/categories?post=1618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/tags?post=1618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}