{"id":252,"date":"2015-04-27T12:53:22","date_gmt":"2015-04-27T12:53:22","guid":{"rendered":"http:\/\/blog.spcollege.edu\/information-technology\/?p=252"},"modified":"2024-01-30T16:04:18","modified_gmt":"2024-01-30T21:04:18","slug":"python-counter","status":"publish","type":"post","link":"https:\/\/blog.spcollege.edu\/technology-educational-information\/python-counter\/","title":{"rendered":"An Inside look at Python Counter"},"content":{"rendered":"<p>What we learn in school is just a tiny fraction of what is available to us as programmers. My goal in this series of blog posts is to introduce you to some of the more advance topics of several different languages. In this post I will talk specifically about the python collections Counter class.<\/p>\n<p>The other day I was writing some code to analyze a dataset. The data contained a dictionary of records representing <a href=\"https:\/\/bitly.com\/\"><span style=\"text-decoration: underline\">bit.ly<\/span><\/a> link data. A record looked like the following:<\/p>\n<p>{u&#8217;a&#8217;: u&#8217;Mozilla\/5.0 (Windows NT 6.1; WOW64) AppleWebKit\/535.11 (KHTML, like Gecko) Chrome\/17.0.963.78 Safari\/535.11&#8242;,<\/p>\n<p>u&#8217;al&#8217;: u&#8217;en-US,en;q=0.8&#8242;,<\/p>\n<p>u&#8217;c&#8217;: u&#8217;US&#8217;,<\/p>\n<p>u&#8217;cy&#8217;: u&#8217;Danvers&#8217;,<\/p>\n<p>u&#8217;g&#8217;: u&#8217;A6qOVH&#8217;,<\/p>\n<p>u&#8217;gr&#8217;: u&#8217;MA&#8217;,<\/p>\n<p>u&#8217;h&#8217;: u&#8217;wfLQtf&#8217;,<\/p>\n<p>u&#8217;hc&#8217;: 1331822918,<\/p>\n<p>u&#8217;hh&#8217;: u&#8217;1.usa.gov&#8217;,<\/p>\n<p>u&#8217;l&#8217;: u&#8217;orofrog&#8217;,<\/p>\n<p>u&#8217;ll&#8217;: [42.576698, -70.954903],<\/p>\n<p>u&#8217;nk&#8217;: 1,<\/p>\n<p>u&#8217;r&#8217;: u&#8217;http:\/\/www.facebook.com\/l\/7AQEFzjSi\/1.usa.gov\/wfLQtf&#8217;,<\/p>\n<p>u&#8217;t&#8217;: 1331923247,<\/p>\n<p>u&#8217;tz&#8217;: u&#8217;America\/New_York&#8217;,<\/p>\n<p>u&#8217;u&#8217;: u&#8217;http:\/\/www.ncbi.nlm.nih.gov\/pubmed\/22415991&#8242;}<\/p>\n<p>The letter immediately following the u is the key. So this line, u&#8217;tz&#8217;: u&#8217;America\/New_York&#8217;, specifies that the time zone for this particular record is \u2018America\/New_York\u2019. Most of the records contained the \u2018tz\u2019 field and a corresponding time zone value. I was attempting to list the top 10 time zones that appeared in the data. The approach you learned in school would involve looping through the data and collecting a count of each. Let\u2019s say we had extracted a list of our time zones that looked like the following:<\/p>\n<p>In [42]: time_zones[:10]<\/p>\n<p>Out[42]:<\/p>\n<p>[u&#8217;America\/New_York&#8217;,<\/p>\n<p>u&#8217;America\/Denver&#8217;,<\/p>\n<p>u&#8217;America\/New_York&#8217;,<\/p>\n<p>u&#8217;America\/Sao_Paulo&#8217;,<\/p>\n<p>u&#8217;America\/New_York&#8217;,<\/p>\n<p>u&#8217;America\/New_York&#8217;,<\/p>\n<p>u&#8217;Europe\/Warsaw&#8217;,<\/p>\n<p>u&#8221;,<\/p>\n<p>u&#8221;,<\/p>\n<p>u&#8221;]<\/p>\n<p>Then we could do something like this:<\/p>\n<p>def get_counts(items):<\/p>\n<p>counts = defaultdict(int)<\/p>\n<p>for x in sequence:<\/p>\n<p>counts[x] += 1<\/p>\n<p>return counts<\/p>\n<p>Here, I\u2019m creating a dictionary of int values. A dictionary is basically an object that stores values by key. Next, I\u2019m iterating through the sequence and every time I encounter a new value I increment the value corresponding to that particular x by 1. So let\u2019s say I had the following list.<\/p>\n<p>student_grades = [\u2018A\u2019, \u2018A\u2019, \u2018B\u2019, \u2018B\u2019, \u2018B\u2019, \u2018B\u2019, \u2018C\u2019, \u2018F\u2019]<\/p>\n<p>The code would begin by reiterating over the list and x would become \u2018B\u2019. The counts[x] += 1 line of code would look in the dictionary for a key called x and then increment it\u2019s value by 1. Since this is the first time we have seen an \u2018A\u2019, the value for \u2018A\u2019 in the dictionary would be 1. Now the loop continues and x becomes the second \u2018A\u2019. When the counts[x] += 1 line is encountered, the value for \u2018A\u2019 is already 1 so it will become 2. The x value would now become \u2018B\u2019, and since there is no \u2018B\u2019 in counts, it becomes 1, then 2, and so on.<\/p>\n<p>I would use the code like this:<\/p>\n<p>cnts = get_counts(student_grades)<\/p>\n<p>I can then get the count for a particular time zone like this:<\/p>\n<p>cnts[\u2018B\u2019]<\/p>\n<p>Which would return 4.<\/p>\n<p>If I want to get the top ten I could do this:<\/p>\n<p>def top_counts(count_dict, n=10):<\/p>\n<p>#n = 10 gives me a default value for 10 if the caller does not specify one.<\/p>\n<p>value_key_pairs = [(count, tx) for tx, count in count_dict.items()]<\/p>\n<p>value_key_pairs.sort()<\/p>\n<p>return value_key_pairs[-n]<\/p>\n<h3>Python Counter list comprehension<\/h3>\n<p>This code begins by using a feature of the python language called a list comprehension. I\u2019ll do a separate blog on those later. We\u2019re extracting the keys and values from the dictionary and them storing them in a list. Next we sort that list and then we use a slice to count backwards from the end of the list, returning n items from the end.<\/p>\n<p>That codes not so bad but we can save ourselves a few keystrokes. In the same library as defaultdict, collections, there exists a class called Counter.<\/p>\n<p>We could shorten all that code to this:<\/p>\n<p>from collections import Counter<\/p>\n<p>counts &#8211; Counter(time_zones)<\/p>\n<p>counts.most_common(10)<\/p>\n<p>Giving us:<\/p>\n<p>In [43]: counts.most_common(10)<\/p>\n<p>Out[43]:<\/p>\n<p>[(u&#8217;America\/New_York&#8217;, 1251),<\/p>\n<p>(u&#8221;, 521),<\/p>\n<p>(u&#8217;America\/Chicago&#8217;, 400),<\/p>\n<p>(u&#8217;America\/Los_Angeles&#8217;, 382),<\/p>\n<p>(u&#8217;America\/Denver&#8217;, 191),<\/p>\n<p>(u&#8217;Europe\/London&#8217;, 74),<\/p>\n<p>(u&#8217;Asia\/Tokyo&#8217;, 37),<\/p>\n<p>(u&#8217;Pacific\/Honolulu&#8217;, 36),<\/p>\n<p>(u&#8217;Europe\/Madrid&#8217;, 35),<\/p>\n<p>(u&#8217;America\/Sao_Paulo&#8217;, 33)]<\/p>\n<p>Whew, that was so much easier. Always explore your language to see if there is something that will solve your task easier. Don\u2019t reinvent the wheel! If you want to find out more about the collection library visit <a title=\"pyton counter collection library\" href=\"https:\/\/docs.python.org\/2\/library\/collections.html\" target=\"_blank\" rel=\"noopener\">https:\/\/docs.python.org\/2\/library\/collections.html<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What we learn in school is just a tiny fraction of what is available to us as programmers. My goal in this series of blog posts is to introduce you to some of the more advance topics of several different languages. In this post I will talk specifically about the python collections Counter class. The &hellip; <a href=\"https:\/\/blog.spcollege.edu\/technology-educational-information\/python-counter\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">An Inside look at Python Counter<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":100,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15,5],"tags":[],"coauthors":[],"class_list":["post-252","post","type-post","status-publish","format-standard","hentry","category-ccit","category-tech-programming"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\r\n<title>An Inside look at Python Counter - Technology<\/title>\r\n<meta name=\"description\" content=\"In this post I will talk specifically about the python collections Counter class.\" \/>\r\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/blog.spcollege.edu\/technology-educational-information\/python-counter\/\" \/>\r\n<meta property=\"og:locale\" content=\"en_US\" \/>\r\n<meta property=\"og:type\" content=\"article\" \/>\r\n<meta property=\"og:title\" content=\"An Inside look at Python Counter - Technology\" \/>\r\n<meta property=\"og:description\" content=\"In this post I will talk specifically about the python collections Counter class.\" \/>\r\n<meta property=\"og:url\" content=\"https:\/\/blog.spcollege.edu\/technology-educational-information\/python-counter\/\" \/>\r\n<meta property=\"og:site_name\" content=\"Technology\" \/>\r\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/stpetecollege\/\" \/>\r\n<meta property=\"article:published_time\" content=\"2015-04-27T12:53:22+00:00\" \/>\r\n<meta property=\"article:modified_time\" content=\"2024-01-30T21:04:18+00:00\" \/>\r\n<meta name=\"author\" content=\"Adrian Tillman\" \/>\r\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\r\n<meta name=\"twitter:creator\" content=\"@spcnews\" \/>\r\n<meta name=\"twitter:site\" content=\"@spcnews\" \/>\r\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Adrian Tillman\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\r\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blog.spcollege.edu\\\/technology-educational-information\\\/python-counter\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.spcollege.edu\\\/technology-educational-information\\\/python-counter\\\/\"},\"author\":{\"name\":\"Adrian Tillman\",\"@id\":\"https:\\\/\\\/blog.spcollege.edu\\\/technology-educational-information\\\/#\\\/schema\\\/person\\\/44a3533aff69580e029541a39054bc1d\"},\"headline\":\"An Inside look at Python Counter\",\"datePublished\":\"2015-04-27T12:53:22+00:00\",\"dateModified\":\"2024-01-30T21:04:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blog.spcollege.edu\\\/technology-educational-information\\\/python-counter\\\/\"},\"wordCount\":736,\"articleSection\":[\"College of Computer and Information Technology\",\"Programming\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blog.spcollege.edu\\\/technology-educational-information\\\/python-counter\\\/\",\"url\":\"https:\\\/\\\/blog.spcollege.edu\\\/technology-educational-information\\\/python-counter\\\/\",\"name\":\"An Inside look at Python Counter - Technology\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.spcollege.edu\\\/technology-educational-information\\\/#website\"},\"datePublished\":\"2015-04-27T12:53:22+00:00\",\"dateModified\":\"2024-01-30T21:04:18+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blog.spcollege.edu\\\/technology-educational-information\\\/#\\\/schema\\\/person\\\/44a3533aff69580e029541a39054bc1d\"},\"description\":\"In this post I will talk specifically about the python collections Counter class.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blog.spcollege.edu\\\/technology-educational-information\\\/python-counter\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blog.spcollege.edu\\\/technology-educational-information\\\/python-counter\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blog.spcollege.edu\\\/technology-educational-information\\\/python-counter\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blog.spcollege.edu\\\/technology-educational-information\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"An Inside look at Python Counter\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blog.spcollege.edu\\\/technology-educational-information\\\/#website\",\"url\":\"https:\\\/\\\/blog.spcollege.edu\\\/technology-educational-information\\\/\",\"name\":\"Technology\",\"description\":\"Read about the state-of-the-art facilities and classes that prepare students for a technology career.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blog.spcollege.edu\\\/technology-educational-information\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blog.spcollege.edu\\\/technology-educational-information\\\/#\\\/schema\\\/person\\\/44a3533aff69580e029541a39054bc1d\",\"name\":\"Adrian Tillman\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/313c7ccc0545621988bda369d5f3c57e248f89b4ca3eba37f505359badbaadc8?s=96&d=mm&r=gefb0947cd071c23d8cce5a6e43659772\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/313c7ccc0545621988bda369d5f3c57e248f89b4ca3eba37f505359badbaadc8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/313c7ccc0545621988bda369d5f3c57e248f89b4ca3eba37f505359badbaadc8?s=96&d=mm&r=g\",\"caption\":\"Adrian Tillman\"},\"url\":\"https:\\\/\\\/blog.spcollege.edu\\\/technology-educational-information\\\/author\\\/adrian-tillman\\\/\"}]}<\/script>\r\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"An Inside look at Python Counter - Technology","description":"In this post I will talk specifically about the python collections Counter class.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.spcollege.edu\/technology-educational-information\/python-counter\/","og_locale":"en_US","og_type":"article","og_title":"An Inside look at Python Counter - Technology","og_description":"In this post I will talk specifically about the python collections Counter class.","og_url":"https:\/\/blog.spcollege.edu\/technology-educational-information\/python-counter\/","og_site_name":"Technology","article_publisher":"https:\/\/www.facebook.com\/stpetecollege\/","article_published_time":"2015-04-27T12:53:22+00:00","article_modified_time":"2024-01-30T21:04:18+00:00","author":"Adrian Tillman","twitter_card":"summary_large_image","twitter_creator":"@spcnews","twitter_site":"@spcnews","twitter_misc":{"Written by":"Adrian Tillman","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.spcollege.edu\/technology-educational-information\/python-counter\/#article","isPartOf":{"@id":"https:\/\/blog.spcollege.edu\/technology-educational-information\/python-counter\/"},"author":{"name":"Adrian Tillman","@id":"https:\/\/blog.spcollege.edu\/technology-educational-information\/#\/schema\/person\/44a3533aff69580e029541a39054bc1d"},"headline":"An Inside look at Python Counter","datePublished":"2015-04-27T12:53:22+00:00","dateModified":"2024-01-30T21:04:18+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.spcollege.edu\/technology-educational-information\/python-counter\/"},"wordCount":736,"articleSection":["College of Computer and Information Technology","Programming"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/blog.spcollege.edu\/technology-educational-information\/python-counter\/","url":"https:\/\/blog.spcollege.edu\/technology-educational-information\/python-counter\/","name":"An Inside look at Python Counter - Technology","isPartOf":{"@id":"https:\/\/blog.spcollege.edu\/technology-educational-information\/#website"},"datePublished":"2015-04-27T12:53:22+00:00","dateModified":"2024-01-30T21:04:18+00:00","author":{"@id":"https:\/\/blog.spcollege.edu\/technology-educational-information\/#\/schema\/person\/44a3533aff69580e029541a39054bc1d"},"description":"In this post I will talk specifically about the python collections Counter class.","breadcrumb":{"@id":"https:\/\/blog.spcollege.edu\/technology-educational-information\/python-counter\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.spcollege.edu\/technology-educational-information\/python-counter\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.spcollege.edu\/technology-educational-information\/python-counter\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.spcollege.edu\/technology-educational-information\/"},{"@type":"ListItem","position":2,"name":"An Inside look at Python Counter"}]},{"@type":"WebSite","@id":"https:\/\/blog.spcollege.edu\/technology-educational-information\/#website","url":"https:\/\/blog.spcollege.edu\/technology-educational-information\/","name":"Technology","description":"Read about the state-of-the-art facilities and classes that prepare students for a technology career.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.spcollege.edu\/technology-educational-information\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.spcollege.edu\/technology-educational-information\/#\/schema\/person\/44a3533aff69580e029541a39054bc1d","name":"Adrian Tillman","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/313c7ccc0545621988bda369d5f3c57e248f89b4ca3eba37f505359badbaadc8?s=96&d=mm&r=gefb0947cd071c23d8cce5a6e43659772","url":"https:\/\/secure.gravatar.com\/avatar\/313c7ccc0545621988bda369d5f3c57e248f89b4ca3eba37f505359badbaadc8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/313c7ccc0545621988bda369d5f3c57e248f89b4ca3eba37f505359badbaadc8?s=96&d=mm&r=g","caption":"Adrian Tillman"},"url":"https:\/\/blog.spcollege.edu\/technology-educational-information\/author\/adrian-tillman\/"}]}},"_links":{"self":[{"href":"https:\/\/blog.spcollege.edu\/technology-educational-information\/wp-json\/wp\/v2\/posts\/252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.spcollege.edu\/technology-educational-information\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.spcollege.edu\/technology-educational-information\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.spcollege.edu\/technology-educational-information\/wp-json\/wp\/v2\/users\/100"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.spcollege.edu\/technology-educational-information\/wp-json\/wp\/v2\/comments?post=252"}],"version-history":[{"count":4,"href":"https:\/\/blog.spcollege.edu\/technology-educational-information\/wp-json\/wp\/v2\/posts\/252\/revisions"}],"predecessor-version":[{"id":4757,"href":"https:\/\/blog.spcollege.edu\/technology-educational-information\/wp-json\/wp\/v2\/posts\/252\/revisions\/4757"}],"wp:attachment":[{"href":"https:\/\/blog.spcollege.edu\/technology-educational-information\/wp-json\/wp\/v2\/media?parent=252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.spcollege.edu\/technology-educational-information\/wp-json\/wp\/v2\/categories?post=252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.spcollege.edu\/technology-educational-information\/wp-json\/wp\/v2\/tags?post=252"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blog.spcollege.edu\/technology-educational-information\/wp-json\/wp\/v2\/coauthors?post=252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}