473,766 Members | 2,172 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.c...stSudokuSolver

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 =----
Aug 1 '06 #1
4 1812
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.
Aug 1 '06 #2
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.c...stSudokuSolver

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_express ion on one side and solve(...) on the other, like this
complex_express ion or solve(...)
*and* the complex_express ion 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.

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 =----
Aug 1 '06 #3
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" <du**********@i nvalid.invalidw rote in message
news:Xn******** *************** **@127.0.0.1...
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 =----
Aug 1 '06 #4
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.c...stSudokuSolver

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_express ion on one side and solve(...) on the other, like this
complex_express ion or solve(...)
*and* the complex_express ion 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 =----
Aug 1 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
15248
by: jsaul | last post by:
Hi there, wouldn't it be useful to have a 'while' conditional in addition to 'if' in list comprehensions? foo = for i in bar: if len(i) == 0: break foo.append(i)
35
2981
by: Moosebumps | last post by:
Does anyone here find the list comprehension syntax awkward? I like it because it is an expression rather than a series of statements, but it is a little harder to maintain it seems. e.g. you could do: result = for element in list: if element == 'blah':
6
1495
by: Eric | last post by:
Pythonistas, I seem at a loss for a List Comprehension syntax that will do what I want. I have a list of string position spans: >>> breaks the first pair representing: someString
24
3356
by: Mahesh Padmanabhan | last post by:
Hi, When list comprehension was added to the language, I had a lot of trouble understanding it but now that I am familiar with it, I am not sure how I programmed in Python without it. Now I see that generator expressions have been added to the language with 2.4 and I question the need for it. I know that it allows for lazy evaluation which speeds things up for larger lists but why was it necessary to add it instead of improving list...
15
1624
by: Darren Dale | last post by:
Hi, I need to replace the following loop with a list comprehension: res= for i in arange(10000): res=res+i In practice, res is a complex 2D numarray. For this reason, the regular output of a list comprehension will not work: constructing a list of every
6
2171
by: Heiko Wundram | last post by:
Hi all! The following PEP tries to make the case for a slight unification of for statement and list comprehension syntax. Comments appreciated, including on the sample implementation. === PEP: xxx Title: Unification of for-statement and list-comprehension syntax
6
2006
by: fdu.xiaojf | last post by:
Hi all, I can use list comprehension to create list quickly. So I expected that I can created tuple quickly with the same syntax. But I found that the same syntax will get a generator, not a tuple. Here is my example: In : a = (i for i in range(10)) In : b =
4
3057
by: beginner | last post by:
Hi All, If I have a list comprehension: ab= c = "ABC" print c
0
1792
by: Hatem Nassrat | last post by:
on Wed Jun 13 10:17:24 CEST 2007, Diez B. Roggisch deets at nospam.web.de wrote: Well I have looked into this and it seems that using the list comprehension is faster, which is reasonable since generators require iteration and stop iteration and what not.
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9838
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.