使用公共子字符串连接两个字符串?

时间:2017-10-21 07:07:44

标签: python string

说我有字符串,

string1 = 'Hello how are you'
string2 = 'are you doing now?'

结果应该是

Hello how are you doing now?

我正在考虑使用re和字符串搜索的不同方式。 (Longest common substring problem

但是有没有简单的方法(或库)在python中执行此操作?

为了清楚起见,我将再添加一组测试字符串!

string1 = 'This is a nice ACADEMY'
string2 = 'DEMY you know!'

结果将是!,

'This is a nice ACADEMY you know!'

5 个答案:

答案 0 :(得分:2)

这应该做:

$alphNm = array
(
array(["tag" => "A1B2C"],["tag" => "3D4E5"],["tag" => "F6G7H"],["tag" => "8I9J1"],["tag" => "0K11L"]),
array(["tag" => "12M13"],["tag" => "N14O1"],["tag" => "5P16Q"],["tag" => "17R18"],["tag" => "S19T2"]),
array(["tag" => "0U21V"],["tag" => "22W23"],["tag" => "X24Y2"],["tag" => "5Z26A"],["tag" => "0B2C3"]),
array(["tag" => "D4E5F"],["tag" => "6G7H8"],["tag" => "I9J10"],["tag" => "K11L1"],["tag" => "2M13N"]),
array(["tag" => "14O15"],["tag" => "P16Q1"],["tag" => "7R18S"],["tag" => "19T20"],["tag" => "U21V1"])

);


foreach ($alphNm as $alphmkey => $alphvalue) {
    foreach ($alphvalue as $alphtagkey => $alphtagvalues) {
        echo implode("", $alphtagvalues);
    }
}

输出:

string1 = 'Hello how are you'
string2 = 'are you doing now?'
i = 0
while not string2.startswith(string1[i:]):
    i += 1

sFinal = string1[:i] + string2

或者,使其成为一个功能,以便您可以在不重写的情况下再次使用它:

>>> sFinal
'Hello how are you doing now?'

输出:

def merge(s1, s2):
    i = 0
    while not s2.startswith(s1[i:]):
        i += 1
    return s1[:i] + s2

答案 1 :(得分:1)

这应该做你想要的:

def overlap_concat(s1, s2):
    l = min(len(s1), len(s2))
    for i in range(l, 0, -1):
        if s1.endswith(s2[:i]):
            return s1 + s2[i:]
    return s1 + s2

示例:

>>> overlap_concat("Hello how are you", "are you doing now?")
'Hello how are you doing now?'
>>> 

>>> overlap_concat("This is a nice ACADEMY", "DEMY you know!")
'This is a nice ACADEMY you know!'
>>> 

答案 2 :(得分:1)

使用str.endswithenumerate

def overlap(string1, string2):
    for i, s in enumerate(string2, 1):
         if string1.endswith(string2[:i]):
            break

    return string1 + string2[i:]
>>> overlap("Hello how are you", "are you doing now?")
'Hello how are you doing now?'

>>> overlap("This is a nice ACADEMY", "DEMY you know!")
'This is a nice ACADEMY you know!'

如果您要考虑尾随特殊字符,您可能希望使用一些基于re的替换。

import re
string1 = re.sub('[^\w\s]', '', string1)

虽然请注意这会删除第一个字符串中的所有特殊字符。

对上述函数的修改将找到最长的匹配子字符串(而不是最短的),包括反向遍历string2

def overlap(string1, string2):
   for i in range(len(s)):
      if string1.endswith(string2[:len(string2) - i]):
          break

   return string1 + string2[len(string2) - i:]
>>> overlap('Where did', 'did you go?') 
'Where did you go?'

答案 3 :(得分:0)

其他答案都很棒,但是这个输入确实失败了。

string1 = 'THE ACADEMY has'
string2= '.CADEMY has taken'

输出:

>>> merge(string1,string2)
'THE ACADEMY has.CADEMY has taken'
>>> overlap(string1,string2)
'THE ACADEMY has'

然而,这个标准库difflib在我的案例中证明是有效的!

match = SequenceMatcher(None, string1,\
                        string2).find_longest_match\
                        (0, len(string1), 0, len(string2))

print(match)  # -> Match(a=0, b=15, size=9)
print(string1[: match.a + match.size]+string2[match.b + match.size:]) 

输出:

Match(a=5, b=1, size=10)
THE ACADEMY has taken

答案 4 :(得分:0)

要替换的单词出现在第二个字符串中,因此您可以尝试以下内容:

new_string=[string2.split()]
new=[]
new1=[j for item in new_string for j in item if j not in string1]
new1.insert(0,string1)
print(" ".join(new1))

第一个测试用例:

string1 = 'Hello how are you'
string2 = 'are you doing now?'

输出:

Hello how are you doing now?

第二个测试案例:

string1 = 'This is a nice ACADEMY'
string2 = 'DEMY you know!'

输出:

This is a nice ACADEMY you know!
  

说明:

首先,我们分割第二个字符串,以便找到我们必须删除或替换的字词:

new_string=[string2.split()]

第二步我们将使用string1检查此拆分器字符串的每个单词,如果该字符串中的任何单词只选择第一个字符串单词,则将该单词留在第二个字符串中:

new1=[j for item in new_string for j in item if j not in string1]

此列表理解与:

相同
new1=[]
for item in new_string:
    for j in item:
        if j not in string1:
            new1.append(j)

最后一步结合字符串并加入列表:

new1.insert(0,string1)
print(" ".join(new1))
相关问题