在不知道我在寻找什么的情况下在字符串中查找模式的最佳方法?

Best way to find patterns in a string without knowing what I'm looking for?

我有包含不超过 16 种颜色的 500x500 位图,我需要将其转换为文本文件,其中每种颜色由一个字符表示。

然后我需要通过在每一行中查找模式来减小文本文件的大小。

我现在在二维数组中有这些字符。

例如:

AHAHAH = 3(AH)

HAHAHA = 3(HA)

AAAHHH = 3(A)3(H)

ABYZTT = ABYZ2(T)

AHAHAB = 2(AH)AB

我不认为我可以使用正则表达式,因为有太多可能的组合。

我什至不知道从哪里开始。

这是我为解决问题所做的工作。 我还没有彻底检查边缘情况,但它正在处理我的测试输入。 也许这对将来的某人有帮助。 它是 运行-Length 编码,但适用于字符组,而不是单个字符。根据我的阅读,正常的 RLE 会将 AAAAHAHA 编码为 A4H1A1H1A1,而我需要编码 4A2HA。

string='AHYAHYAHAHAHAHAHAHAHBBBBBBBTATAZAB*+I'
length=len(string)
half=round(length/2)
new_string=""
i=1
while i<=half and string:
  if i>length-i:
    pass
  sub_string1=string[:i]
  sub_string2=string[i:i+i]
  if sub_string1==sub_string2:
    match=True
    count=1
    while match is True:
        sub_string1=string[count*i:(count+1)*i]
        sub_string2=string[(count+1)*i:(count+2)*i]
        if sub_string1 == sub_string2:
          count+=1
        else:
          match=False
          new_string+="("+str(count+1)+")"+sub_string1
          string=string[count*i+i:]
          i=1
  else:  
    if i==len(string):
      new_string+=string[0]
      string=string[1:]
      i=1
    else:
      i+=1

print(new_string)
(2)AHY(7)AH(7)B(2)TAZAB*+I