Python Forum

Full Version: Example of list comprehensions doesn't work
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
x = int(input())
y = int(input())
n = int(input())
print([i,j] for i in range(x+1) for j in range(y+1) if (((i+j) !=n)))
The goal is to print all possible coordinates.
I receive a generator object message. Don't understand why.

and when I change print line to
print[[x,y,z] [b]for [/b]i in range(x+1) for j in range(y+1) for z in range(z+1) if ((x+y+z) !=n )]
it gives "SyntaxError: invalid syntax" message pointing to bolded for.
Are you using Python 3? If so, print needs parenthesis.
Did you declare z as as an int earlier in the program. If so, please post that code as well.
(May-10-2018, 11:19 PM)Truman Wrote: [ -> ]
x = int(input())
y = int(input())
n = int(input())
print([i,j] for i in range(x+1) for j in range(y+1) if (((i+j) !=n)))
The goal is to print all possible coordinates.
I receive a generator object message. Don't understand why.

Add another set of angular brackets in a print statement - within and next to outermost brackets.

And
(((i+j) !=n))
is extremely ugly and un-Pythonic.

i + j != n
is the proper Pythonic form. I sentence you to read PEP-8 Idea
@micseydel
yes, I use P3
@woooee
In pasting code I mixed 2D with 3D program, sorry for that. I was referring to x,y program that doesn't work.
@volcano63
It may sound stupid but I've never heard for term "angular brackets". The closest that google gives are angle brackets <>. Do you mean square brackets?
And thank you for PEP-8 link, will start reading it immediately.
(May-11-2018, 09:30 PM)Truman Wrote: [ -> ]Do you mean square brackets?

My bad, exactly. Brackets is not an issue I often discuss in English Blush
x = int(input())
y = int(input())
n = int(input())
print([i,j] for i in range(x+1) for j in range(y+1) if i+j != n)
I just don't understand what's wrong with this code. :(

Finally got it
x = int(input())
y = int(input())
n = int(input())
print([[i,j] for i in range(x+1) for j in range(y+1) if i+j != n])
Do we always use [] for list comprehensions?

Now, doing a 3D version:
x = int(input())
y = int(input())
z = int(input())
n = int(input())
print([[x,y,z] for i in range(x+1) for j in range(y+1) for z in range(z+1) if i+j+z != n ])
I receive a ValueError: invalid literal for int() with base 10: ''
That will happen if you enter an empty line instead of an int.

Also when you get an error message please make sure to include your full stacktrace, not just one line from it.
Error:
Traceback (most recent call last): File "C:\Python36\kodovi\koordinate.py", line 10, in <module> print([[x,y,z] for i in range(x+1) for j in range(y+1) for z in range(z+1) if i+j+z != n]) File "C:\Python36\kodovi\koordinate.py", line 10, in <listcomp> print([[x,y,z] for i in range(x+1) for j in range(y+1) for z in range(z+1) if i+j+z != n]) UnboundLocalError: local variable 'z' referenced before assignment
This is what I receive for code:
x = int(input())
y = int(input())
z = int(input())
n = int(input())
print([[x,y,z] for i in range(x+1) for j in range(y+1) for z in range(z+1)  if i+j+z != n])
That's a weird error to be getting, but you can solve it by not re-using variable names (you reuse z here). Take your list comprehension and write a nested loop instead. Only after you've gotten that working should you try putting it back into the more advanced form.
Pages: 1 2