2
  1. When #validCandidateList += word stays commented, program runs fine. When this line is uncommented, program starts printing out some repeated line over and over again.

  2. 2. As you can see sorted(set(eWord)) is sorted, so for example if I input "dog", or "good", it creates same sorted list of letters - ['d', 'g', 'o'], but the program does not print the out the word good when input is dog, even though you can confirm that both words exist in the dictionary, by inputting them in a single run of program, separated by spaces, or inputting one of the words in different program runs.I am sorry, I just had to apply .lowercase() to the word. Dictionary contains some uppercase letters too.

    Please help.

import os

cwd = os.path.dirname(os.path.abspath(__file__))
fname = "\\dictionary.txt"
file = open(cwd + fname, "r")
readFile = file.read()
dictionary = readFile.split() #list type variable with more than 400 000 words.

input = input("Input your list of words, separated by spaces: ")
inputList = input.split()

validCandidateList = inputList

for eWord in dictionary: 
    for word in inputList:
        if sorted(set(eWord)) == sorted(set(word)):
            print(word, ": ",eWord)
            #validCandidateList += word

print(validCandidateList)
Caribbean
  • 25
  • 5

3 Answers3

2

I found it!

Your line here:

validCandidateList = inputList

Does not copy the contents of inputList into a new variable named validCandidateList as you expect, it just links the two variables, so when you change validCandidateList inside your loop, it also changes inputList as well, which you are trying to loop over. Changing the list you are looping over causes big issues in Python (don't do it). So to fix this, you need to actually copy the contents of inputList into validCandidateList like this:

validCandidateList = inputList[:]

Alternatively you can use the copy() function if you're using Python 3

validCandidateList = inputList.copy()

This looks a bit more obvious for what you're doing, but either are perfectly fine :)

Hoog
  • 2,280
  • 1
  • 14
  • 20
0

As for the first problem you mentioned:

these lines are the origin of it:

inputList = input.split()
validCandidateList = inputList

this is a reference assignment, that means whenever validCandidateList changes, inputList will change too.

you loop over inputList and inside the loop you change validCandidateList, which basically extends the loop, and you see the repeated output.

Example:

x = [1,2,3]
y = x
y.append(4)
print(x)
# output => [1,2,3,4]

in order to correct that behavior, you can use the copy operator:

validCandidateList = inputList.copy()

this makes a shallow copy of your list and make a new reference.

More info on how copy works can be found in the official docs here

hope this helps.

Ammar
  • 1,305
  • 2
  • 11
  • 16
0

I think you need to initialise your list with an empty list, rather than adding to the existing list, try this:

validCandidateList = []

Then later on

validCandidateList.append(word)

Hope this helps!

Ash Oldershaw
  • 302
  • 2
  • 13