Dictionaries in Python:
Python count word frequency dictionary: Dictionary is a mutable built-in Python Data Structure. It is conceptually related to List, Set, and Tuples. It is, however, indexed by keys rather than a sequence of numbers and can be thought of as associative arrays. On a high level, it consists of a key and its associated value. The Dictionary class in Python represents a hash-table implementation.
Examples:
Example1:
Input:
given string ="hello this is hello BTechGeeks BTechGeeks BTechGeeks this python programming python language"
Output:
{'hello': 2, 'this': 2, 'is': 1, 'BTechGeeks': 3, 'python': 2, 'programming': 1, 'language': 1}
Example2:
Input:
given string ="good morning this is good this python python BTechGeeks good good python online coding platform"
Output:
{'good': 4, 'morning': 1, 'this': 2, 'is': 1, 'python': 3, 'BTechGeeks': 1, 'online': 1, 'coding': 1, 'platform': 1}
- Python Program to Accept a Hyphen Separated Sequence of Words as Input and Print the Words in a Hyphen-Separated Sequence after Sorting them Alphabetically
- Python Program to Print Even Length Words in a String
- Python Program to Map Two Lists into a Dictionary using Zip(), Append() Functions | How to Combine Two Lists into a Dictionary?
Program to Count the Frequency of Words Appearing in a String Using a Dictionary
There are several ways to count the frequency of all words in the given string using dictionary some of them are:
Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.
Method #1:Using count and zip function (Static Input)
Approach:
- Give the string input as static and store it in a variable
- Split the given string into words using split() function
- Convert this into list using list() function.
- Count the frequency of each term using count() function and save the results in a separate list using list Comprehension.
- Merge the lists containing the terms and the word counts into a dictionary using the zip() function.
- The resultant dictionary is printed
- End of Program
Below is the implementation:
# given string given_string = "hello this is hello BTechGeeks BTechGeeks BTechGeeks this python programming python language" # Split the given string into words using split() function # Convert this into list using list() function. listString = given_string.split() # Count the frequency of each term using count() function and # save the results in a separate list using list Comprehension. freqWords = [listString.count(k) for k in listString] # Merge the lists containing the terms and the word counts # into a dictionary using the zip() function. resultdict = dict(zip(listString, freqWords)) # Print the resultant dictionary print(resultdict)
Output:
{'hello': 2, 'this': 2, 'is': 1, 'BTechGeeks': 3, 'python': 2, 'programming': 1, 'language': 1}
Explanation:
- A string must be entered by the user and saved in a variable.
- The string is divided into words and saved in the list using a space as the reference.
- Using list comprehension and the count() function, the frequency of each word in the list is counted.
- The complete dictionary is printed after being created with the zip() method.
Method #2:Using count and zip function (User Input)
Approach:
- Scan the string using input() function.
- Split the given string into words using split() function
- Convert this into list using list() function.
- Count the frequency of each term using count() function and save the results in a separate list using list Comprehension.
- Merge the lists containing the terms and the word counts into a dictionary using the zip() function.
- The resultant dictionary is printed
- End of Program
Below is the implementation:
# Scan the given string using input() function given_string = input("Enter some random string separated by spaces = ") # Split the given string into words using split() function # Convert this into list using list() function. listString = given_string.split() # Count the frequency of each term using count() function and # save the results in a separate list using list Comprehension. freqWords = [listString.count(k) for k in listString] # Merge the lists containing the terms and the word counts # into a dictionary using the zip() function. resultdict = dict(zip(listString, freqWords)) # Print the resultant dictionary print(resultdict)
Output:
Enter some random string separated by spaces = good morning this is good this python python BTechGeeks good good python online coding platform {'good': 4, 'morning': 1, 'this': 2, 'is': 1, 'python': 3, 'BTechGeeks': 1, 'online': 1, 'coding': 1, 'platform': 1}
Related Programs:
- Python Program to Read a File and Capitalize the First Letter of Every Word in the File
- Python Program to Map Two Lists into a Dictionary
- Python Program to Generate a Dictionary that Contains Numbers (between 1 and n) in the Form (x, x*x).
- Python Program to Add a Key, Value Pair to the Dictionary
- Python Program to Sum All the Items in a Dictionary