我有一个 token 列表,例如:
hel
lo
bye
我想生成这些字符串的所有可能组合,例如:
hello
lohel
helbye
byehel
lobye
byelo
语言不重要,有什么建议吗?
我找到了 Generating permutations using bash ,但这会在单行上进行排列。
最佳答案
您的示例可以用 Python 编写为
from itertools import combinations
print list(combinations(["hel", "lo", "bye"], 2))
再次将输出组合成字符串:
print ["".join(a) for a in combinations(["hel", "lo", "bye"], 2)]
如果您对这个函数的实际实现感兴趣,请查看 documentation .
关于python - 从 token 列表生成所有可能的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4059550/