這里不需要遞歸。您可以將itertools.product與['Left', 'Right']的多個副本一起使用,例如: from itertools import productoptions = ['Left', 'Right']N = 3print(list(product(*(options,)*N))) gives: [('Left', 'Left', 'Left'), ('Left', 'Left', 'Right'), ('Left', 'Right', 'Left'), ('Left', 'Right', 'Right'), ('Right', 'Left', 'Left'), ('Right', 'Left', 'Right'), ('Right', 'Right', 'Left'), ('Right', 'Right', 'Right')] 如果愿意,可以將內部元素轉換為列表而不是元組: print([list(t) for t in (product(*(options,)*N))]) gives: [['Left', 'Left', 'Left'], ['Left', 'Left', 'Right'], ['Left', 'Right', 'Left'], ['Left', 'Right', 'Right'], ['Right', 'Left', 'Left