Posts: 2,125
Threads: 11
Joined: May 2017
May-14-2018, 09:03 AM
(This post was last modified: May-14-2018, 09:03 AM by DeaD_EyE.)
You get a
Error: UnboundLocalError: local variable 'z' referenced before assignment
The cause is, you're referencing z before it has been assigned.
In this case it doesn't matter, that the name z is global.
In the context of the list comprehension the name z is a local variable, because it's assigned.
You may ask now, where the assignment happens:
for z in range( z+1)
The red one, is the assignment of local variable z. The green is read access to the variable.
Just change the z into z_ and the code works.
print([[x,y,z_] for i in range(x+1) for j in range(y+1) for z_ in range(z+1)])
Posts: 404
Threads: 94
Joined: Dec 2017
May-14-2018, 08:52 PM
(This post was last modified: May-14-2018, 08:54 PM by Truman.)
@ micseydel
At first I used nested loops but I read somewhere that that using nested loops in Python is considered a garbage code
@ DeaD_EyE
Thank you, I completely missed that. print([[x,y,z] for i in range(x+1) for j in range(y+1) for k in range(z+1) if i+j+k != n]) works fine.
p.s. checking again it doesn't work as I expected.
The result is: Output: [[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]]
the input was: 1 1 1 2
Posts: 2,342
Threads: 62
Joined: Sep 2016
(May-14-2018, 08:52 PM)Truman Wrote: At first I used nested loops but I read somewhere that that using nested loops in Python is considered a garbage code That doesn't make any sense to me. I'm a professional software engineer and if I saw that complicated of a comprehension during code review, I'd have them break it up. I had a Google interviewer suggest I not use a comprehension for something that would have been less complicated than that.
Comprehensions are an intermediate feature so there's some prestige associated with them I suppose, but you should use the right tool for the right job, and code at your level - as I said I don't think you'd have this problem with a plain old nested loop, so I suggest you stick to simpler concepts until you have more mastery. Even if you prefer to keep using comprehensions, I'd suggest in situations like this where you're struggling to debug the comprehension, break it up anyway. If you need to break it up to debug it, it probably should be broken up, but personally even if I want it to ultimately be a comprehension I'll simplify the code for debugging.
Posts: 404
Threads: 94
Joined: Dec 2017
May-17-2018, 07:56 PM
(This post was last modified: May-17-2018, 07:56 PM by Truman.)
Thank you.
I would appreciate anyone's opinion on why my previous line of code doesn't work. It doesn't look complicated but still it doesn't give me the output that I want. Comprehension with x and y works fine, but the one with x,y and z doesn't although it's the same logic.
Posts: 2,953
Threads: 48
Joined: Sep 2016
What you are doing is to sacrifice the readability of the code for some desire to be pythonic. Pythonic means just that, the right tool in the right place. As @ micseydel mentioned it already.
One reason why Python has this popularity is that it's easy to read. And to write. You are struggling to write one piece of code. It should not be that way. If it's hard for you to write it, it will be hard for someone else to read it.
Posts: 8,158
Threads: 160
Joined: Sep 2016
May-18-2018, 08:17 AM
(This post was last modified: May-18-2018, 08:17 AM by buran.)
(May-14-2018, 08:52 PM)Truman Wrote: print([[x,y,z] for i in range(x+1) for j in range(y+1) for k in range(z+1) if i+j+k != n]) (May-17-2018, 07:56 PM)Truman Wrote: I would appreciate anyone's opinion on why my previous line of code doesn't work. It doesn't look complicated but still it doesn't give me the output that I want. well, that's the correct output.
Probably you were thinking of writing
print([[i,j,k] for i in range(x+1) for j in range(y+1) for k in range(z+1) if i+j+k != n])
Posts: 566
Threads: 10
Joined: Apr 2017
May-19-2018, 10:02 PM
(This post was last modified: May-19-2018, 10:03 PM by volcano63.)
(May-18-2018, 08:17 AM)buran Wrote: print([[i,j,k] for i in range(x+1) for j in range(y+1) for k in range(z+1) if i+j+k != n])
Being an extremely lazy person  , I would have written that as
[coords for coords in itertools.product(range(x+1), range(y+1), range(z+1))
if sum(coords) != n] I know that it is actually longer - but I really hate one-letter variables  ( x , y and z I can forgive  )
Test everything in a Python shell (iPython, Azure Notebook, etc.) - Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.
Posts: 8,158
Threads: 160
Joined: Sep 2016
I fully agree regarding one-letter variable names :-)
|