gpt4 book ai didi

python - 与列表中的前缀和后缀单词匹配并返回包含所有链接单词的字符串

转载 作者:太空宇宙 更新时间:2023-11-03 19:50:08 32 4
gpt4 key购买 nike

我有一个单词列表,我必须匹配该列表中具有相同前缀和后缀的所有单词。我的函数将返回一个包含所有链接单词的字符串。只有一种方法可以链接所有单词

list_ = ["sequence","horse", "ermin"]

期望的输出:

"horsequencermin"

如何获得所需的输出?

最佳答案

传递您的加入名单

def join(list):
if len(list) == 1:
print(list[0])
return True;
one = list[0];
for x in range(1,len(list)):
two = list[x]
three = overlap(one, two)
if(len(three) != 0):
temp = list[:]
temp.append(three)
temp.remove(one)
temp.remove(two)
if(join(temp)):
return True;

return False


def overlap(one , two):
len1 = len(one)
len2 = len(two)

for i in range(1,min(len1,len2)+1):
subone = one[len1-i:]
subone2 = one[:i]
subtwo = two[:i]
subtwo2 = two[len2-i:]
if(subone == subtwo):
return one[:len1-i]+subone+two[i:]
if(subone2 == subtwo2):
return two[:len2-i]+subone2+one[i:]

return ""

关于python - 与列表中的前缀和后缀单词匹配并返回包含所有链接单词的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59897733/

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