buttHurtBuddy
buttHurtBuddy

Reputation: 35

Python: Check if all characters from a word exists in a string in any order

I want to check if two strings are anagrams. For example, if my word is "halo", I want to check if those letters appear in "loha". It should match because it's an anagram.

My attempt fails and I'm not sure why. My code and output is below. I have a list of words and I want to check to see which elements are anagrams in the list.

def anagram(myList):
    for elem in myList:
        chars = set(elem)
        if all((c in chars) for c in myList):
            print  "Yes, anagram ", elem, chars
        else:
            print "NOT anagram ", elem, chars


wordsList = ["halo", "loha", "ahlo", "sully"]
anagram(wordsList)

And here's my output

NOT anagram  halo set(['a', 'h', 'l', 'o'])
NOT anagram  loha set(['a', 'h', 'l', 'o'])
NOT anagram  ahlo set(['a', 'h', 'l', 'o'])
NOT anagram  sully set(['y', 's', 'u', 'l'])

Upvotes: 1

Views: 3045

Answers (7)

Louis Barto
Louis Barto

Reputation: 11

Use sets:

set('hola')==set('loha')

outputs True

As noted in the comments, this will not work even with a len parameter attached. Here's a recursive anagram test to redeem myself

`def anagram(string1,string2):
if string1=='' or string2== '':
    if string1!='' or string2!='':
        return False
    else:
        return True
if len(string1)!=len(string2) or set(string1)!=set(string2):
    return False
stemp1 = list(string1)
stemp2 = list(string2)
stemp2.remove(stemp1[0])
return anagram(''.join(stemp1[1:]),''.join(stemp2))`

Upvotes: 0

satyakrish
satyakrish

Reputation: 119

Below code takes a list of words as input and groups them into anagrams

def is_grouped(word, anagram_groups):
    is_grouped = False
    for group in anagram_groups:
        if word in group:
            print "{0} is already part of anagram group={1}".format(word, group)
            is_grouped = True
            break
    return is_grouped

def get_anagram_group(word_to_check, word_to_check_index, wordlist):
    word_to_check_as_list = list(word_to_check)
    word_to_check_as_list.sort()
    group = [word_to_check]
    for i in range(0, len(wordlist)):
        if not i == word_to_check_index:
            word = wordlist[i]
            word_as_list = list(word)
            word_as_list.sort()
            if word_as_list == word_to_check_as_list:
                group.append(word)   
    return group

def group_anagrams(wordlist):
    anagram_groups = []
    for i in range(0, len(wordlist)):

        word_under_test = wordlist[i]
        # check if the word's anagrams already identified as part of anagram group
        anagramed = is_grouped(word_under_test, anagram_groups)
        if not anagramed:
            # if word not already a part of anagram group then find find all anagrams for the word
            anagram_group = get_anagram_group(word_under_test, i, wordlist)
            if len(anagram_group) == 1:
                print "no anagrams found for word {0}".format(word_under_test)
            else:
                print "anagrams found for word {0}={1}".format(word_under_test, anagram_group)
                anagram_groups.append(anagram_group)
    return anagram_groups



wlist = ['aloha' , 'hoala', 'loaha', '123', '321', 'xya']
print group_anagrams(wlist)

output:

anagrams found for word aloha=['aloha', 'hoala', 'loaha']
hoala is already part of anagram group=['aloha', 'hoala', 'loaha']
loaha is already part of anagram group=['aloha', 'hoala', 'loaha']
anagrams found for word 123=['123', '321']
321 is already part of anagram group=['123', '321']
no anagrams found for word xya
[['aloha', 'hoala', 'loaha'], ['123', '321']]

Upvotes: 0

ragardner
ragardner

Reputation: 1975

This should prove to be relatively fast in that it keeps a sorted list so it only has to make ONE new one every time it iterates.

from itertools import islice

def anagram(myList):
    s1 = sorted(myList[0])
    for i,elem in enumerate(islice(myList,1,len(myList))):
        s2 = sorted(elem)
        if s1 == s2:
            print ("Yes anagram: ",myList[i],elem)
        else:
            print ("Not anagram: ",myList[i],elem)
        s1 = s2

wordsList = ["halo", "loha", "ahlo", "sully"]
anagram(wordsList)

result:

Yes anagram:  halo loha
Yes anagram:  loha ahlo
Not anagram:  ahlo sully

Different structure option:

from itertools import islice

def is_anagram(sorted_1,sorted_2):
    if sorted_1 == sorted_2:
        return True
    return False

wordsList = ["halo", "loha", "ahlo", "sully"]

s1 = sorted(wordsList[0])
for i,elem in enumerate(islice(wordsList,1,len(wordsList))):
    s2 = sorted(elem)
    if is_anagram(s1,s2):
        print ("Yes anagram: ",wordsList[i],elem)
    else:
        print ("Not anagram: ",wordsList[i],elem)
    s1 = s2

Upvotes: 0

Austin
Austin

Reputation: 26039

Simply use sorted() on both strings and compare them.

def is_anagram(str1, str2):
   return sorted(str1) == sorted(str2)

k = is_anagram('angel', 'glean')
if k == True:
   print('Strings are anagrams')
else:
   print('Strings are not anagrams')

Here, sorted('angel') outputs ['a', 'e', 'g', 'l', 'n'] and sorted('glean') also outputs ['a', 'e', 'g', 'l', 'n']. Both equal, hence are anagrams.

To make things clear:

>>> sorted('angel') 
['a', 'e', 'g', 'l', 'n'] 
>>> sorted('glean') 
['a', 'e', 'g', 'l', 'n'] 
>>> sorted('angel') == sorted('glean') 
True

Solution to your problem:

def is_anagram(str1, str2):
   return sorted(str1) == sorted(str2)

wordsList = ["halo", "loha", "ahlo", "sully"]
each = wordsList[0]
for another in wordsList:
   if each != another:
      k = is_anagram(each, another)
      if k == True:
         print('{} and {} are anagrams'.format(each, another))
      else:
         print('{} and {} are not anagrams'.format(each, another))

Output:

halo and loha are anagrams
halo and ahlo are anagrams
halo and sully are not anagrams

Upvotes: 0

Whooper
Whooper

Reputation: 625

If you want to find all the angrams in your list, you might want to do this instead:

# Checks if two strings are anagrams
def isAnagram(str1, str2):
    return sorted(str1) == sorted(str2)

# Iterates over all items in your list and compares it with all other items to check if they are anagrams.
def anagrams(myList):
    for i in range(0,len(myList)):
        for j in range(i+1, len(myList)):
            if isAnagram(myList[i], myList[j]):
                print "Yes anagram: ", myList[i], myList[j]
            else:
                print "Not anagram: ", myList[i], myList[j]

wordsList = ["halo", "loha", "ahlo", "sully"]
anagram(wordsList)

This would return the following:

Yes anagram: halo loha
Yes anagram: halo ahlo
Not anagram: halo sully
Yes anagram: loha ahlo
Not anagram: loha sully
Not anagram: ahlo sully

This may not be the most efficient solution but it gets the job done.

Upvotes: 0

Pratik Kumar
Pratik Kumar

Reputation: 2231

you can use counter which outputs a dictionary of letter counts for each letter in a word

from Collections import Counter

word1='halo'
word2='hola'

if Counter(word1) == Counter(word2):
   print 'Yes Anagram {} {}'.format(word1,word2)
else :
   print 'Not Anagram {} {}'.format(word1,word2)

Upvotes: 0

Artier
Artier

Reputation: 1673

Try this

def isanagram(ele1,ele2):
    ele1=list(ele1)
    ele2=list(ele2)
    return(sorted(ele1)==sorted(ele2))

print(isanagram("ahlo", "halo"))

Out put

True

For multiple elements check

print(map(isanagram,["ahlo", "halo"],[ "alho", "sully"])

Out put

[True, False]

Upvotes: 1

Related Questions