{"id":12877,"date":"2021-10-01T11:00:29","date_gmt":"2021-10-01T05:30:29","guid":{"rendered":"https:\/\/python-programs.com\/?p=12877"},"modified":"2021-11-22T18:33:27","modified_gmt":"2021-11-22T13:03:27","slug":"python-program-to-group-words-with-the-same-set-of-characters","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-group-words-with-the-same-set-of-characters\/","title":{"rendered":"Python Program to Group Words with the Same Set of Characters"},"content":{"rendered":"
Group Words:<\/strong><\/p>\n In Python, grouping words with the same set of characters is also known as Group Anagrams. We are given a list of words with the same set of characters but in various positions in the word, such as \u2018clock’ and \u2018klocc’ and we must group them together in a list.<\/p>\n Given the list of words, the task is to group words with the Same set of Characters.<\/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 Below are the ways to group words with the same set of characters in Python.<\/p>\n Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list<\/a> is mandatory.<\/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 Related Programs<\/strong>:<\/p>\n Group Words: In Python, grouping words with the same set of characters is also known as Group Anagrams. We are given a list of words with the same set of characters but in various positions in the word, such as \u2018clock’ and \u2018klocc’ and we must group them together in a list. Given the list …<\/p>\nGiven list of strings = ['ehlo', 'this', 'is', 'olhe', 'helo', 'si', 'btechgeeks']<\/pre>\n
The group of words which are similar in given list of strings ['ehlo', 'this', 'is', 'olhe', 'helo', 'si', 'btechgeeks'] is :\r\n[['ehlo', 'olhe', 'helo'], ['this'], ['is', 'si'], ['btechgeeks']]<\/pre>\n
Given list of strings = ['here', 'we', 'are', 'ew', 'in', 'galazy', 'ereh', 'aer']<\/pre>\n
The group of words which are similar in given list of strings ['here', 'we', 'are', 'ew', 'in', 'galazy', 'ereh', 'aer'] is :\r\n[['here', 'ereh'], ['we', 'ew'], ['are', 'aer'], ['in'], ['galazy']]<\/pre>\n
Program to Group Words with the Same Set of Characters in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Import the defaultdict from collections using the import keyword.\r\n\r\nfrom collections import defaultdict\r\n# Give the list of strings as static input and store it in a variable.\r\ngvnstrnglist = ['ehlo', 'this', 'is', 'olhe', 'helo', 'si', 'btechgeeks']\r\n# The defaultdict method is used to construct a dictionary\r\n# corresponding to a key that contains word characters.\r\n# The list argument is used to generate key-value list pairs.\r\ngrpwords = defaultdict(list)\r\n\r\n# Loop over the list of strings using For loop.\r\nfor strngword in gvnstrnglist:\r\n # The str method on sorted(word) returns a list of keys\r\n # that include the alphabets of words.\r\n # Append the word using append(word) joins together words that are related.\r\n grpwords[str(sorted(strngword))].append(strngword)\r\n\r\n# Get all the values of the defaultdict using the values() function.\r\nsimipairs = list(grpwords.values())\r\n# Print the defaultdict.\r\nprint('The group of words which are similar in given list of strings ',\r\n gvnstrnglist, 'is :')\r\nprint(simipairs)\r\n<\/pre>\n
The group of words which are similar in given list of strings ['ehlo', 'this', 'is', 'olhe', 'helo', 'si', 'btechgeeks'] is :\r\n[['ehlo', 'olhe', 'helo'], ['this'], ['is', 'si'], ['btechgeeks']]<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n
# Import the defaultdict from collections using the import keyword.\r\n\r\nfrom collections import defaultdict\r\n# Give the list of strings as user input using list(), split(), and input() functions\r\n# and store it in a variable.\r\ngvnstrnglist = list(input('Enter some random list of strings = ').split())\r\n# The defaultdict method is used to construct a dictionary\r\n# corresponding to a key that contains word characters.\r\n# The list argument is used to generate key-value list pairs.\r\ngrpwords = defaultdict(list)\r\n\r\n# Loop over the list of strings using For loop.\r\nfor strngword in gvnstrnglist:\r\n # The str method on sorted(word) returns a list of keys\r\n # that include the alphabets of words.\r\n # Append the word using append(word) joins together words that are related.\r\n grpwords[str(sorted(strngword))].append(strngword)\r\n\r\n# Get all the values of the defaultdict using the values() function.\r\nsimipairs = list(grpwords.values())\r\n# Print the defaultdict.\r\nprint('The group of words which are similar in given list of strings ',\r\n gvnstrnglist, 'is :')\r\nprint(simipairs)\r\n<\/pre>\n
Enter some random list of strings = here we are ew in galazy ereh aer\r\nThe group of words which are similar in given list of strings ['here', 'we', 'are', 'ew', 'in', 'galazy', 'ereh', 'aer'] is :\r\n[['here', 'ereh'], ['we', 'ew'], ['are', 'aer'], ['in'], ['galazy']]<\/pre>\n
\n