{"id":18718,"date":"2021-08-26T10:40:21","date_gmt":"2021-08-26T05:10:21","guid":{"rendered":"https:\/\/python-programs.com\/?p=18718"},"modified":"2021-11-22T18:37:15","modified_gmt":"2021-11-22T13:07:15","slug":"python-program-to-count-words-that-appear-exactly-two-times-in-an-array-list-of-words","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-count-words-that-appear-exactly-two-times-in-an-array-list-of-words\/","title":{"rendered":"Python Program to Count Words that Appear Exactly Two Times in an Array\/List of Words"},"content":{"rendered":"
In the previous article, we have discussed Python Program for Snake Case of a Given Sentence<\/a> 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 Below are the ways to count the words that appear exactly twice in an array\/list of words in a given list.<\/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 Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program for Snake Case of a Given Sentence The task is counting the words that appear exactly twice in an array\/list of words in a given list in Python Examples: Example1: Input: Given List of words = [‘hi’, ‘all’, ‘this’, ‘is’, ‘btechgeeks’, ‘hi’, ‘this’, ‘btechgeeks’] Output: The …<\/p>\n
\nThe task is counting the words that appear exactly twice in an array\/list of words in a given list in Python<\/p>\nGiven List of words = ['hi', 'all', 'this', 'is', 'btechgeeks', 'hi', 'this', 'btechgeeks']<\/pre>\n
The Count of words in a given list ['hi', 'all', 'this', 'is', 'btechgeeks', 'hi', 'this', 'btechgeeks'] which are exactly repeated two times = 3<\/pre>\n
Given List of words = ['good', 'morning', 'this', 'is', 'btechgeeks', 'good', 'is']<\/pre>\n
The Count of words in a given list ['good', 'morning', 'this', 'is', 'btechgeeks', 'good', 'is'] which are exactly repeated two times = 2<\/pre>\n
Program to Count Words that Appear Exactly Two Times in an Array\/List of Words<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Import the Counter() function from collections using the import keyword.\r\nfrom collections import Counter\r\n# Give the list of words as static input and store it in a variable.\r\ngvn_lst = [\"hi\", \"all\", \"this\", \"is\", \"btechgeeks\", \"hi\", \"this\", \"btechgeeks\"]\r\n# Calculate the frequency of all the given list of words using the Counter() function\r\n# which returns the element and its frequency as key-value pair and store this\r\n# dictionary in a variable(say dictnry)\r\ndictnry = Counter(gvn_lst)\r\n# Take a variable say countt and initialize its value with 0.\r\ncountt = 0\r\n# Iterate in the above-obtained dictionary values using the for loop.\r\nfor itr in dictnry.values():\r\n # Check if the iterator value is equal to 2 or not using the if conditional statement.\r\n if itr == 2:\r\n # If the statement is true, increase the value of\u00a0\"countt\" by 1.\r\n countt += 1\r\n# Print the count of words that appear exactly twice in an array\/list of words in a\r\n# given list.\r\nprint(\"The Count of words in a given list\", gvn_lst,\r\n \" which are exactly repeated two times =\", countt)\r\n<\/pre>\n
The Count of words in a given list ['hi', 'all', 'this', 'is', 'btechgeeks', 'hi', 'this', 'btechgeeks'] which are exactly repeated two times = 3<\/pre>\n
Method #2: Using For loop (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 of words as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(input(\r\n 'Enter some random List Elements separated by spaces = ').split())\r\n# Calculate the frequency of all the given list of words using the Counter() function\r\n# which returns the element and its frequency as key-value pair and store this\r\n# dictionary in a variable(say dictnry)\r\ndictnry = Counter(gvn_lst)\r\n# Take a variable say countt and initialize its value with 0.\r\ncountt = 0\r\n# Iterate in the above-obtained dictionary values using the for loop.\r\nfor itr in dictnry.values():\r\n # Check if the iterator value is equal to 2 or not using the if conditional statement.\r\n if itr == 2:\r\n # If the statement is true, increase the value of\u00a0\"countt\" by 1.\r\n countt += 1\r\n# Print the count of words that appear exactly twice in an array\/list of words in a\r\n# given list.\r\nprint(\"The Count of words in a given list\", gvn_lst,\r\n \" which are exactly repeated two times =\", countt)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = good morning this is btechgeeks good is\r\nThe Count of words in a given list ['good', 'morning', 'this', 'is', 'btechgeeks', 'good', 'is'] which are exactly repeated two times = 2<\/pre>\n
\n