{"id":1650,"date":"2018-12-31T14:03:20","date_gmt":"2018-12-31T13:03:20","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=1650"},"modified":"2018-12-31T18:17:45","modified_gmt":"2018-12-31T17:17:45","slug":"counting-elements-in-a-list","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/counting-elements-in-a-list\/","title":{"rendered":"Counting elements in a list"},"content":{"rendered":"<p>If you need to count elements in a list you could code this:<\/p>\n<pre class=\"lang:default decode:true \">from collections import Counter\r\na = [1,2,3,1,1,2,2,3]\r\nb = Counter(a)\r\nprint(b)<\/pre>\n<p>The result being this:<\/p>\n<pre class=\"lang:default decode:true \">Counter({1: 3, 2: 3, 3: 2})<\/pre>\n<p>But you can do it also like this:<\/p>\n<pre class=\"lang:default decode:true \">k = {}\r\nfor v in a:\r\n\tif v in k:\r\n\t\tk[v] += 1\r\n\telse:\r\n\t\tk[v] = 1\r\n\r\nprint(k)<\/pre>\n<p>The result:<\/p>\n<pre class=\"lang:default decode:true \">{1: 3, 2: 3, 3: 2}<\/pre>\n<h2>Counting words<\/h2>\n<p>If you want you can do so, to count the words in a string:<\/p>\n<pre class=\"lang:default decode:true \">rej = \"\"\"\r\nTwo households, both alike in dignity,\r\nIn fair Verona, where we lay our scene,\r\nFrom ancient grudge break to new mutiny,\r\nWhere civil blood makes civil hands unclean.\r\nFrom forth the fatal loins of these two foes\r\nA pair of star-cross'd lovers take their life;\r\nWhose misadventured piteous overthrows\r\nDo with their death bury their parents' strife.\r\nThe fearful passage of their death-mark'd love,\r\nAnd the continuance of their parents' rage,\r\nWhich, but their children's end, nought could remove,\r\nIs now the two hours' traffic of our stage;\r\nThe which if you with patient ears attend,\r\nWhat here shall miss, our toil shall strive to mend.\r\n\"\"\"\r\n\r\ndef wordcount(a):\r\n\ta = a.replace(\",\",\"\").replace(\".\",\"\").replace(\";\",\"\").split()\r\n\tk = {}\r\n\tfor v in a:\r\n\t\tif v in k:\r\n\t\t\tk[v] += 1\r\n\t\telse:\r\n\t\t\tk[v] = 1\r\n\treturn k\r\n\r\nw = wordcount(rej)\r\nprint(w)<\/pre>\n<p>The result:<\/p>\n<pre class=\"lang:default decode:true \">{'Two': 1, 'households': 1, 'both': 1, 'alike': 1, 'in': 1, 'dignity': 1, 'In': 1, 'fair': 1, 'Verona': 1, 'where': 1, 'we': 1, 'lay': 1, 'our': 3, 'scene': 1, 'From': 2, 'ancient': 1, 'grudge': 1, 'break': 1, 'to': 2, 'new': 1, 'mutiny': 1, 'Where': 1, 'civil': 2, 'blood': 1, 'makes': 1, 'hands': 1, 'unclean': 1, 'forth': 1, 'the': 3, 'fatal': 1, 'loins': 1, 'of': 5, 'these': 1, 'two': 2, 'foes': 1, 'A': 1, 'pair': 1, \"star-cross'd\": 1, 'lovers': 1, 'take': 1, 'their': 6, 'life': 1, 'Whose': 1, 'misadventured': 1, 'piteous': 1, 'overthrows': 1, 'Do': 1, 'with': 2, 'death': 1, 'bury': 1, \"parents'\": 2, 'strife': 1, 'The': 2, 'fearful': 1, 'passage': 1, \"death-mark'd\": 1, 'love': 1, 'And': 1, 'continuance': 1, 'rage': 1, 'Which': 1, 'but': 1, \"children's\": 1, 'end': 1, 'nought': 1, 'could': 1, 'remove': 1, 'Is': 1, 'now': 1, \"hours'\": 1, 'traffic': 1, 'stage': 1, 'which': 1, 'if': 1, 'you': 1, 'patient': 1, 'ears': 1, 'attend': 1, 'What': 1, 'here': 1, 'shall': 2, 'miss': 1, 'toil': 1, 'strive': 1, 'mend': 1}<\/pre>\n<p><iframe loading=\"lazy\" title=\"Count the elements of a list with Python\" width=\"747\" height=\"420\" src=\"https:\/\/www.youtube.com\/embed\/_K0euDOC7TY?feature=oembed&amp;enablejsapi=1\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"How to count elements i a list.\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/counting-elements-in-a-list\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":1026,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[210],"tags":[],"class_list":["post-1650","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-basics"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":false,"av_sharing_on":{"fb":[],"tw":[]},"av_allow_affiliate_banner":false,"av_allow_affiliate_multi_banner":false,"av_show_affiliation_buy_button":false,"av_post_rating":true,"av_have_post_rating_value":false,"av_is_artificial_intelligence_content":false,"_links":{"self":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/1650","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/comments?post=1650"}],"version-history":[{"count":2,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/1650\/revisions"}],"predecessor-version":[{"id":1652,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/1650\/revisions\/1652"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/1026"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=1650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=1650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=1650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}