list comprehension syntax..?
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 ] == 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 = -- http://mail.python.org/mailman/listinfo/python-list
Re: list comprehension syntax..?
Very helpful, thanks!! So I see that it parses as: m='1' a="asdf" b="1234" print [((m in a) or b) for m in '%d'%1234 ] I get it. Thanks, Greg "Duncan Booth" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > 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. > > You are spot on there. It is a list comprehension, but the resulting list > is just thrown away, so using a list comprehension is a complete waste of > time serving only to confuse the issue. Presumably it saved the author a > character or two. > > [ exp for var in seq ] > > when the result isn't used can be rewritten as: > > for var in seq: > exp > > and: > > exp1 or exp2 > > when the result is thrown away is just: > > if not exp1: > exp2 > > > So: > > [ 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 ] > > is equivalent to: > > inner = [(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) ] > for m in '3814697265625': >if m not in inner: > solve(board[:i]+m+board[i+1:]) > > (That inner list comprehension doesn't depend on m, so it doesn't need to > be reevaluated each time round the loop except, again, to save a few > characters.) > > The '%d'%5**18 looks to be a silly way to iterate through all possible > digits for m even though it does some of them twice while saving one > character over writing range(1,10). > > The strange expression I called 'inner' is a list containing the string > value board[j] if j is in the same row, column or block as i, or an > integer > for any other cells. So 'm not in inner' is true only if the value for m > is > not already used in that row column or block and is therefore a possible > candidate for that location in the board. == 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 = -- http://mail.python.org/mailman/listinfo/python-list
language design question
I am comparing Python to a few other scripting languages, and used a simple anagrams program as a sample. I was surprised ast a few python features that did not work as I would expect/wish; which caused less compact/expressive program styles that I wanted - reverting to a FORTRAN like series of assignments. For example, - why is len() not a member function of strings? Instead one says len(w). - Why doesn't sort() return a value? This would allow things like: key = '',join( list(word.lower().strip()).sort() ) instead: key = ... key.sort() key = ... - Another feature I assumed but it failed, is a nice default for dictionaries, and more += like operations; For example: to acculumate words in a dictionary - dict[key] += [word] Instead of: mark[key] = mark.get(key,[]) + [word] The former seems very intuitive, and clearer. I am a bit used to the compactness and convenient defaults of Perl, which would do this: my $key = join '', sort(split(//, lc($word))); push @{$anagrams{$key}}, $word I am curious why these "obvious" conveniences are not present. :-) Thansk for any context or insight. Best, Gregory Perl is great, and == 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 = -- http://mail.python.org/mailman/listinfo/python-list