0

I am learning python and tried the anagram generator exercise and then decided to try different methods of generating anagrams and then decided to generate sub anagrams. The problem I am having is that the lists need to be compared and then list_2 needs to be removed from list_1 to state whether it is a sub anagram, however this is not happening and when I run it the first letter is removed then one letter that is the same in both words. My code is below:

def sub_anagram(word_1, word_2): 
        #letters need to be sorted into alphabetical order
        list_1 = [c for c in word_1] #turns the string into a list
        #list_1.sort() #sorts the list alphabetically
        print(list_1) 
        list_2 = [c for c in word_2] #turns the string into a list
        #list_2.sort() #sorts the list alphabetically
        print(list_2)

        #then the lists get compared  
        #loop to check if any letters from list_2 are in list_1
        for list_2 in list_1: 
        #removes the letters in list_1 that are in
        list_1.remove(list_2)  list_2       
        print(list_2)
        print(list_1)
        list_1[len(list_1)-1] #checks the length of the list
        if list_2 == list_1:           
            print("It is a sub-anagram")#return true if it is a sub-anagram
        else:
            print("It is not a sub-anagram") #return false if not 

print("Sub-Anagram Antics")

#user inputs two words
#user input word 1
word_1 = input("Enter a word: ") 
#user input word 2
word_2 = input("Enter a word: ")


#call function 
sub_anagram(word_1, word_2)
SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73
Jane Doe
  • 9
  • 1
  • 2
    Could you give some examples? – jonrsharpe Nov 12 '15 at 16:03
  • 1
    Your indentation is not correct. After for list_2 in list_1, at least one line should be indented. When you say "list_2 needs to be removed from list_1", do you mean that the items in list_2 should be removed from list_1 or that list_2 should be an element within list_1? Providing an example of the input and expected output would help us to help you. – Tom Nov 12 '15 at 16:13
  • Your question does not contain a question. Your code looks like a complete code to me (even though I haven't tried it). So are you asking for help finding errors, or do you have a real conceptual question? – Raimund Krämer Nov 12 '15 at 16:16
  • As a general hint, you should try to use more meaningful names, as this improves readability and helps us understand your code, and it helps you keep the overview of what your code does. For example, a list does not have to be named list, instead you can name it with what it contains: `words` for example. The items within a list (like in your for loop) SHOULD not be called list, as this distracts from what the name represents. Rather call it `item` when iterating over arbitrary objects, or `words` when you have a list of words. – Raimund Krämer Nov 12 '15 at 16:24
  • 1
    @Jane: You don't return by `print`ing. – GingerPlusPlus Nov 12 '15 at 16:50
  • @Jane: I'm not sure if I understand you correctly; if 'anagrams' for you are words that are made from the same letters, you can check if they're anagrams by writing [`from collections import Counter`](https://docs.python.org/3/library/collections.html#collections.Counter); `Counter(word1) == Counter(word2)` – GingerPlusPlus Nov 12 '15 at 17:14
  • @GingerPlusPlus, she's looking for "sub-anagrams"--whether word2 is comprised of some (not necessarily all) of the letters found within word1. – Tom Nov 12 '15 at 17:46
  • @Tom: then possible solution is [`Multi_set`](http://stackoverflow.com/a/15208779/3821804)`(sub_anagram) <= Multi_set(super_anagram)` – GingerPlusPlus Nov 12 '15 at 18:07

2 Answers2

0

You have some badly formed python code here:

    for list_2 in list_1:  ## <<< == nothing indented under the for loop, meaning that the for loop is not actually looping

    #removes the letters in list_1 that are in
    list_1.remove(list_2)  list_2        ### <<<  Here you have a dangling expression, the second instance of "list_2"

And just to make your code more readable, I think your for loop should look like

    for ch in list_1:
        list2.remove(ch)

The idea is that you shouldn't label your loop variable "list_2" when it isn't a list...it is one element from a list.

gariepy
  • 3,576
  • 6
  • 21
  • 34
  • I think that the wrong indentation is caused by pasting it here without checking again, because otherwise this would be a syntax error and not even run. – Raimund Krämer Nov 12 '15 at 16:18
  • "shouldn't label your loop variable "list_2" when it isn't a list"...I don't think that's what she was trying to do though. I think she meant to loop through the items in list_2 and then remove them from list_1, but I can't be sure. – Tom Nov 12 '15 at 16:22
0

Is this what you were looking for:

def sub_anagram(word_1, word_2):
        #letters need to be sorted into alphabetical order
        list_1 = list(word_1) #turns the string into a list
        #list_1.sort() #sorts the list alphabetically
        print(list_1)
        list_2 = list(word_2) #turns the string into a list
        #list_2.sort() #sorts the list alphabetically
        print(list_2)

        #then the lists get compared
        #loop to check if any letters from list_2 are in list_1
        try:
            for letter in list_2:
                list_1.remove(letter)
            print('It is a sub-anagram')
        except ValueError:
            print('It is not a sub-anagram')

#user inputs two words
#user input word 1
word_1 = input("Enter a word: ")
#user input word 2
word_2 = input("Enter a word: ")


#call function
sub_anagram(word_1, word_2)

Also, as others have pointed out, you should be careful with your variable names. Your code was assigning different values at different times to the name list_2. Items within a list are not the same as the list itself.

Tom
  • 1,196
  • 2
  • 12
  • 29