我想合并为两个句子,例如:
sent1 = 'abcdefghiklmn'
sent2 = 'ziklmopqrst'
2个句子具有相同的 iklm
result = 'abcdefghiklmnopqrst'
非常感谢!
答案 0 :(得分:1)
也许这可以帮助
sent1 = 'abcdefghiklmn'
sent2 = 'ziklmnopqrst'
for i in sent1:
n = 0
for f in sent2:
n += 1
if i == f:
result = sent1 + sent2[n:]
break
print(result)
答案 1 :(得分:0)
difflib.SequenceMatcher
派上用场了:
from difflib import SequenceMatcher
match = SequenceMatcher(None, sent1, sent2).find_longest_match(0, len(sent1), 0, len(sent2))
result = sent1[:match.a]+sent2[match.b:]
答案 2 :(得分:0)
这可能有效:
list(set(list("abc")+list("adef")))
输出为:
['a', 'c', 'b', 'e', 'd', 'f']
并将其转换为单个字符串:
"".join(['a', 'c', 'b', 'e', 'd', 'f'])
输出为:
'acbedf'