473,703 Members | 2,690 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 1810
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
15233
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
2971
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
1493
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
3347
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
1619
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
2166
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
2004
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
3055
by: beginner | last post by:
Hi All, If I have a list comprehension: ab= c = "ABC" print c
0
1789
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
8750
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8662
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9244
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...
0
9111
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8961
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...
0
7853
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6585
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...
1
3114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2058
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.