我有一个字符串列表,其中包含以下关键字:
list1 = ["ABC", "DEF", "GHI"]
我想创建一个列表,将这些列表随机合并在一起,如:
list2= ["ABC" , "DEF" , "GHI" , "ABCDEF", "ABCGHI", ...]
更多信息: 知道itertools.combination模块的工作如下:
from itertools import combinations
list = ["ABC", "DEF" , "GHI"]
["".join(a) for a in combinations(list, 2)]
会做如下组合:
[" ABCDEF"," ABCGHI"," DEFGHI"]
但我想要所有可能的答案:
可以这样做:
["".join(a) for a in combinations(list, 1)]
["".join(a) for a in combinations(list, 2)]
["".join(a) for a in combinations(list, 3)]
将打印:
['ABC', 'DEF', 'GHI']
['ABCDEF', 'ABCGHI', 'DEFGHI']
['ABCDEFGHI']