0

I try to sort this phrase only by identical letters in the word Like 'deltas', 'desalt' which are made up of the same letters.

list_of_words = ['deltas', 'retainers', 'desalt', 'pants', 'slated', 'generating', 'ternaries', 'smelters',
                     'termless', 'salted', 'staled', 'greatening', 'lasted', 'resmelts']
def sort_anagrams(list_of_strings):
    newbox = []
    for x in list_of_words:
        for y in list_of_words:
            if y not in newbox:
                if sorted(x) == sorted(y):
                    newbox.append(y)
    return newbox

That's what I get

['deltas', 'desalt', 'slated', 'salted', 'staled', 'lasted', 'retainers', 'ternaries', 'pants', 'generating', 'greatening', 'smelters', 'termless', 'resmelts']

But I need each organ as a list as in the example here

[['deltas', 'desalt', 'slated', 'salted', 'staled', 'lasted'], ['retainers', 'ternaries'], ['pants'], ['generating', 'greatening'], ['smelters', 'termless', 'resmelts']]
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
topman2
  • 23
  • 2
  • 1
    Okay, so what do you think are the logical steps to solving the problem? What part can't you figure out by yourself? Try writing out the plan with pencil and paper, in plain English words (I'm serious). – Karl Knechtel Mar 25 '21 at 08:04
  • Does this answer your question? [Finding and grouping anagrams by Python](https://stackoverflow.com/questions/8181513/finding-and-grouping-anagrams-by-python) – DjaouadNM Mar 25 '21 at 08:09

3 Answers3

0

You need a mapping of some kind to identify that a word in a list is an anagram of other words.

I would suggest to use a dictionary for that purpose. The code also gets simplified using the dictionary approach:

def sort_anagrams(list_of_strings):
    newbox = {}
    for x in list_of_words:
        sorted_word = ''.join(sorted(x))
        if sorted_word in newbox.keys():
            newbox[sorted_word].append(x)
        else:
            newbox[sorted_word] = [x]

    return newbox.values()

Further, you can make use of setdefault to get rid of if-else logic:

def sort_anagrams(list_of_strings):
    newbox = {}
    for x in list_of_words:
        sorted_word = ''.join(sorted(x))
        newbox.setdefault(sorted_word, []).append(x)

    return newbox.values()
Krishna Chaurasia
  • 8,924
  • 6
  • 22
  • 35
0

On top of my head, I can suggest a few things for a filter that could work.

  1. Sort all the strings based on their lengths. And put the once with the same string length in an array

  2. Inside the array, for each array, sort the string and check if it's the same as others if they are the same, keep them in the same array or pop them, then move them in a new array.

0

There are probably better ways to do it, without so many intermediary steps, but this appears to work as required.

list_of_words = ['deltas', 'retainers', 'desalt', 'pants', 'slated', 'generating', 'ternaries', 'smelters',
                     'termless', 'salted', 'staled', 'greatening', 'lasted', 'resmelts']
def sort_anagrams(list_of_strings):
    newbox = {}
    check = sorted(set([''.join(chars) for chars in list(map(sorted, list_of_words))]))
    for word in check:
        newbox[word] = []
    for wd in list_of_words:
        newbox[''.join(sorted(wd))].append(wd)
            
    return [words for words in newbox.values()]

result = sort_anagrams(list_of_words)

print(result)
norie
  • 9,609
  • 2
  • 11
  • 18