如何在Python中组合列表和列表
我不知道是称之为组合还是置换,所以这个问题可以根据您的评论进行编辑 我的清单如下:如何在Python中组合列表和列表,python,list,python-3.x,combinations,python-3.5,Python,List,Python 3.x,Combinations,Python 3.5,我不知道是称之为组合还是置换,所以这个问题可以根据您的评论进行编辑 我的清单如下: [ ["a"], ["b", "c"], ["d", "e", "f"] ] 我希望将其输出为: [ "abd", "acd", "abe", "ace", "abf", "acf" ] 我的首要任务是使用内置工具或手工制作,而不是使用其他科学模块。但是,如果没有办法,可以使用科学模块 环境 python 3.5.1 根据评论建议,您
[
["a"],
["b", "c"],
["d", "e", "f"]
]
我希望将其输出为:
[
"abd",
"acd",
"abe",
"ace",
"abf",
"acf"
]
我的首要任务是使用内置工具或手工制作,而不是使用其他科学模块。但是,如果没有办法,可以使用科学模块
环境
- python 3.5.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)
你试过了吗?谢谢你的提醒。你可以这么做,但是为什么要重新发明轮子呢?这是一个典型的
itertools.product
问题。