我想从一组字符串中获取字符串的所有组合。例如:
permut = set()
permut.add("D")
permut.add("C")
def getAllKombos(stuff):
returnvalue = set()
for L in range(0, len(stuff) + 1):
for subset in itertools.combinations(stuff, L):
for i in subset:
x = x + (str(i))
returnvalue.add(x)
x = ""
return returnvalue
print getAllKombos(permut)
我的输出是:
set(['C', 'D', 'CD'])
但我需要
set(['C', 'D', 'CD', 'DC'])
我不知道我做错了什么
答案 0 :(得分:2)
import itertools
permut = set()
permut.add("D")
permut.add("C")
def getAllKombos(stuff):
returnvalue = set()
for L in range(0, len(stuff) + 1):
for subset in itertools.permutations(stuff, L):
for i in subset:
x = x + (str(i))
returnvalue.add(x)
x = ""
return returnvalue
print getAllKombos(permut)
此代码现在可以使用,您所要做的就是将组合切换为置换。