Hey all, still working on my lil program. But now that I'm getting into loops I'm pretty confused on this one. Needless to say I think this is the first while loop I have used since I started python about a week ago. What I'm trying to do at this point is narrow the possibilities down by checking the list for an instance of charecters in the scrambled word. Well I thought this would run through the list and check each item, but the problem is the index seems to be going out of range, I thought because as it is deleting items there is no update to the variable. So I tried putting something like c = c - 1 after deleting but I think this made it worse after printing the index number after and getting negative results. I assume it has something to do with the for loop its wrapped inside, but not sure. Anyway this is the code any help or suggestions would be awsome, thanks btw for all the help so far.

word = possible[p]
x = len(matches)
while c < x:
    c = c + 1
    print len(matches)
    print c
    for letter in word:
        if letter in matches[c]:
            nothing = 0
        else:
            del matches[c]

Recommended Answers

All 7 Replies

It's not exactly clear what this example is doing.. Plus this isn't a stand-alone example of the problem that you are seeing since we don't know what possible and matches are (ie, are they lists, tuples, dictionaries, strings?) ...

If you could provide us with an idea of exactly what you're trying to do with an example of input -> output it would help greatly!

x = len(matches) so when you delete a character, the length of matches is reduced but x remains the same. There are several ways to do this. The easiest would be to test for
if word.lower() in matches.lower(), but if you want to use this example then remove the del matches[c] line since it serves no purpose as far as I can tell. If that doesn't solve your problem, then post back with some sample data.

Hey guys, sorry I thought I had enough of the code to make it understandable what was goin on. Here is the rest of the code. I'm trying to run through the list to check if the two words have the same charecters in them. If they don't I want it to delete that entry from the list. Thanks a bunch.

#decode scrambled word from a word list in 30 sec or less.
wordlist = []
possible = []
matches = []
wordlist = []
i = -1
c = -1
p = 0
wordlistfile = open('C:\\Documents and Settings\\William\\Desktop\\wordlist\\wordlist.txt', 'r')
possiblefile = open('C:\\Documents and Settings\\William\\Desktop\\possible.txt', 'r')

#add file contents to lists.
for line in wordlistfile:
    wordlist.append(line.strip())
for line in possiblefile:
    possible.append(line.strip())
#match length of words to narrow possible matches
for line in wordlist:
    i = i + 1
    if len(wordlist[i]) == len(possible[p]):
        matches.append(wordlist[i])
#narrow further by checking for instances of charecters
word = possible[p]
x = len(matches)
while c < x:
    c = c + 1
    print len(matches)
    print c
    for letter in word:
        if letter in matches[c]:
            nothing = 0
        else:
            del matches[c]

Ah, now it makes slightly more sense...

First things first: to remove an item from a list:

>>> mathches = [ 1,2,3,4,5 ]
>>> mathches.remove(3)
>>> mathches
[1, 2, 4, 5]
>>>

also, instead of nothing = 0 ; which I'm assuming is just there for the syntax you could use pass . Or simply reverse your logic and do away with a second case:

if not letter in matches[c]:
     matches.remove(c)

That way you don't even need to worry about an else case. Because really you should never need a "do nothing here" line in your code.

OK I modified the if statement, but when I tried the matches.remove its trying to remove the letter instead of the list item. If I use use del matches its taking that item out of the list thus making the list smaller, but you are right the length stays the same in the while statement, which I thought it would update it every time it runs the while loop. Is there a way to modify the code so that the index can be updated every time through the loop?

Not sure if this helps or not either but when I print out c or x after the del statement they merge at around the value of 40 and that seems to be when c goes out of the range of the index.

OK just figured it out, the while loop was running one to many times thus the last time throwing it out of index. Here is my revised code to handle it.

#decode scrambled word from a word list in 30 sec or less.
wordlist = []
possible = []
matches = []
wordlist = []
i = -1
c = -1
p = 0
wordlistfile = open('C:\\Documents and Settings\\William\\Desktop\\wordlist\\wordlist.txt', 'r')
possiblefile = open('C:\\Documents and Settings\\William\\Desktop\\possible.txt', 'r')

#add file contents to lists.
for line in wordlistfile:
    wordlist.append(line.strip())
for line in possiblefile:
    possible.append(line.strip())
#match length of words to narrow possible matches
for line in wordlist:
    i = i + 1
    if len(wordlist[i]) == len(possible[p]):
        matches.append(wordlist[i])
#narrow further by checking for instances of charecters
word = possible[p]
x = len(matches)
while c < x:
    c = c + 1
    for letter in word:
        if c == x:
            break
        if not letter in matches[c]:
            del matches[c]
            x = x - 1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.