Python Forum
Example of list comprehensions doesn't work
Thread Rating:
  • 2 Vote(s) - 1.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Example of list comprehensions doesn't work
#1
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.
Reply
#2
Are you using Python 3? If so, print needs parenthesis.
Reply
#3
Did you declare z as as an int earlier in the program. If so, please post that code as well.
Reply
#4
(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
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.
Reply
#5
@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.
Reply
#6
(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
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.
Reply
#7
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: ''
Reply
#8
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.
Reply
#9
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])
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why doesn't list require global keyword? johnywhy 9 794 Jan-15-2024, 11:47 PM
Last Post: sgrey
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 929 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,320 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Beginner: Code not work when longer list raiviscoding 2 816 May-19-2023, 11:19 AM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,774 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  How to work with list kafka_trial 8 2,004 Jan-24-2023, 01:30 PM
Last Post: jefsummers
  color code doesn't work harryvl 1 883 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  convert this List Comprehensions to loop jacklee26 8 1,502 Oct-21-2022, 04:25 PM
Last Post: deanhystad
  client.get_all_tickers() Doesn't work gerald 2 1,705 Jun-16-2022, 07:59 AM
Last Post: gerald
  Are list/dict comprehensions interpreted really sequentially? anata2047 3 1,450 May-31-2022, 08:43 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020