{"id":517,"date":"2013-12-17T21:23:28","date_gmt":"2013-12-17T20:23:28","guid":{"rendered":"http:\/\/it4bus.vn\/blog\/?p=517"},"modified":"2014-08-08T17:36:56","modified_gmt":"2014-08-08T15:36:56","slug":"counting-how-many-times-each-string-appears-in-my-list","status":"publish","type":"post","link":"https:\/\/www.itersdesktop.com\/vi\/2013\/12\/17\/counting-how-many-times-each-string-appears-in-my-list\/","title":{"rendered":"T\u00ednh s\u1ed1 l\u1ea7n xu\u1ea5t hi\u1ec7n m\u1ed9t chu\u1ed7i trong m\u1ed9t danh s\u00e1ch"},"content":{"rendered":"<p><em>Trong b\u00e0i vi\u1ebft h\u01b0\u1edbng d\u1eabn <a title=\"Count the times a word appears in a given string\" href=\"http:\/\/www.itersdesktop.com\/2013\/02\/18\/count-the-times-a-word-appears-in-a-given-string\/\" target=\"_blank\">c\u00e1ch \u0111\u1ebfm s\u1ed1 l\u1ea7n xu\u1ea5t hi\u1ec7n c\u1ee7a m\u1ed9t t\u1eeb trong chu\u1ed7i k\u00ed t\u1ef1<\/a>, ch\u00fang ta \u0111\u00e3 l\u00e0m quen v\u1edbi h\u00e0m count c\u1ee7a \u0111\u1ed1i t\u01b0\u1ee3ng string. \u0110\u1ec3 \u0111\u1ebfm s\u1ed1 l\u1ea7n xu\u1ea5t hi\u1ec7n m\u1ed9t chu\u1ed7i k\u00ed t\u1ef1 (th\u01b0\u1eddng l\u00e0 m\u1ed9t t\u1eeb) trong m\u1ed9t danh s\u00e1ch c\u00e1c chu\u1ed7i (t\u1eeb) cho tr\u01b0\u1edbc, theo d\u1ef1 \u0111o\u00e1n ch\u00fang ta c\u00f3 th\u1ec3 \u00e1p d\u1ee5ng t\u01b0\u01a1ng t\u1ef1 h\u00e0m count.<\/em><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a3cb17c14f24\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a3cb17c14f24\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.itersdesktop.com\/vi\/2013\/12\/17\/counting-how-many-times-each-string-appears-in-my-list\/#dan-nhap\" >D\u1eabn nh\u1eadp<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.itersdesktop.com\/vi\/2013\/12\/17\/counting-how-many-times-each-string-appears-in-my-list\/#cach-thuc-hien\" >C\u00e1ch th\u1ef1c hi\u1ec7n<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.itersdesktop.com\/vi\/2013\/12\/17\/counting-how-many-times-each-string-appears-in-my-list\/#tham-khao\" >Tham kh\u1ea3o<\/a><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"dan-nhap\"><\/span><span style=\"color: #008080; font-weight: bold;\">D\u1eabn nh\u1eadp<\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>T\u00ecm ki\u1ebfm lu\u00f4n l\u00e0 thao t\u00e1c \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng nhi\u1ec1u trong h\u1ea7u h\u1ebft c\u00e1c \u1ee9ng d\u1ee5ng. T\u00ecm ki\u1ebfm \u0111\u01a1n thu\u1ea7n \u0111\u1ec3 th\u1ea5y \u0111\u01b0\u1ee3c ph\u1ea7n t\u1eed c\u1ea7n t\u00ecm nh\u01b0 b\u1ea1n t\u00ecm t\u1eeb, c\u00e2u trong Word, Excel. B\u00e0i vi\u1ebft n\u00e0y mu\u1ed1n tr\u00ecnh b\u00e0y c\u00e1ch t\u00ecm ki\u1ebfm v\u00e0 \u0111\u1ebfm s\u1ed1 l\u1ea7n s\u1ef1 xu\u1ea5t hi\u1ec7n c\u1ee7a m\u1ed9t chu\u1ed7i trong danh s\u00e1ch h\u00e0ng lo\u1ea1t c\u00e1c chu\u1ed7i k\u00ed t\u1ef1. N\u00f3 c\u00f3 kh\u00e1c ch\u00fat \u0111\u1ec9nh so v\u1edbi b\u00e0i vi\u1ebft <a title=\"Count the times a word appears in a given string\" href=\"http:\/\/www.itersdesktop.com\/2013\/02\/18\/count-the-times-a-word-appears-in-a-given-string\/\">c\u00e1ch \u0111\u1ebfm s\u1ed1 l\u1ea7n xu\u1ea5t hi\u1ec7n c\u1ee7a m\u1ed9t t\u1eeb trong chu\u1ed1i k\u00ed t\u1ef1<\/a>, nh\u01b0ng t\u1ef1u chung l\u1ea1i l\u00e0 b\u00e0i to\u00e1n t\u00ecm ki\u1ebfm trong l\u1eadp tr\u00ecnh Python.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"cach-thuc-hien\"><\/span><span style=\"color: #008080; font-weight: bold;\">C\u00e1ch th\u1ef1c hi\u1ec7n<\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Tuy nhi\u00ean, ngo\u00e0i nh\u1eefng c\u00e1ch n\u00eau ra trong b\u00e0i vi\u1ebft \u0111\u00f3, \u1edf \u0111\u00e2y b\u00e0i n\u00e0y h\u01b0\u1edbng d\u1eabn c\u00e1ch l\u00e0m nh\u01b0 sau:<\/p>\n<pre class=\"decode-attributes:true lang:python decode:true\">from collections import Counter\r\ncnt = Counter()\r\nfor word in ['red','blue','green','red','blue','blue']:\r\n    cnt[word] += 1\r\nprint(cnt)<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1736\" src=\"http:\/\/www.itersdesktop.com\/wp-content\/uploads\/2013\/12\/CounterPython.png\" alt=\"CounterPython\" width=\"586\" height=\"239\" srcset=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2013\/12\/CounterPython.png 586w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2013\/12\/CounterPython-300x122.png 300w\" sizes=\"auto, (max-width: 586px) 100vw, 586px\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"tham-khao\"><\/span><span style=\"color: #008080; font-weight: bold;\">Tham kh\u1ea3o<\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<div><a href=\"http:\/\/stackoverflow.com\/questions\/11800755\/python-lists-finding-number-of-times-a-string-occurs\">python-lists-finding-number-of-times-a-string-occurs<\/a><\/div>\n<div><a href=\"http:\/\/stackoverflow.com\/questions\/11803298\/same-words-counting-in-text-file-using-python\">same-words-counting-in-text-file-using-python<\/a><\/div>\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>Trong b\u00e0i vi\u1ebft h\u01b0\u1edbng d\u1eabn c\u00e1ch \u0111\u1ebfm s\u1ed1 l\u1ea7n xu\u1ea5t hi\u1ec7n c\u1ee7a m\u1ed9t t\u1eeb trong chu\u1ed7i k\u00ed t\u1ef1, ch\u00fang ta \u0111\u00e3 l\u00e0m quen v\u1edbi h\u00e0m count c\u1ee7a \u0111\u1ed1i t\u01b0\u1ee3ng string.&hellip; <\/p>\n","protected":false},"author":2,"featured_media":1736,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[348,230,122],"tags":[125,347,123,346],"class_list":["post-517","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-structures","category-programming","category-python","tag-count","tag-list","tag-python-language","tag-string"],"_links":{"self":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts\/517","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/comments?post=517"}],"version-history":[{"count":15,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts\/517\/revisions"}],"predecessor-version":[{"id":1737,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts\/517\/revisions\/1737"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/media\/1736"}],"wp:attachment":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/media?parent=517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/categories?post=517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/tags?post=517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}