我有一个字符串列表的列表
[['a','b'],['c','d'],['e','f']]
我想获得所有可能的组合,以使结果为
[['a','b'],['c','d'],['e','f'],
['a','b','c','d'],['a','b','e','f'],['c','d','e','f'],
['a','b','c','d','e','f']]
到目前为止,我已经想出了这段代码片段。
input = [['a','b'],['c','d'],['e','f']]
combs = []
for i in xrange(1, len(input)+1):
els = [x for x in itertools.combinations(input, i)]
combs.extend(els)
print combs
大部分参考了这篇帖子中的答案。
但是结果是:
[(['a','b'],),(['c','d'],),(['e','f'],),
(['a','b'],['c','d']),(['a','b'],['e','f']),(['c','d'],['e','f']),
(['a','b'],['c', 'd'],['e', 'f'])]
我目前遇到了难题,正在尝试寻找一种优雅、Pythonic的方法来解包这些元组。
itertools
的强大。 - John Coleman