python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python算法字符串提取重复子串

使用Python算法实现从字符串中提取重复子串

作者:东方佑

在文本处理和数据分析中,经常需要从字符串中提取重复出现的子串,本文将解析一个高效的Python算法,用于从给定字符串中提取长度超过3的重复子串,需要的朋友可以参考下

算法概述

该算法包含两个核心函数:

1. 预处理函数:replace_text()

def replace_text(copy_text):
    replace_char = ""
    # 统计字符频率
    for char, count in Counter(list(copy_text)).items():
        if count == 1:  # 只出现一次的字符
            if replace_char == "":
                replace_char = char  # 选择第一个低频字符作为替换字符
            else:
                # 用替换字符替换其他低频字符
                copy_text = copy_text.replace(char, replace_char)
    
    if replace_char != "":
        # 分割字符串并筛选长度>3的子串
        return [sub for sub, _ in Counter(copy_text.split(replace_char)).items() 
                if len(sub) > 3]
    else:
        return [copy_text]  # 没有低频字符时返回整个字符串

功能说明

2. 主处理函数:compute_sub_string_list()

def compute_sub_string_list(text1):
    text_list = replace_text(text1)  # 预处理
    new_text_list = []

    for one_text in text_list:
        if len(one_text) == 4:
            # 处理长度为4的子串
            if one_text in new_text_list:
                continue
            if text1.count(one_text) > 1:
                new_text_list.append(one_text)
        else:
            # 处理长度>4的子串
            max_count = 0
            max_str = ""
            while len(one_text) > 4:
                sub_str = one_text[:3]
                up_str_count = 0
                up_str = ""
                
                # 扩展子串并检查重复性
                for char in one_text[3:]:
                    sub_str += char
                    if sub_str in new_text_list:
                        continue
                    str_count = text1.count(sub_str)
                    if str_count > 1:
                        if up_str_count <= str_count:
                            up_str_count = str_count
                            up_str = sub_str
                    else:
                        break  # 停止扩展
                
                # 更新最佳子串
                if up_str:
                    if up_str_count > max_count:
                        max_count = up_str_count
                        max_str = up_str
                
                # 滑动窗口
                one_text = one_text[1:]
            
            if max_str:
                new_text_list.append(max_str)
    
    return new_text_list

功能说明

算法优势

  1. 高效预处理:通过替换低频字符优化后续处理
  2. 智能子串扩展:动态扩展子串直到不再重复
  3. 滑动窗口 技术:高效遍历所有可能子串
  4. 频率优先:优先选择出现次数最多的子串

使用示例

if __name__ == '__main__':
    text = "abracadabraabracadabra"
    result = compute_sub_string_list(text)
    print("重复子串:", result)
    # 输出: ['abra', 'racad', 'acada', 'cadab', 'adabr']

应用场景

这个算法通过巧妙的预处理和滑动窗口 技术,高效地从字符串中提取有意义的重复模式,特别适合处理包含重复模式的长文本数据。

def replace_text(copy_text):
    replace_text = ""
    for i in Counter(list(copy_text)).items():
        if i[1] == 1:
            if replace_text == "":
                replace_text = i[0]
            else:
                copy_text = copy_text.replace(i[0], replace_text)
    if replace_text != "":

        return [i[0] for i in Counter(copy_text.split(replace_text)).items() if len(i[0]) > 3]
    else:
        return [copy_text]


def compute_sub_string_list(text1):
    copy_text = text1
    text_list = replace_text(copy_text)

    new_text_list = []

    for one_text in text_list:
        if len(one_text) == 4:
            if one_text in new_text_list:
                continue
            if text1.count(one_text) > 1:
                new_text_list.append(one_text)
        else:
            max_count = 0
            max_str = ""
            while True:

                sub_str = one_text[:3]
                up_str_count = 0
                up_str = ""

                for s in one_text[3:]:
                    sub_str += s
                    if sub_str in new_text_list:
                        continue
                    str_count = text1.count(sub_str)
                    if str_count > 1:
                        if up_str_count <= str_count:
                            up_str_count = str_count
                            up_str = sub_str
                    else:
                        break
                if up_str:
                    if up_str_count > max_count:
                        max_count = up_str_count
                        max_str = up_str
                if len(one_text) > 4:

                    one_text = one_text[1:]
                else:
                    break
            new_text_list.append(max_str)
    return new_text_list


if __name__ == '__main__':
    print()

以上就是使用Python算法实现从字符串中提取重复子串的详细内容,更多关于Python算法字符串提取重复子串的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文