list comprehension syntax..?

Claudio Grondi claudio.grondi at freenet.de
Tue Aug 1 15:38:20 EDT 2006


Gary Herron wrote:
> Gregory Guthrie wrote:
> 
>>Sorry for a simple question- but I don't understand how to parse this use of 
>>a list comprehension.
>>
>>The "or" clauses are odd to me.
>>
>>It also seems like it is being overly clever (?) in using a lc expression as 
>>a for loop to drive the recursion.
>>
>>Thanks for any insight!
>>Gregory
>>-------------------------
>>
>>#  http://markbyers.com/moinmoin/moin.cgi/ShortestSudokuSolver
>>
>>def solve(board):
>> i=board.find('0')      # find next open cell
>> if i<0:                # done if none...
>>     print board; exit("Done")
>> [ m in [(i-j)%9*(i/9^j/9)*(i/27^j/27|i%9/3^j%9/3)
>>          or board[j] for j in range(81) ]
>>     or solve(board[:i]+m+board[i+1:]) for m in'%d'%5**18 ] 
>>  
> 
> 
> The "or" clause (as you call it) has nothing to do with the list
> comprehension.
> The syntax being used here is
> [ big_expression for m in something]
> *and* the big_expression contains an "or" OPERATOR, with a
> complex_expression on one side and solve(...) on the other, like this
> complex_expression or solve(...)
> *and* the complex_expression contains a nested list comprehension like this
> m in nested_lc
> *and* nested_lc is
> [ugly_expression for j in range(81)]
> *and* ugly_expression contains another "or" OPERATOR with ...
> *and* sigh...
> 
> Whoever put this expression together has made something that is
> completely unreadable, mostly unmaintainable, and not noticeably more
> efficient than code that is readable and maintainable.
> 
> Moreover, all the work of creating the outer list seems to be wasted
> since that list is just thrown out.
> 
> This is worse than "overly clever". Loops are for looping, list
> comprehension is for building lists. It is bad programming practice to
> use list comprehension for looping.
Isn't it an advantage considering speed of the execution?
I have just compared the speed of an explicit loop with the speed of a 
list comprehension doing the same thing in another context to see it is 
much better to use the latter. In board solvers speed is vital, so isn't 
it a good practice to use a list comprehension for looping in this 
context, anyway?

Claudio
> 
> Hope that helps you,
> Gary Herron
> 
> 
>>
>>----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
>>http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
>>----= East and West-Coast Server Farms - Total Privacy via Encryption =----
>>  



More information about the Python-list mailing list