Python Counter Exercises & Solutions
Python counter Data Type [ 10 exercises with solution ]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
A Counter is a dict subclass for counting hashable objects. It is a collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts. The Counter class is similar to bags or multisets in other languages.
1. Write a Python program to create a 'Counter' of the letters in the string "Python Exercise!".
Click me to see the sample solution
2. Write a Python program that creates a 'Counter' from a list of elements and print the most common elements along with their counts.
Click me to see the sample solution
3. Write a Python program that creates a counter of the vowels in the word "Python Exercises".
Click me to see the sample solution
4. Write a Python program that creates two 'Counter' objects and finds their union, intersection, and difference.
Click me to see the sample solution
5. Write a Python program that creates a counter of the words in a sentence and prints the words in ascending and descending order of their frequency.
Click me to see the sample solution
6. Write a function that takes two 'Counter' objects and returns their sum.
Click me to see the sample solution
7. Write a Python program that creates a 'Counter' for a list of items and converts it to a list of unique items with their counts.
Click me to see the sample solution
8. Write a Python program that creates a 'Counter' for a list of words and removes all items with a count less than a certain value..
Click me to see the sample solution
9. Write a python program to find the Jaccard similarity coefficient between two lists using 'Counter' objects.
Click me to see the sample solution
10. Write a Python program that creates a 'Counter' for a list of items and uses dictionary-style access to update the count of specific items.
Click me to see the sample solution
Python Code Editor:
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
Test your Python skills with w3resource's quiz
Python: Tips of the Day
Memory Footprint Of An Object:
import sys x = 'farhadmalik' print(sys.getsizeof(x))
60
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook