Python: The resulting array of type string does not match
Question:
I want to make a program to group words consisting of the same characters in an array, but the results don’t match when using the Python programming language
Example problem: Suppose I have 3 arrays of type string as follows:
oe1 = ["abc", "def"];
oe2 = ["asd", "cab", "fed", "eqw"];
oe3 = ["qwe", "efd", "bca"];
Note: The order of the array or the elements doesn’t matter, the important thing is that they are grouped.
Output example:
[abc, cab, bca]
[asd]
[def, fed, efd]
[eqw, qwe]
But I try to use coding like this the results are not appropriate:
oe1 = ["abc", "def"];
oe2 = ["asd", "cab", "fed", "eqw"];
oe3 = ["qwe", "efd", "bca"];
anagram_list = []
for word_1 in oe1:
for word_2 in oe2:
for word_3 in oe3:
if word_1 != word_2 != word_3 and (sorted(word_1)==sorted(word_2)==sorted(word_3)):
anagram_list.append(word_1 + word_2 + word_3)
print(anagram_list)
my output is like this:
['abccabbca', 'deffedefd']
How do I make it match the example output above?
Answers:
Here is one way to perhaps solve your problem:
- Create a function that takes three lists of words as input and returns a list of lists of anagrams, grouped by input list.
def group_anagrams(oe1, oe2, oe3):
# Create a dictionary where the keys are the sorted version of each word
# and the values are lists of the words that have the same characters.
anagrams = {}
# Loop over each word in each input list.
for words in [oe1, oe2, oe3]:
for word in words:
# Sort the word to create a unique key for each set of anagrams.
key = ''.join(sorted(word))
# Add the word to the list of anagrams for this key.
if key in anagrams:
anagrams[key].append(word)
else:
anagrams[key] = [word]
# Return the values of the dictionary (i.e. the lists of anagrams) as a list of lists.
return anagrams.values()
- Use the function to group the anagrams in your example.
oe1 = ["abc", "def"]
oe2 = ["asd", "cab", "fed", "eqw"]
oe3 = ["qwe", "efd", "bca"]
anagram_lists = group_anagrams(oe1, oe2, oe3)
# Print the result.
for anagrams in anagram_lists:
print(anagrams)
This should output the following:
[abc, cab, bca]
[asd]
[def, fed, efd]
[eqw, qwe]
Hope this helps!
To be easier, let merge them all together. Also, use condition with set()
to check whether 2 text are using the same set of characters.
However, my solution doesn’t require any sort()
. But I added it because I want the output to be the same as your desired output.
oe = oe1 + oe2 + oe3
oe.sort() # optional, no need for sorting
oe_group = []
for i in range(len(oe)):
if i == 0:
oe_group.append([oe[i]])
else:
for j in range(len(oe_group)):
if set(oe[i]) == set(oe_group[j][0]):
oe_group[j].append(oe[i])
break
if j == len(oe_group) - 1:
oe_group.append([oe[i]])
output
[['abc', 'bca', 'cab'], ['asd'], ['def', 'efd', 'fed'], ['eqw', 'qwe']]
First off, let’s combine those lists and sort them using a lambda that converts each string to a list of characters, then sorts that.
>>> sorted(oe1 + oe2 + oe3, key=lambda s: sorted(list(s)))
['abc', 'cab', 'bca', 'asd', 'def', 'fed', 'efd', 'eqw', 'qwe']
Then using itertools.groupby
to group them based on the same lambda.
>>> k = lambda s: sorted(list(s))
>>> [list(v) for _, v in groupby(sorted(oe1 + oe2 + oe3, key=k), key=k)]
[['abc', 'cab', 'bca'], ['asd'], ['def', 'fed', 'efd'], ['eqw', 'qwe']]
This can be simplified a bit further by not first converting to a list and just sorting each string.
>>> sorted(oe1 + oe2 + oe3, key=sorted)
['abc', 'cab', 'bca', 'asd', 'def', 'fed', 'efd', 'eqw', 'qwe']
>>> [list(v) for _, v in groupby(sorted(oe1 + oe2 + oe3, key=sorted), key=sorted)]
[['abc', 'cab', 'bca'], ['asd'], ['def', 'fed', 'efd'], ['eqw', 'qwe']]
I want to make a program to group words consisting of the same characters in an array, but the results don’t match when using the Python programming language
Example problem: Suppose I have 3 arrays of type string as follows:
oe1 = ["abc", "def"];
oe2 = ["asd", "cab", "fed", "eqw"];
oe3 = ["qwe", "efd", "bca"];
Note: The order of the array or the elements doesn’t matter, the important thing is that they are grouped.
Output example:
[abc, cab, bca]
[asd]
[def, fed, efd]
[eqw, qwe]
But I try to use coding like this the results are not appropriate:
oe1 = ["abc", "def"];
oe2 = ["asd", "cab", "fed", "eqw"];
oe3 = ["qwe", "efd", "bca"];
anagram_list = []
for word_1 in oe1:
for word_2 in oe2:
for word_3 in oe3:
if word_1 != word_2 != word_3 and (sorted(word_1)==sorted(word_2)==sorted(word_3)):
anagram_list.append(word_1 + word_2 + word_3)
print(anagram_list)
my output is like this:
['abccabbca', 'deffedefd']
How do I make it match the example output above?
Here is one way to perhaps solve your problem:
- Create a function that takes three lists of words as input and returns a list of lists of anagrams, grouped by input list.
def group_anagrams(oe1, oe2, oe3):
# Create a dictionary where the keys are the sorted version of each word
# and the values are lists of the words that have the same characters.
anagrams = {}
# Loop over each word in each input list.
for words in [oe1, oe2, oe3]:
for word in words:
# Sort the word to create a unique key for each set of anagrams.
key = ''.join(sorted(word))
# Add the word to the list of anagrams for this key.
if key in anagrams:
anagrams[key].append(word)
else:
anagrams[key] = [word]
# Return the values of the dictionary (i.e. the lists of anagrams) as a list of lists.
return anagrams.values()
- Use the function to group the anagrams in your example.
oe1 = ["abc", "def"]
oe2 = ["asd", "cab", "fed", "eqw"]
oe3 = ["qwe", "efd", "bca"]
anagram_lists = group_anagrams(oe1, oe2, oe3)
# Print the result.
for anagrams in anagram_lists:
print(anagrams)
This should output the following:
[abc, cab, bca]
[asd]
[def, fed, efd]
[eqw, qwe]
Hope this helps!
To be easier, let merge them all together. Also, use condition with set()
to check whether 2 text are using the same set of characters.
However, my solution doesn’t require any sort()
. But I added it because I want the output to be the same as your desired output.
oe = oe1 + oe2 + oe3
oe.sort() # optional, no need for sorting
oe_group = []
for i in range(len(oe)):
if i == 0:
oe_group.append([oe[i]])
else:
for j in range(len(oe_group)):
if set(oe[i]) == set(oe_group[j][0]):
oe_group[j].append(oe[i])
break
if j == len(oe_group) - 1:
oe_group.append([oe[i]])
output
[['abc', 'bca', 'cab'], ['asd'], ['def', 'efd', 'fed'], ['eqw', 'qwe']]
First off, let’s combine those lists and sort them using a lambda that converts each string to a list of characters, then sorts that.
>>> sorted(oe1 + oe2 + oe3, key=lambda s: sorted(list(s)))
['abc', 'cab', 'bca', 'asd', 'def', 'fed', 'efd', 'eqw', 'qwe']
Then using itertools.groupby
to group them based on the same lambda.
>>> k = lambda s: sorted(list(s))
>>> [list(v) for _, v in groupby(sorted(oe1 + oe2 + oe3, key=k), key=k)]
[['abc', 'cab', 'bca'], ['asd'], ['def', 'fed', 'efd'], ['eqw', 'qwe']]
This can be simplified a bit further by not first converting to a list and just sorting each string.
>>> sorted(oe1 + oe2 + oe3, key=sorted)
['abc', 'cab', 'bca', 'asd', 'def', 'fed', 'efd', 'eqw', 'qwe']
>>> [list(v) for _, v in groupby(sorted(oe1 + oe2 + oe3, key=sorted), key=sorted)]
[['abc', 'cab', 'bca'], ['asd'], ['def', 'fed', 'efd'], ['eqw', 'qwe']]