用于查找两个字符串中未提供正确输出的所有公共子字符串的函数

时间:2017-08-15 22:36:10

标签: python string substring

我正在使用以下函数来查找两个字符串之间的所有常见子字符串:

def substringFinder(string1, string2):
    answer = ""
    anslist=[]
    len1, len2 = len(string1), len(string2)
    for i in range(len1):
        match = ""
        for j in range(len2):
            if (i + j < len1 and string1[i + j] == string2[j]):
                match += string2[j]
                j=j+1
            else:
                #if (len(match) > len(answer)): 
                answer = match
                if answer != '':
                    anslist.append(answer)
                match = ""

        if match != '':
            anslist.append(match)
        break
    print(anslist)

所以当我substringFinder("ALISSA", "ALYSSA")给出['AL', 'SSA']时,这很好。但是当我执行substringFinder("AHAMMAD", "AHAMAD")时,它只提供输出['AHAM'],但我希望['AHAM', 'MAD']作为输出。怎么做到的?

3 个答案:

答案 0 :(得分:0)

  1. 不要break
  2. 在添加字符串之前检查字符串的长度,以避免像"A"
  3. 这样的结果
  4. return函数结果,而不是在函数内打印
  5. 喜欢这样:

    def substringFinder(string1, string2):
        answer = ""
        anslist=[]
        len1, len2 = len(string1), len(string2)
        for i in range(len1):
            match = ""
            for j in range(len2):
                if (i + j < len1 and string1[i + j] == string2[j]):
                    match += string2[j]
                else:
                    #if (len(match) > len(answer)): 
                    answer = match
                    if answer != '' and len(answer) > 1:
                        anslist.append(answer)
                    match = ""
    
            if match != '':
                anslist.append(match)
            # break
        return anslist
    
    print substringFinder("AHAMMAD", "AHAMAD")
    

    <强>结果: ['AHAM', 'MAD']

答案 1 :(得分:0)

这是一个直截了当的蛮力解决方案:

In [7]: def substring_finder(s1, s2):
   ...:     matches = []
   ...:     size = len(s1)
   ...:     for i in range(2, size):
   ...:         for j in range(0, size, i):
   ...:             stop = j+i
   ...:             if stop > size:
   ...:                 continue
   ...:             sub = s1[j:stop]
   ...:             if sub in s2:
   ...:                 matches.append(sub)
   ...:     return matches
   ...:

In [8]: substring_finder("ALISSA", "ALYSSA")
Out[8]: ['AL', 'SA', 'SSA']

In [9]: substring_finder("AHAMMAD", "AHAMAD")
Out[9]: ['AH', 'AM', 'MA', 'AHA', 'AHAM']

答案 2 :(得分:0)

你可以试试这个:

def substrings(s1, s2):
    final = [s1[i:b+1] for i in range(len(s1)) for b in range(len(s1))]


    return [i for i in final if i in s1 and i in s2 and len(i) > 1]

s1, s2 = "ALISSA", "ALYSSA"


print(substrings(s1, s2))

输出:

['AL', 'SS', 'SSA', 'SA']