{"id":8830,"date":"2023-11-04T11:11:45","date_gmt":"2023-11-04T05:41:45","guid":{"rendered":"https:\/\/python-programs.com\/?p=8830"},"modified":"2023-11-10T12:15:59","modified_gmt":"2023-11-10T06:45:59","slug":"python-program-to-count-the-frequency-of-words-appearing-in-a-string-using-a-dictionary","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-count-the-frequency-of-words-appearing-in-a-string-using-a-dictionary\/","title":{"rendered":"Python Program to Count the Frequency of Words Appearing in a String Using a Dictionary"},"content":{"rendered":"
Dictionaries in Python:<\/strong><\/p>\n 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.<\/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 There are several ways to count the frequency of all words in the given string using dictionary some of them are:<\/p>\n Drive into Python Programming Examples<\/a> and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation: Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Dictionaries in Python: 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 …<\/p>\ngiven string =\"hello this is hello BTechGeeks BTechGeeks BTechGeeks this python programming python language\"<\/pre>\n
{'hello': 2, 'this': 2, 'is': 1, 'BTechGeeks': 3, 'python': 2, 'programming': 1, 'language': 1}<\/pre>\n
given string =\"good morning this is good this python python BTechGeeks good good python online coding platform\"<\/pre>\n
{'good': 4, 'morning': 1, 'this': 2, 'is': 1, 'python': 3, 'BTechGeeks': 1, 'online': 1, 'coding': 1, 'platform': 1}<\/pre>\n
Program to Count the Frequency of Words Appearing in a String Using a Dictionary<\/h2>\n
\n
Method #1:Using count and zip function (Static Input)<\/h3>\n
\n
# given string\r\ngiven_string = \"hello this is hello BTechGeeks BTechGeeks BTechGeeks this python programming python language\"\r\n# Split the given string into words using split() function\r\n# Convert this into list using list() function.\r\nlistString = given_string.split()\r\n# Count the frequency of each term using count() function and\r\n# save the results in a separate list using list Comprehension.\r\nfreqWords = [listString.count(k) for k in listString]\r\n# Merge the lists containing the terms and the word counts\r\n# into a dictionary using the zip() function.\r\nresultdict = dict(zip(listString, freqWords))\r\n# Print the resultant dictionary\r\nprint(resultdict)\r\n<\/pre>\n
{'hello': 2, 'this': 2, 'is': 1, 'BTechGeeks': 3, 'python': 2, 'programming': 1, 'language': 1}<\/pre>\n
\n<\/strong><\/p>\n\n
Method #2:Using count and zip function (User Input)<\/h3>\n
\n
# Scan the given string using input() function\r\ngiven_string = input(\"Enter some random string separated by spaces = \")\r\n# Split the given string into words using split() function\r\n# Convert this into list using list() function.\r\nlistString = given_string.split()\r\n# Count the frequency of each term using count() function and\r\n# save the results in a separate list using list Comprehension.\r\nfreqWords = [listString.count(k) for k in listString]\r\n# Merge the lists containing the terms and the word counts\r\n# into a dictionary using the zip() function.\r\nresultdict = dict(zip(listString, freqWords))\r\n# Print the resultant dictionary\r\nprint(resultdict)\r\n<\/pre>\n
Enter some random string separated by spaces = good morning this is good this python python BTechGeeks good good python online coding platform\r\n{'good': 4, 'morning': 1, 'this': 2, 'is': 1, 'python': 3, 'BTechGeeks': 1, 'online': 1, 'coding': 1, 'platform': 1}<\/pre>\n
\n