在一个集合中生成字符串及其子字符串的所有组合 -- python
Generate all combinations of strings and their substrings in a set -- python
我想从一组字符串中获取字符串的所有组合。例如:
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'])
我不明白我做错了什么
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)
这段代码现在可以运行了,您所要做的就是将组合切换为排列。
我想从一组字符串中获取字符串的所有组合。例如:
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'])
我不明白我做错了什么
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)
这段代码现在可以运行了,您所要做的就是将组合切换为排列。