python - 字符串列表中的公共(public)子字符串

标签 python python-3.x algorithm substring python-3.8

我在尝试解决给定一些字符串及其长度的问题时遇到了一个问题,您需要找到它们的公共(public)子字符串。我循环遍历列表然后遍历其中每个单词的部分代码是这样的:

num_of_cases = int(input())
for i in range(1, num_of_cases+1):
    if __name__ == '__main__':
        len_of_str = list(map(int, input().split()))
        len_of_virus = int(input())

    strings = []
    def string(strings, len_of_str):
        len_of_list = len(len_of_str)
        for i in range(1, len_of_list+1):
            strings.append(input())
    
    lst_of_subs = []
    virus_index = []
    def substr(strings, len_of_virus):
        for word in strings:
             for i in range(len(len_of_str)):
                  leng = word[i:len_of_virus]
                  lst_of_subs.append(leng)
                  virus_index.append(i)

    print(string(strings, len_of_str))
    print(substr(strings, len_of_virus))

它在给定字符串的情况下打印以下内容:ananasso、associazione、tassonomia、masson

['anan', 'nan', 'an', 'n', 'asso', 'sso', 'so', 'o', 'tass', 'ass', 'ss', 's', 'mass', 'ass', 'ss', 's']

似乎结束索引没有增加,尽管我通过在循环结束时写入 len_of_virus += 1 来尝试。

示例输入:

1
8 12 10 7
4
ananasso
associazione
tassonomia
massone

第一个字母是案例数,第二行是字符串的名称,第三行是病毒的长度(公共(public)子字符串),然后是我应该循环的给定字符串。

预期输出:

Case #1: 4 0 1 1

其中四个数字是公共(public)子字符串的起始索引。(我认为打印代码不关心这个特定问题)

我该怎么办?请帮忙!!

最佳答案

除了在奇怪的地方定义函数和使用所述函数以不真正鼓励的方式产生副作用之外,问题在于:

for i in range(len(len_of_str)):
    leng = word[i:len_of_virus]

i 在每次迭代中不断增加,但 len_of_virus 保持不变,所以你正在有效地做

word[0:4] #when len_of_virus=4
word[1:4]
word[2:4]
word[3:4]
...

这就是 'anan', 'nan', 'an', 'n', 来自第一个单词“ananasso”的地方,其他单词也一样

>>> word="ananasso"
>>> len_of_virus = 4
>>> for i in range(len(word)):
        word[i:len_of_virus]

    
'anan'
'nan'
'an'
'n'
''
''
''
''
>>> 

你可以修复它,将上端移动 i,但在另一端留下同样的问题

>>> for i in range(len(word)):
    word[i:len_of_virus+i]

    
'anan'
'nana'
'anas'
'nass'
'asso'
'sso'
'so'
'o'
>>>

所以在范围和问题上做一些简单的调整:

>>> for i in range(len(word)-len_of_virus+1):
        word[i:len_of_virus+i]

    
'anan'
'nana'
'anas'
'nass'
'asso'
>>> 

子串部分搞定了,剩下的也简单

>>> def substring(text,size):
        return [text[i:i+size] for i in range(len(text)-size+1)]

>>> def find_common(lst_text,size):
        subs = [set(substring(x,size)) for x in lst_text]
        return set.intersection(*subs)

>>> test="""ananasso
associazione
tassonomia
massone""".split()
>>> find_common(test,4)
{'asso'}
>>> 

要找到列表中所有字符串的公共(public)部分,我们可以使用 set ,首先我们将给定单词的所有子串放入一个集合中,最后我们将它们全部相交。

剩下的就是根据自己的喜好打印出来

>>> virus = find_common(test,4).pop()
>>> print("case 1:",*[x.index(virus) for x in test])
case 1: 4 0 1 1
>>> 

关于python - 字符串列表中的公共(public)子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66231459/

相关文章:

python - 使用 boost::python vector_indexing_suite 包装 std::vector

python - 尝试在 Windows 上的 Python 3 中使用套接字时出现 Winsock 错误 10014

python - Matlab 到 Python - 为什么嵌套 for 循环在 python 中运行频率是两倍?

Java:如何计算程序的时间复杂度?

python - 为 "Lights out"变体生成切换矩阵

algorithm - 两个数字形状的重叠测试

python - 如何生成可重复的随机数序列?

python - 使用 txt 文件中的多个根元素创建 xml

python - 如何以渐进方式生成没有值的组合

python - 将每个子列表项与每个其他子列表中的每个项进行匹配(python)