gpt4 book ai didi

python - 读取 Python 中所有可能的顺序子串

转载 作者:太空狗 更新时间:2023-10-30 02:30:28 24 4
gpt4 key购买 nike

如果我有一个字母列表,比如:
word = ['W','I','N','E']
并且需要获取长度为 3 或更小的所有可能的子字符串序列,例如:
W I N E, WI N E, WI NE, W IN E, WIN E
最有效的方法是什么?

现在,我有:

word = ['W','I','N','E']
for idx,phon in enumerate(word):
phon_seq = ""
for p_len in range(3):
if idx-p_len >= 0:
phon_seq = " ".join(word[idx-(p_len):idx+1])
print(phon_seq)

这只是给了我以下内容,而不是子序列:

W
I
W I
N
I N
W I N
E
N E
I N E

我只是不知道如何创建每个可能的序列。

最佳答案

试试这个递归算法:

def segment(word):
def sub(w):
if len(w) == 0:
yield []
for i in xrange(1, min(4, len(w) + 1)):
for s in sub(w[i:]):
yield [''.join(w[:i])] + s
return list(sub(word))

# And if you want a list of strings:
def str_segment(word):
return [' '.join(w) for w in segment(word)]

输出:

>>> segment(word)
[['W', 'I', 'N', 'E'], ['W', 'I', 'NE'], ['W', 'IN', 'E'], ['W', 'INE'], ['WI', 'N', 'E'], ['WI', 'NE'], ['WIN', 'E']]

>>> str_segment(word)
['W I N E', 'W I NE', 'W IN E', 'W INE', 'WI N E', 'WI NE', 'WIN E']

关于python - 读取 Python 中所有可能的顺序子串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26791259/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com