1

I need to generate all points coordinates by given dimensions, for example:

>>>points(2,2)
>>>[(0,0),(0,1),(1,0),(1,1)]
>>>points(1,1,1)
>>>[(0,0,0),(0,0,1),(0,1,0),(0,1,1),(1,1,0),(1,1,1)]

I,ve seen solution for 2-dimensions, but cant find a way to make method independent of number of dimensions:

>>> from itertools import product
>>> list(product(xrange(d) for d in (1,2,3)))
[(xrange(1),), (xrange(2),), (xrange(3),)] #where is my tuples?

where (1,2,3) are *args tuple, which can be anything.

1 Answer 1

2

You need to use a * when calling the function to use an iterable object as an argument list. f(*[1,2,3]) works like f(1,2,3).

Knowing this, and using itertools:

def points(*args):
  return list(product(*[range(n) for n in args]))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.