以编程方式生成其他列表的组合列表

Programmatically generate list of combinations other lists

我想创建一个列表列表的可能组合列表(示例会更好地解释)

list=[[a,b],[c,d],[f]]

结果应该是

acf
adf
bcf
bdf

列表的长度可以变化,变量列表中列表的长度也可以变化。我将如何以编程方式制作 this/these 循环? (最好用Python或伪语言解释)

你可以递归地做:

def printCombos(arrays, combo):
  if len(arrays) == 0:
    print combo
  else:
    for i in arrays[0]:
      combo.append(i)
      printCombos(arrays[1:], combo)
      combo.pop()

l=[['a','b'],['c','d'],['f']]
printCombos(l, [])

这就是 itertools.product 的用途:

>>> lst = ['ab','cd','f']
>>> from itertools import product
>>> list(product(*lst))
[('a', 'c', 'f'), ('a', 'd', 'f'), ('b', 'c', 'f'), ('b', 'd', 'f')]
import itertools

list=[['a','b'],['c','d'],['f']]

for comb in itertools.product(*list):
  print ''.join(comb)
curlist = []

for firstobj in listoflists[0]:
    for secondobj in listoflists[1]:
        for lastobj in listoflists[2]:
            curlist.append(firstobj)
            curlist.append(secondobj)
            curlist.append(lastobj)
            print ','.join(curlist)