Printing all anagrams of a given string from a text file
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
|
show 1 more comment
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
How are you calling yourfind_all_anagrams
function?
– slider
Nov 13 '18 at 0:16
@slider as suchprint(find_all_anagrams('python'))
– anon2000
Nov 13 '18 at 0:17
2
find_all_anagrams
isn't returning anything right now. Try addingreturn anagrams_list
at the end
– boonwj
Nov 13 '18 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 '18 at 0:20
So now that's a different problem :P Your code seems to only be reading the first line inenglish_words.txt
. Are you only comparing your input string with 1 word in the file?
– boonwj
Nov 13 '18 at 0:24
|
show 1 more comment
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
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
python anagram
edited Nov 13 '18 at 0:40
eyllanesc
74.7k103156
74.7k103156
asked Nov 13 '18 at 0:12
anon2000anon2000
135
135
How are you calling yourfind_all_anagrams
function?
– slider
Nov 13 '18 at 0:16
@slider as suchprint(find_all_anagrams('python'))
– anon2000
Nov 13 '18 at 0:17
2
find_all_anagrams
isn't returning anything right now. Try addingreturn anagrams_list
at the end
– boonwj
Nov 13 '18 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 '18 at 0:20
So now that's a different problem :P Your code seems to only be reading the first line inenglish_words.txt
. Are you only comparing your input string with 1 word in the file?
– boonwj
Nov 13 '18 at 0:24
|
show 1 more comment
How are you calling yourfind_all_anagrams
function?
– slider
Nov 13 '18 at 0:16
@slider as suchprint(find_all_anagrams('python'))
– anon2000
Nov 13 '18 at 0:17
2
find_all_anagrams
isn't returning anything right now. Try addingreturn anagrams_list
at the end
– boonwj
Nov 13 '18 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 '18 at 0:20
So now that's a different problem :P Your code seems to only be reading the first line inenglish_words.txt
. Are you only comparing your input string with 1 word in the file?
– boonwj
Nov 13 '18 at 0:24
How are you calling your
find_all_anagrams
function?– slider
Nov 13 '18 at 0:16
How are you calling your
find_all_anagrams
function?– slider
Nov 13 '18 at 0:16
@slider as such
print(find_all_anagrams('python'))
– anon2000
Nov 13 '18 at 0:17
@slider as such
print(find_all_anagrams('python'))
– anon2000
Nov 13 '18 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 '18 at 0:18
find_all_anagrams
isn't returning anything right now. Try adding return anagrams_list
at the end– boonwj
Nov 13 '18 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 '18 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 '18 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 '18 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 '18 at 0:24
|
show 1 more comment
1 Answer
1
active
oldest
votes
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
1
this is great! thank you! it worked!
– anon2000
Nov 13 '18 at 0:29
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
1
this is great! thank you! it worked!
– anon2000
Nov 13 '18 at 0:29
add a comment |
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
1
this is great! thank you! it worked!
– anon2000
Nov 13 '18 at 0:29
add a comment |
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
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
answered Nov 13 '18 at 0:27
sliderslider
8,10011129
8,10011129
1
this is great! thank you! it worked!
– anon2000
Nov 13 '18 at 0:29
add a comment |
1
this is great! thank you! it worked!
– anon2000
Nov 13 '18 at 0:29
1
1
this is great! thank you! it worked!
– anon2000
Nov 13 '18 at 0:29
this is great! thank you! it worked!
– anon2000
Nov 13 '18 at 0:29
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
How are you calling your
find_all_anagrams
function?– slider
Nov 13 '18 at 0:16
@slider as such
print(find_all_anagrams('python'))
– anon2000
Nov 13 '18 at 0:17
2
find_all_anagrams
isn't returning anything right now. Try addingreturn anagrams_list
at the end– boonwj
Nov 13 '18 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 '18 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 '18 at 0:24