{"id":511,"date":"2013-02-18T18:09:32","date_gmt":"2013-02-18T17:09:32","guid":{"rendered":"http:\/\/it4bus.vn\/blog\/?p=511"},"modified":"2014-08-08T19:22:33","modified_gmt":"2014-08-08T17:22:33","slug":"count-the-times-a-word-appears-in-a-given-string","status":"publish","type":"post","link":"https:\/\/www.itersdesktop.com\/vi\/2013\/02\/18\/count-the-times-a-word-appears-in-a-given-string\/","title":{"rendered":"\u0110\u1ebfm s\u1ed1 l\u1ea7n xu\u1ea5t hi\u1ec7n m\u1ed9t t\u1eeb trong chu\u1ed7i k\u00ed t\u1ef1"},"content":{"rendered":"<p><em>L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 \u0111\u1ebfm xem m\u1ed9t t\u1eeb &#8220;word&#8221; xu\u1ea5t hi\u1ec7n bao nhi\u00eau l\u1ea7n trong m\u1ed9t chu\u1ed7i cho tr\u01b0\u1edbc &#8220;string&#8221;. C\u00f3 m\u1ed9t s\u1ed1 c\u00e1ch c\u00f3 th\u1ec3 tham kh\u1ea3o nh\u01b0 sau: D\u00f9ng h\u00e0m <strong>split\u00a0<\/strong>\u0111\u1ec3 t\u00e1ch c\u00e1c t\u1eeb trong chu\u1ed7i.<\/em><\/p>\n<pre dir=\"ltr\">string = \"I am dude a string dude and dude this dude dude is really starting to dude me dufe off\"\r\nnum = 0\r\nfor i in string.split():\r\n    if i == \"dude\":\r\n        num += 1\r\nprint \"There are %s instances of 'dude' in '%s'\" % (num, string)<\/pre>\n<p>Tr\u00ean \u0111\u00e2y l\u00e0 c\u00e1ch \u0111\u01a1n gi\u1ea3n nh\u1ea5t. M\u1ed9t c\u00e1ch kh\u00e1c l\u00e0:<\/p>\n<pre dir=\"ltr\">string = \"word anyone here word, dude word word www internet python is awesome lorum ipsum word\"\r\nkey = \"word\"\r\n\r\ndef count_words(string, key):\r\n\treturn float(len(string) - len(string.replace(key, ''))) \/ float(len(key))\r\n\r\nnum = count_words(string, key)\r\nprint \"string '%s' occurs %d times in string '%s'.\" % (key, num, string)<\/pre>\n<p>Ho\u1eb7c d\u00f9ng ph\u01b0\u01a1ng th\u1ee9c count c\u1ee7a l\u1edbp string, nh\u01b0 sau:<\/p>\n<pre>s = 'Nguyen Vu Ngoc Tung Tung Tung'\r\ns.count('Tung')<\/pre>\n<p>N\u1ebfu c\u1ea7n t\u00ecm m\u1ed9t t\u1eeb xu\u1ea5t hi\u1ec7n bao nhi\u00eau l\u1ea7n trong m\u1ed9t danh s\u00e1ch t\u1eeb th\u00ec ch\u00fang ta c\u00f3 th\u1ec3 \u00e1p d\u1ee5ng \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a l\u1edbp <a href=\"https:\/\/docs.python.org\/3.4\/library\/collections.html#counter-objects\">Counter<\/a> c\u1ee7a module <a href=\"https:\/\/docs.python.org\/3.4\/library\/collections.html\">collections<\/a>. Xem th\u00eam b\u00e0i <a href=\"http:\/\/www.itersdesktop.com\/2013\/12\/17\/counting-how-many-times-each-string-appears-in-my-list\/\">T\u00ednh s\u1ed1 l\u1ea7n xu\u1ea5t hi\u1ec7n m\u1ed9t chu\u1ed7i trong m\u1ed9t danh s\u00e1ch<\/a>.<\/p>\n<div><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1749\" src=\"http:\/\/www.itersdesktop.com\/wp-content\/uploads\/2013\/02\/wordcount.png\" alt=\"wordcount\" width=\"858\" height=\"246\" srcset=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2013\/02\/wordcount.png 858w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2013\/02\/wordcount-300x86.png 300w\" sizes=\"auto, (max-width: 858px) 100vw, 858px\" \/><\/div>\n<h3><span style=\"color: #339966;\">Tham kh\u1ea3o<\/span><\/h3>\n<div><a href=\"http:\/\/docs.python.org\/2\/library\/stdtypes.html#str.count\">http:\/\/docs.python.org\/2\/library\/stdtypes.html#str.count<\/a><\/p>\n<div><a href=\"http:\/\/ubuntuforums.org\/showthread.php?t=1232488\">http:\/\/ubuntuforums.org\/showthread.php?t=1232488<\/a>   <\/div>\n<\/div>\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>L\u00e0m th\u1ebf n\u00e0o \u0111\u1ebfm xem m\u1ed9t &#8220;word&#8221; xu\u1ea5t hi\u1ec7n bao nhi\u00eau l\u1ea7n trong m\u1ed9t &#8220;string&#8221; cho tr\u01b0\u1edbc. C\u00e1ch \u0111\u01a1n gi\u1ea3n nh\u1ea5t nhi\u1ec1u ng\u01b0\u1eddi bi\u1ebft: t\u00e1ch chu\u1ed7i th\u00e0nh t\u1eebng t\u1eeb v\u00e0 so s\u00e1nh ch\u00fang v\u1edbi t\u1eeb c\u1ea7n t\u00ecm.<\/p>\n","protected":false},"author":2,"featured_media":1749,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[348,230,122],"tags":[125,102],"class_list":["post-511","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-structures","category-programming","category-python","tag-count","tag-string-operations"],"_links":{"self":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts\/511","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=511"}],"version-history":[{"count":18,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts\/511\/revisions"}],"predecessor-version":[{"id":1772,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts\/511\/revisions\/1772"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/media\/1749"}],"wp:attachment":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/media?parent=511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/categories?post=511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/tags?post=511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}