473,387 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Nested list comprehensions

Hey guys:
[(i,j,k) for i in range(1,j) for j in range(1,k) for k in range(1,5)] [(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 2, 1), (1, 2, 2), (1,
2, 3), (1, 2, 4), (1, 3, 1), (1, 3, 2), (1, 3, 3), (1, 3, 4), (2, 1,
1), (2, 1, 2), (2, 1, 3), (2, 1, 4), (2, 2, 1), (2, 2, 2), (2, 2, 3),
(2, 2, 4), (2, 3, 1), (2, 3, 2), (2, 3, 3), (2, 3, 4)] def a(): .... print [(j,k) for j in range(1,k) for k in range(1,5)]
.... a()

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in a
UnboundLocalError: local variable 'k' referenced before assignment

Why is it that I can execute the nested list comprehension in the
intepreter
but if the same line is within a method declaration I get an unbound
reference error?

Is this a bug or am I missing some deep rule?

Regards, Neil Dunn

Nov 27 '05 #1
2 4180
ne******@gmail.com wrote:
Hey guys:
[(i,j,k) for i in range(1,j) for j in range(1,k) for k in range(1,5)] [(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 2, 1), (1, 2, 2), (1,
2, 3), (1, 2, 4), (1, 3, 1), (1, 3, 2), (1, 3, 3), (1, 3, 4), (2, 1,
1), (2, 1, 2), (2, 1, 3), (2, 1, 4), (2, 2, 1), (2, 2, 2), (2, 2, 3),
(2, 2, 4), (2, 3, 1), (2, 3, 2), (2, 3, 3), (2, 3, 4)] def a(): ... print [(j,k) for j in range(1,k) for k in range(1,5)]
... a() Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in a
UnboundLocalError: local variable 'k' referenced before assignment

Why is it that I can execute the nested list comprehension in the
intepreter
but if the same line is within a method declaration I get an unbound
reference error?

Is this a bug or am I missing some deep rule?

Regards, Neil Dunn

You may not be getting the name error in the interpreter because you already
defined those variables. Try this:

$ python
[(j,k) for j in range(1,k) for k in range(1,5)] Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'k' is not defined


Looks like the variable was not defined here, either.

You are trying to use the variable "k" in the range() function before it has
been bound to anything, hence the error.
Nov 27 '05 #2
ne******@gmail.com writes:
Hey guys:
[(i,j,k) for i in range(1,j) for j in range(1,k) for k in range(1,5)] [(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 2, 1), (1, 2, 2), (1,
2, 3), (1, 2, 4), (1, 3, 1), (1, 3, 2), (1, 3, 3), (1, 3, 4), (2, 1,
1), (2, 1, 2), (2, 1, 3), (2, 1, 4), (2, 2, 1), (2, 2, 2), (2, 2, 3),
(2, 2, 4), (2, 3, 1), (2, 3, 2), (2, 3, 3), (2, 3, 4)]


Give this a careful look: i varies slowest, and takes on the values 1
and 2. j varies next slowest, and takes on the values range(1, 4) in
both the i loops. k varies fastest, and goes through range(1, 5) in
all loops. I don't think it's doing what you intend.
def a(): ... print [(j,k) for j in range(1,k) for k in range(1,5)]
... a() Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in a
UnboundLocalError: local variable 'k' referenced before assignment

Why is it that I can execute the nested list comprehension in the
intepreter
but if the same line is within a method declaration I get an unbound
reference error?

Is this a bug or am I missing some deep rule?


What are the values of the global variables j and k before you enter
the loop? Wouldn't happen to be 2 and 4, would they? Deleting them
first gives different results:
del j
del k
[(i,j,k) for i in range(1,j) for j in range(1,k) for k in range(1,5)]

Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'j' is not defined

Methinks you want your for loops in the opposite order.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Nov 27 '05 #3

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

Similar topics

9
by: T. Earle | last post by:
To list, I'm trying to figure out the best approach to the following problem: I have four variables: 1) headlines 2) times 3) states 4) zones
2
by: Elaine Jackson | last post by:
List comprehensions don't work the way you intuitively expect them to work. I realize many people have no intuitions about how list comprehensions 'should' work, so if you recognize yourself in...
25
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
24
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...
9
by: Neuruss | last post by:
I have a doubt regarding list comprehensions: According to Mark Lutz in his book Learning Pyhon: "...there is currently a substantial performance advantage to the extra complexity in this case:...
23
by: Mike Meyer | last post by:
Ok, we've added list comprehensions to the language, and seen that they were good. We've added generator expressions to the language, and seen that they were good as well. I'm left a bit...
15
by: Xah Lee | last post by:
Here's the belated Java solution. import java.util.List; import java.util.ArrayList; import java.lang.Math; class math { public static List range(double n) { return range(1,n,1); }
30
by: Steven Bethard | last post by:
George Sakkis wrote: > "Steven Bethard" <steven.bethard@gmail.com> wrote: >> Dict comprehensions were recently rejected: >> http://www.python.org/peps/pep-0274.html >> The reason, of course,...
16
by: koutoo | last post by:
I start my code with some constants then a while statement. But I have some For statements towards the end within the While statement where I start getting some errors. I'm hoping I won't have to...
13
by: Fredrik Lundh | last post by:
Patrol Sun wrote: so why exactly are you trying to nest 20 or 100 for-in loops? </F>
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...

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.