-3

Can anyone explain to me how another way to write ans. I don't know how it works written in one line.

X = int(raw_input())
Y = int(raw_input())
Z = int(raw_input())
N = int(raw_input())
ans = [[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]
print ans
7
  • 2
    look up list comprehensions Commented Apr 7, 2022 at 18:19
  • @PedjaMirkovic Which part don't you understand? Commented Apr 7, 2022 at 18:20
  • You want is in one line? print([[i, j, k] for i in range(int(input()) + 1) for j in range(int(input() + 1) for k in range(int(input() + 1) if i + j + k != int(input()])? (translated into python 3) Commented Apr 7, 2022 at 18:22
  • @Sören ans = [[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] how to write another way, normally Commented Apr 7, 2022 at 18:23
  • nested list comprehensions like this are never very readable IMO Commented Apr 7, 2022 at 18:23

1 Answer 1

0

I think this is what you meant:

X = int(raw_input())
Y = int(raw_input())
Z = int(raw_input())
N = int(raw_input())
ans = []

for i in range(X + 1):
    for j in range(Y + 1):
        for k in range(Z + 1):
            if i + j + k != N:
                ans.append([i, j, k])
print ans

Please correct me if I'm wrong

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.