python - What is the pythonic way of generating this type of list? (Faces of an n-cube) -
if n == 1: return [(-1,), (1,)] if n == 2: return [(-1,0), (1,0), (0,-1), (0,1)] if n == 3: return [(-1,0,0), (1,0,0), (0,-1,0), (0,1,0), (0,0,-1), (0,0,1)]
basically, return list of 2n
tuples conforming above specification. above code works fine purposes i'd see function works n ∈ ℕ (just edification). including tuple([0]*n)
in answer acceptable me.
i'm using generate direction of faces measure polytope. directions, can use list(itertools.product(*[(0, -1, 1)]*n))
, can't come quite concise face directions.
def faces(n): def iter_faces(): f = [0] * n in range(n): x in (-1, 1): f[i] = x yield tuple(f) f[i] = 0 return list(iter_faces())
>>> faces(1) [(-1,), (1,)] >>> faces(2) [(-1, 0), (1, 0), (0, -1), (0, 1)] >>> faces(3) [(-1, 0, 0), (1, 0, 0), (0, -1, 0), (0, 1, 0), (0, 0, -1), (0, 0, 1)]
Comments
Post a Comment