1

我想合并成 2 个句子,例如:

sent1 = 'abcdefghiklmn'
sent2 = 'ziklmopqrst'

2个句子有相同的iklm

result = 'abcdefghiklmnopqrst'

非常感谢!

4

3 回答 3

2

也许这可以帮助

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)
于 2019-10-01T08:46:19.873 回答
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:]
于 2019-10-01T08:27:14.390 回答
0

这可能有效:

list(set(list("abc")+list("adef")))

输出是:

['a', 'c', 'b', 'e', 'd', 'f']

并将其转换为单个字符串:

"".join(['a', 'c', 'b', 'e', 'd', 'f'])

输出是:

'acbedf'
于 2019-10-01T10:45:19.400 回答