我正在尝试编写一个脚本,它将在每个字符串的开头或结尾找到共享5个字母重叠区域的字符串(如下例所示)。
facgakfjeakfjekfzpgghi
pgghiaewkfjaekfjkjakjfkj
kjfkjaejfaefkajewf
我正在尝试创建一个连接所有三个字符串的新字符串,因此输出结果为:
facgakfjeakfjekfzpgghiaewkfjaekfjkjakjfkjaejfaefkajewf
编辑:
这是输入:
x = ('facgakfjeakfjekfzpgghi', 'kjfkjaejfaefkajewf', 'pgghiaewkfjaekfjkjakjfkj')
**列表未订购
到目前为止我写的是*但是不正确:
def findOverlap(seq)
i = 0
while i < len(seq):
for x[i]:
#check if x[0:5] == [:5] elsewhere
x = ('facgakfjeakfjekfzpgghi', 'kjfkjaejfaefkajewf', 'pgghiaewkfjaekfjkjakjfkj')
findOverlap(x)
答案 0 :(得分:8)
创建一个字典,将每个字符串的前5个字符映射到其尾部
strings = {s[:5]: s[5:] for s in x}
和一组所有后缀:
suffixes = set(s[-5:] for s in x)
现在找到前缀与任何后缀不匹配的字符串:
prefix = next(p for p in strings if p not in suffixes)
现在我们可以跟随字符串链了:
result = [prefix]
while prefix in strings:
result.append(strings[prefix])
prefix = strings[prefix][-5:]
print "".join(result)
答案 1 :(得分:0)
循环每对候选者,反转第二个字符串并使用here
中的答案答案 2 :(得分:0)
蛮力方法 - 执行所有组合并返回匹配链接术语的第一个:
def solution(x):
from itertools import permutations
for perm in permutations(x):
linked = [perm[i][:-5] for i in range(len(perm)-1)
if perm[i][-5:]==perm[i+1][:5]]
if len(perm)-1==len(linked):
return "".join(linked)+perm[-1]
return None
x = ('facgakfjeakfjekfzpgghi', 'kjfkjaejfaefkajewf', 'pgghiaewkfjaekfjkjakjfkj')
print solution(x)