I have a string of letters that I'd like to split into all possible combinations (the order of letters must be remain fixed), so that:
s = 'monkey'
becomes:
combinations = [['m', 'onkey'], ['mo', 'nkey'], ['m', 'o', 'nkey'] ... etc]
Any ideas?
解决方案def splitter(str):
for i in range(1, len(str)):
start = str[0:i]
end = str[i:]
yield (start, end)
for split in splitter(end):
result = [start]
result.extend(split)
yield result
combinations = list(splitter(str))
Note that I defaulted to a generator to save you from running out of memory with long strings.