What I'm trying to do is if I have a list like:
["lime", "mile", "liem", "tag", "gat", "goat", "math"]
I want to write a function that returns the words in the list that have an anagram, which would look like:
["lime", "mile", "liem", "tag", "gat",]
So far I have this code:
def anagramprinter(x):
output = []
for i in x:
for n in i:
if n in x[i]:
I can't past this part and would like some help and would also appreciate a thorough explanation as well.
Can anyone please show me a way that doesn't involve importing? Thanks
Thanks.
Checks two given strings if they're anagrams or not. Those strings may include spaces, numbers or special characters
So here's your code. Enjoy!
The easy way to analyses anagram words is to put them in alphabetic order.So you create a second list with alphabetic ordered words.
I think you can have more pythonesque code than the one i give you, but i think the important thing for you is to order letters in the word.
Now you can do something to analyse your anagrams
An algorithm to check if two words are anagrams in python.
1) Take two words: e.g.
("mile", "lime") ("tiles", "miles")
2) Make a string array/list:
(['m', 'i', 'l', 'e'], ['l', 'i', 'm', 'e']) (['t', 'i', 'l','e', 's'], ['m', 'i', 'l', 'e', 's'])
3) Sort arrays
(['e', 'i', 'l', 'm'], ['e', 'i', 'l', 'm']) (['e', 'i', 'l','s', 't'], ['e', 'i', 'l', 'm', 's'])
4)Check if
first_array[i]
==second_array[i]
for0<=i<=len(first_array)||second_array
5) Conclusion. If 4) is held, return true, else false.
an approach identifying the words by the
frozenset
of the characters:the output is not yet quite what you wanted, but flattening that list is easy if you prefer that.
update after comments:
the solution above will not handle words with repeating characters well. but this will (this time the key of the dictionary is just the string consisting of the sorted letters):
That's a decent start (though it would be clearer if you named the variables something like 'wordlist', 'word' (or even 'w'), and 'char' or 'c'...). But a couple issues:
1: for each word ('i'), you need to compare the other words, hoping to find at least one that is an anagram of i.
2: you need to see if any character fails to be found.
You could start like this:
That's close, but isn't actually enough, because to be a true anagram you have to have the same number of occurrences of each letter, not just the same set of distinct letters (consider 'mail' vs 'milla' or 'mailmailmail').
One way to do that would be to make a copy of w2, and then as you go through the characters of w1, remove the letter in that copy that matches against each letter of w1. That way it can't match twice. And, you'd need to make sure the copy has become empty when you 're done with the 'c' loop.
There are many other ways; some clever ones involve "collection" types such as set and multiset. And as Captain Wise suggested, sorting the characters in each word alphabetically lets you just compare them, instead of looping through characters one at a time.
Hope that helps.
-s