我正在寻找一个库或一种在 Python 中实现以下内容的有效方法
Input:
"He was hungry"
Desired Output:
[["He","was","hungry"]
["He was","hungry"]
["He","was hungry"]
["He was hungry"]]
最佳答案
def f(a):
if(len(a) == 0):
yield []
for i in range(len(a)):
for c in f(a[i+1:]):
yield [" ".join(a[:i+1]), *c]
s = "He was hungry"
print(list(f(s.split())))
[['He', 'was', 'hungry'], ['He', 'was hungry'], ['He was', 'hungry'], ['He was hungry']]
关于python - 依次生成所有可能的子串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57906801/