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 ‘clock’ and ‘klocc’ and we must group them together in a list.
Given the list of words, the task is to group words with the Same set of Characters.
Examples:
Example1:
Input:
Given list of strings = ['ehlo', 'this', 'is', 'olhe', 'helo', 'si', 'btechgeeks']
Output:
The group of words which are similar in given list of strings ['ehlo', 'this', 'is', 'olhe', 'helo', 'si', 'btechgeeks'] is : [['ehlo', 'olhe', 'helo'], ['this'], ['is', 'si'], ['btechgeeks']]
- Python Program to find N Character Word in the Given Text File
- 5 Simple Ways to Add a Newline Character in Python
- Python Program to Split a String at First Instance of a Character
Example2:
Input:
Given list of strings = ['here', 'we', 'are', 'ew', 'in', 'galazy', 'ereh', 'aer']
Output:
The group of words which are similar in given list of strings ['here', 'we', 'are', 'ew', 'in', 'galazy', 'ereh', 'aer'] is : [['here', 'ereh'], ['we', 'ew'], ['are', 'aer'], ['in'], ['galazy']]
Program to Group Words with the Same Set of Characters in Python
Below are the ways to group words with the same set of characters in Python.
Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.
Method #1: Using For Loop (Static Input)
Approach:
- Import the defaultdict from collections using the import keyword.
- Give the list of strings as static input and store it in a variable.
- The defaultdict method is used to construct a dictionary corresponding to a key that contains word characters.
- The list argument is used to generate key-value list pairs.
- Loop over the list of strings using For loop.
- The str method on sorted(word) returns a list of keys that include the alphabets of words.
- Append the word using append(word) joins together words that are related.
- Get all the values of the defaultdict using the values() function.
- Print the defaultdict.
- The Exit of the Program.
Below is the implementation:
# Import the defaultdict from collections using the import keyword. from collections import defaultdict # Give the list of strings as static input and store it in a variable. gvnstrnglist = ['ehlo', 'this', 'is', 'olhe', 'helo', 'si', 'btechgeeks'] # The defaultdict method is used to construct a dictionary # corresponding to a key that contains word characters. # The list argument is used to generate key-value list pairs. grpwords = defaultdict(list) # Loop over the list of strings using For loop. for strngword in gvnstrnglist: # The str method on sorted(word) returns a list of keys # that include the alphabets of words. # Append the word using append(word) joins together words that are related. grpwords[str(sorted(strngword))].append(strngword) # Get all the values of the defaultdict using the values() function. simipairs = list(grpwords.values()) # Print the defaultdict. print('The group of words which are similar in given list of strings ', gvnstrnglist, 'is :') print(simipairs)
Output:
The group of words which are similar in given list of strings ['ehlo', 'this', 'is', 'olhe', 'helo', 'si', 'btechgeeks'] is : [['ehlo', 'olhe', 'helo'], ['this'], ['is', 'si'], ['btechgeeks']]
Method #2: Using For Loop (User Input)
Approach:
- Import the defaultdict from collections using the import keyword.
- Give the list of strings as user input using list(), split(), and input() functions and store it in a variable.
- The defaultdict method is used to construct a dictionary corresponding to a key that contains word characters.
- The list argument is used to generate key-value list pairs.
- Loop over the list of strings using For loop.
- The str method on sorted(word) returns a list of keys that include the alphabets of words.
- Append the word using append(word) joins together words that are related.
- Get all the values of the defaultdict using the values() function.
- Print the defaultdict.
- The Exit of the Program.
Below is the implementation:
# Import the defaultdict from collections using the import keyword. from collections import defaultdict # Give the list of strings as user input using list(), split(), and input() functions # and store it in a variable. gvnstrnglist = list(input('Enter some random list of strings = ').split()) # The defaultdict method is used to construct a dictionary # corresponding to a key that contains word characters. # The list argument is used to generate key-value list pairs. grpwords = defaultdict(list) # Loop over the list of strings using For loop. for strngword in gvnstrnglist: # The str method on sorted(word) returns a list of keys # that include the alphabets of words. # Append the word using append(word) joins together words that are related. grpwords[str(sorted(strngword))].append(strngword) # Get all the values of the defaultdict using the values() function. simipairs = list(grpwords.values()) # Print the defaultdict. print('The group of words which are similar in given list of strings ', gvnstrnglist, 'is :') print(simipairs)
Output:
Enter some random list of strings = here we are ew in galazy ereh aer The group of words which are similar in given list of strings ['here', 'we', 'are', 'ew', 'in', 'galazy', 'ereh', 'aer'] is : [['here', 'ereh'], ['we', 'ew'], ['are', 'aer'], ['in'], ['galazy']]
Related Programs:
- python program to calculate the number of words and the number of characters present in a string
- python program to read a list of words and return the length of the longest word
- program to clear the rightmost set bit of a number in cpp and python
- python program to remove the ith occurrence of the given word in a list where words can repeat
- python program to count the frequency of words appearing in a string using a dictionary
- python program to create a list of tuples with the first element as the number and second element as the square of the number
- python program to count the number of words in a text file