{"id":25900,"date":"2021-12-16T09:20:42","date_gmt":"2021-12-16T03:50:42","guid":{"rendered":"https:\/\/python-programs.com\/?p=25900"},"modified":"2021-12-16T09:20:42","modified_gmt":"2021-12-16T03:50:42","slug":"python-collections-counter-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-collections-counter-method-with-examples\/","title":{"rendered":"Python collections Counter() Method with Examples"},"content":{"rendered":"
Counter():<\/strong><\/p>\n The Counter class is a subset of the object data-set offered by Python3’s collections module. The Collections module exposes specialized container datatypes to the user, serving as an alternative to Python’s general-purpose built-ins such as dictionaries, lists, and tuples.<\/p>\n The counter is a subclass that counts hashable objects. When called, it constructs an iterable hash table implicitly.<\/p>\n elements() is a function of the Counter class that, when called on a Counter object, returns an itertool of all the known elements in the Counter object.<\/p>\n Parameters:<\/strong><\/p>\n Return type:<\/strong><\/p>\n Exceptions and Errors:<\/strong><\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n i)elements() for string<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n ii)Printing elements of Counter(String)<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n iii)Printing elements with corresponding Frequency using elements()(String)<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n iv)Printing elements with corresponding Frequency(List)<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n v)Printing elements with corresponding frequency(String)<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Counter(): The Counter class is a subset of the object data-set offered by Python3’s collections module. The Collections module exposes specialized container datatypes to the user, serving as an alternative to Python’s general-purpose built-ins such as dictionaries, lists, and tuples. The counter is a subclass that counts hashable objects. When called, it constructs an iterable …<\/p>\n It does not accept any parameters.\r\n\r\n<\/pre>\n
Returns an itertool for every element in the Counter object with a positive count.\r\n\r\n<\/pre>\n
Because it returns an itertool rather than a specific data-container, it will output a garbage value when directly printed.\r\n-> If an item's count has already been established in the Counter object, it will ignore items with zero and negative values.<\/pre>\n
Given String = \"python-programs\"<\/pre>\n
p = 2\r\ny = 1\r\nt = 1\r\nh = 1\r\no = 2\r\nn = 1\r\n- = 1\r\nr = 2\r\ng = 1\r\na = 1\r\nm = 1\r\ns = 1<\/pre>\n
Given List=\u00a0[3, 1, 3, 4, 5, 6, 2, 2, 1, 5, 6, 9]<\/pre>\n
3 = 2\r\n1 = 2\r\n4 = 1\r\n5 = 2\r\n6 = 2\r\n2 = 2\r\n9 = 1<\/pre>\n
Collections Counter() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import the Counter function from collections using the import keyword.\r\nfrom collections import Counter\r\n# Give the string as static input and store it in a variable.\r\ngvnstrng = \"python-programs\"\r\n# Apply the Counter() function for the string and store it in a\r\n# variable(It returns a dictionary).\r\nrslt = Counter(gvnstrng)\r\n# Apply elements() function for the Above result Counter Dictionary elements and print it.\r\nprint(rslt.elements())\r\n<\/pre>\n
<itertools.chain object at 0x7f8599afb780><\/pre>\n
\n
# Import the Counter function from collections using the import keyword.\r\nfrom collections import Counter\r\n# Give the string as static input and store it in a variable.\r\ngvnstrng = \"python-programs\"\r\n# Apply the Counter() function for the string and store it in a\r\n# variable(It returns a dictionary).\r\nrslt = Counter(gvnstrng)\r\n# Loop in the elements of the Above result Counter Dictionary elements using for loop and elements() function.\r\nfor element in rslt.elements():\r\n # Print the elements.\r\n print(element)\r\n<\/pre>\n
p\r\np\r\ny\r\nt\r\nh\r\no\r\no\r\nn\r\n-\r\nr\r\nr\r\ng\r\na\r\nm\r\ns<\/pre>\n
\n
# Import the Counter function from collections using the import keyword.\r\nfrom collections import Counter\r\n# Give the string as static input and store it in a variable.\r\ngvnstrng = \"python-programs\"\r\n# Apply the Counter() function for the string and store it in a\r\n# variable(It returns a dictionary).\r\nrslt = Counter(gvnstrng)\r\n# Loop in the elements of the Above result Counter Dictionary elements using for loop and elements() function.\r\nfor element in rslt.elements():\r\n # Print the elements and their frequency using the [] operator.\r\n print(element, ' = ', rslt[element])\r\n<\/pre>\n
p = 2\r\np = 2\r\ny = 1\r\nt = 1\r\nh = 1\r\no = 2\r\no = 2\r\nn = 1\r\n- = 1\r\nr = 2\r\nr = 2\r\ng = 1\r\na = 1\r\nm = 1\r\ns = 1<\/pre>\n
\n
# Import the Counter function from collections using the import keyword.\r\nfrom collections import Counter\r\n# Give the list as static input and store it in a variable.\r\ngvllst = [3, 1, 3, 4, 5, 6, 2, 2, 1, 5, 6, 9]\r\n# Apply the Counter() function for the list and store it in a\r\n# variable(It returns a dictionary).\r\nrslt = Counter(gvllst)\r\n# Loop in the Above result Counter Dictionary elements using for loop.\r\nfor element in rslt:\r\n # Print the elements and their frequency using the [] operator.\r\n print(element, ' = ', rslt[element])\r\n<\/pre>\n
3 = 2\r\n1 = 2\r\n4 = 1\r\n5 = 2\r\n6 = 2\r\n2 = 2\r\n9 = 1<\/pre>\n
\n
# Import the Counter function from collections using the import keyword.\r\nfrom collections import Counter\r\n# Give the string as static input and store it in a variable.\r\ngvnstrng = \"python-programs\"\r\n# Apply the Counter() function for the string and store it in a\r\n# variable(It returns a dictionary).\r\nrslt = Counter(gvnstrng)\r\n# Loop in the elements of the Above result Counter Dictionary elements using for loop and elements() function.\r\nfor element in rslt:\r\n # Print the elements and their frequency using the [] operator.\r\n print(element, ' = ', rslt[element])<\/pre>\n
p = 2\r\ny = 1\r\nt = 1\r\nh = 1\r\no = 2\r\nn = 1\r\n- = 1\r\nr = 2\r\ng = 1\r\na = 1\r\nm = 1\r\ns = 1<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import the Counter function from collections using the import keyword.\r\nfrom collections import Counter\r\n# Give the list as user input using map(),list(),input(),int() functions and store it in a variable.\r\ngvllst = list(map(int, input('Enter some random elements = ').split()))\r\n# Apply the Counter() function for the list and store it in a\r\n# variable(It returns a dictionary).\r\nrslt=Counter(gvllst)\r\n# Loop in the Above result Counter Dictionary elements using for loop and elements() function.\r\nfor element in rslt:\r\n # Print the elements and their frequency using the [] operator.\r\n print(element, ' = ', rslt[element])\r\n<\/pre>\n
Enter some random elements = 7 1 2 1 8 9 2 4 6\r\n7 = 1\r\n1 = 2\r\n2 = 2\r\n8 = 1\r\n9 = 1\r\n4 = 1\r\n6 = 1<\/pre>\n","protected":false},"excerpt":{"rendered":"