Question

I'm working on a scrabble solver. I have 81 squares, A1 (top left corner) to I9 (bottom left) and 3 lists of lists of length 9 with 9 elements each for the rows, columns and sub grids respectively.

I also have a masterList = sub + col + row

For example:

row[0] = [['1A', '2A', '3A', '4A', '5A','6A','7A','8A','9A']]

When I do this:

test = [i for i in masterList if 'A1' in i]
print test
print len(test)

The length is only 2 (it should be 3) and it only prints from the first 2 lists that make up masterList (so in this instance, it does not find row[0].

 test2 = [i for i in row if 'A1' in i]
 print test2

Works correctly. I'm not sure why it is doing this.

Was it helpful?

Solution 2

Your first and second list comprehension are not identical:

test = [i for i in test if 'A1' in masterList]

is not the same as

 test2 = [i for i in row if 'A1' in i]

For one, the ONLY thing the first test checks is to see if 'A1' is in masterList, you're not actually iteratively checking anything.

I can't explain why/what you should be getting because it's not clear from your question what is contained in test initially, but the fact that you're checking to see if 'A1' in i in one list comp and if 'A1' in masterList in the other is probably the source of the discrepancy.

OTHER TIPS

You cannot place an assigment statement with a print. Separate them as follows:

test = [i for i in test if 'A1' in masterList]
print test
print len(test)

As you can see here: print a = ... is invalid syntax

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top