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

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -