我不知道是否将其称为组合或排列,因此可以针对您的评论修改问题。
我有一个列表如下:
[
["a"],
["b", "c"],
["d", "e", "f"]
]
我想将其输出为:
[
"abd",
"acd",
"abe",
"ace",
"abf",
"acf"
]
我的首要任务是使用内置工具或手工制作,而不是使用其他科学模块。但是,如果没有办法,可以使用科学模块。
答案 0 :(得分:1)
根据评论的建议,您可以使用itertools.product
。或者你可以实现一个简单的递归方法:
def combine(lists, index=0, combination=""):
if index == len(lists):
print combination
return
for i in lists[index]:
combine(lists, index+1, combination + i)
lists = [
["a"],
["b", "c"],
["d", "e", "f"]
]
combine(lists)