Problem on List comprehension

python

#1

Hi,

I am absolutely new in Python and just stuck with this below problem.
X,Y and Z are the three coordinates of a cuboid. Now X=1,Y=1 , Z=1 and N=2.
I have to print a list of all possible coordinates on a 3D grid where the sum of Xi + Yi + Zi is not equal to N. If X=2, the possible values of Xi can be 0, 1 and 2. The same applies to Y and Z.

results = []
sets = []

for x in range(X):
    for y in range(Y):
        for z in range(Z):
            if(int(x+y+z) != N):
                 sets.append(x)
                 sets.append(y)
                  sets.append(z)
             results.append(sets)

Could any one please help me out with the proper way of doing it using list comprehension?


#2
  1. Why do you want to use list comprehension?
  2. And I think this:

    if(int(x+y+z) != N):
        sets.append(x)
        sets.append(y)
        sets.append(z)
    results.append(sets)

can much simply be:

if x + y + z != N:
    results.append([x, y, z])

P.S. Since this is not CC related problem, i am re-categorizing this post.