Printing all anagrams of a given string from a text file












0














I'm writing a program which takes the input string and prints out all anagrams of the string, from a text file, in a list.



So far, I've got the following; but nothing is returned and I don't quite understand why.



def anagrams( s1, s2 ):
s1_list = list(s1.lower())
s1_list.sort()
s2_list = list(s2.lower())
s2_list.sort()

if s1.lower() == s2.lower():
return(False)
elif s1_list == s2_list:
return(True)
else:
return(False)

def find_all_anagrams( string ):

anagrams_list =

with open("english_words.txt", "r") as fileObject:

line = fileObject.readline()

if anagrams(string, line):
anagrams_list.append(line)
return anagrams_list









share|improve this question
























  • How are you calling your find_all_anagrams function?
    – slider
    Nov 13 at 0:16










  • @slider as such print(find_all_anagrams('python'))
    – anon2000
    Nov 13 at 0:17






  • 2




    find_all_anagrams isn't returning anything right now. Try adding return anagrams_list at the end
    – boonwj
    Nov 13 at 0:18












  • @boonwj aah yes. forgot to add that, but upon adding that nothing seems to be in the list and I can't quite figure out why. The list is empty with no found anagrams
    – anon2000
    Nov 13 at 0:20










  • So now that's a different problem :P Your code seems to only be reading the first line in english_words.txt. Are you only comparing your input string with 1 word in the file?
    – boonwj
    Nov 13 at 0:24


















0














I'm writing a program which takes the input string and prints out all anagrams of the string, from a text file, in a list.



So far, I've got the following; but nothing is returned and I don't quite understand why.



def anagrams( s1, s2 ):
s1_list = list(s1.lower())
s1_list.sort()
s2_list = list(s2.lower())
s2_list.sort()

if s1.lower() == s2.lower():
return(False)
elif s1_list == s2_list:
return(True)
else:
return(False)

def find_all_anagrams( string ):

anagrams_list =

with open("english_words.txt", "r") as fileObject:

line = fileObject.readline()

if anagrams(string, line):
anagrams_list.append(line)
return anagrams_list









share|improve this question
























  • How are you calling your find_all_anagrams function?
    – slider
    Nov 13 at 0:16










  • @slider as such print(find_all_anagrams('python'))
    – anon2000
    Nov 13 at 0:17






  • 2




    find_all_anagrams isn't returning anything right now. Try adding return anagrams_list at the end
    – boonwj
    Nov 13 at 0:18












  • @boonwj aah yes. forgot to add that, but upon adding that nothing seems to be in the list and I can't quite figure out why. The list is empty with no found anagrams
    – anon2000
    Nov 13 at 0:20










  • So now that's a different problem :P Your code seems to only be reading the first line in english_words.txt. Are you only comparing your input string with 1 word in the file?
    – boonwj
    Nov 13 at 0:24
















0












0








0







I'm writing a program which takes the input string and prints out all anagrams of the string, from a text file, in a list.



So far, I've got the following; but nothing is returned and I don't quite understand why.



def anagrams( s1, s2 ):
s1_list = list(s1.lower())
s1_list.sort()
s2_list = list(s2.lower())
s2_list.sort()

if s1.lower() == s2.lower():
return(False)
elif s1_list == s2_list:
return(True)
else:
return(False)

def find_all_anagrams( string ):

anagrams_list =

with open("english_words.txt", "r") as fileObject:

line = fileObject.readline()

if anagrams(string, line):
anagrams_list.append(line)
return anagrams_list









share|improve this question















I'm writing a program which takes the input string and prints out all anagrams of the string, from a text file, in a list.



So far, I've got the following; but nothing is returned and I don't quite understand why.



def anagrams( s1, s2 ):
s1_list = list(s1.lower())
s1_list.sort()
s2_list = list(s2.lower())
s2_list.sort()

if s1.lower() == s2.lower():
return(False)
elif s1_list == s2_list:
return(True)
else:
return(False)

def find_all_anagrams( string ):

anagrams_list =

with open("english_words.txt", "r") as fileObject:

line = fileObject.readline()

if anagrams(string, line):
anagrams_list.append(line)
return anagrams_list






python anagram






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 0:40









eyllanesc

72.6k93054




72.6k93054










asked Nov 13 at 0:12









anon2000

135




135












  • How are you calling your find_all_anagrams function?
    – slider
    Nov 13 at 0:16










  • @slider as such print(find_all_anagrams('python'))
    – anon2000
    Nov 13 at 0:17






  • 2




    find_all_anagrams isn't returning anything right now. Try adding return anagrams_list at the end
    – boonwj
    Nov 13 at 0:18












  • @boonwj aah yes. forgot to add that, but upon adding that nothing seems to be in the list and I can't quite figure out why. The list is empty with no found anagrams
    – anon2000
    Nov 13 at 0:20










  • So now that's a different problem :P Your code seems to only be reading the first line in english_words.txt. Are you only comparing your input string with 1 word in the file?
    – boonwj
    Nov 13 at 0:24




















  • How are you calling your find_all_anagrams function?
    – slider
    Nov 13 at 0:16










  • @slider as such print(find_all_anagrams('python'))
    – anon2000
    Nov 13 at 0:17






  • 2




    find_all_anagrams isn't returning anything right now. Try adding return anagrams_list at the end
    – boonwj
    Nov 13 at 0:18












  • @boonwj aah yes. forgot to add that, but upon adding that nothing seems to be in the list and I can't quite figure out why. The list is empty with no found anagrams
    – anon2000
    Nov 13 at 0:20










  • So now that's a different problem :P Your code seems to only be reading the first line in english_words.txt. Are you only comparing your input string with 1 word in the file?
    – boonwj
    Nov 13 at 0:24


















How are you calling your find_all_anagrams function?
– slider
Nov 13 at 0:16




How are you calling your find_all_anagrams function?
– slider
Nov 13 at 0:16












@slider as such print(find_all_anagrams('python'))
– anon2000
Nov 13 at 0:17




@slider as such print(find_all_anagrams('python'))
– anon2000
Nov 13 at 0:17




2




2




find_all_anagrams isn't returning anything right now. Try adding return anagrams_list at the end
– boonwj
Nov 13 at 0:18






find_all_anagrams isn't returning anything right now. Try adding return anagrams_list at the end
– boonwj
Nov 13 at 0:18














@boonwj aah yes. forgot to add that, but upon adding that nothing seems to be in the list and I can't quite figure out why. The list is empty with no found anagrams
– anon2000
Nov 13 at 0:20




@boonwj aah yes. forgot to add that, but upon adding that nothing seems to be in the list and I can't quite figure out why. The list is empty with no found anagrams
– anon2000
Nov 13 at 0:20












So now that's a different problem :P Your code seems to only be reading the first line in english_words.txt. Are you only comparing your input string with 1 word in the file?
– boonwj
Nov 13 at 0:24






So now that's a different problem :P Your code seems to only be reading the first line in english_words.txt. Are you only comparing your input string with 1 word in the file?
– boonwj
Nov 13 at 0:24














1 Answer
1






active

oldest

votes


















0














You need to iterate through all the words (I'm assuming one per line) in english_words.txt. You also need strip any newline characters from each word before you call anagrams().



def find_all_anagrams( string ):
anagrams_list =
with open("english_words.txt", "r") as fileObject:
for line in fileObject: # check all lines
word = line.strip() # strip newline characters
if anagrams(string, word):
anagrams_list.append(word)
return anagrams_list # finally return results





share|improve this answer

















  • 1




    this is great! thank you! it worked!
    – anon2000
    Nov 13 at 0:29











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271953%2fprinting-all-anagrams-of-a-given-string-from-a-text-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You need to iterate through all the words (I'm assuming one per line) in english_words.txt. You also need strip any newline characters from each word before you call anagrams().



def find_all_anagrams( string ):
anagrams_list =
with open("english_words.txt", "r") as fileObject:
for line in fileObject: # check all lines
word = line.strip() # strip newline characters
if anagrams(string, word):
anagrams_list.append(word)
return anagrams_list # finally return results





share|improve this answer

















  • 1




    this is great! thank you! it worked!
    – anon2000
    Nov 13 at 0:29
















0














You need to iterate through all the words (I'm assuming one per line) in english_words.txt. You also need strip any newline characters from each word before you call anagrams().



def find_all_anagrams( string ):
anagrams_list =
with open("english_words.txt", "r") as fileObject:
for line in fileObject: # check all lines
word = line.strip() # strip newline characters
if anagrams(string, word):
anagrams_list.append(word)
return anagrams_list # finally return results





share|improve this answer

















  • 1




    this is great! thank you! it worked!
    – anon2000
    Nov 13 at 0:29














0












0








0






You need to iterate through all the words (I'm assuming one per line) in english_words.txt. You also need strip any newline characters from each word before you call anagrams().



def find_all_anagrams( string ):
anagrams_list =
with open("english_words.txt", "r") as fileObject:
for line in fileObject: # check all lines
word = line.strip() # strip newline characters
if anagrams(string, word):
anagrams_list.append(word)
return anagrams_list # finally return results





share|improve this answer












You need to iterate through all the words (I'm assuming one per line) in english_words.txt. You also need strip any newline characters from each word before you call anagrams().



def find_all_anagrams( string ):
anagrams_list =
with open("english_words.txt", "r") as fileObject:
for line in fileObject: # check all lines
word = line.strip() # strip newline characters
if anagrams(string, word):
anagrams_list.append(word)
return anagrams_list # finally return results






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 at 0:27









slider

8,0151129




8,0151129








  • 1




    this is great! thank you! it worked!
    – anon2000
    Nov 13 at 0:29














  • 1




    this is great! thank you! it worked!
    – anon2000
    Nov 13 at 0:29








1




1




this is great! thank you! it worked!
– anon2000
Nov 13 at 0:29




this is great! thank you! it worked!
– anon2000
Nov 13 at 0:29


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271953%2fprinting-all-anagrams-of-a-given-string-from-a-text-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)