【问题标题】:Best way to find patterns in a string without knowing what I'm looking for?在不知道我在寻找什么的情况下在字符串中查找模式的最佳方法?
【发布时间】:2021-06-14 02:16:37
【问题描述】:

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

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

我现在有一个二维数组中的字符。

例如:

AHAHAH = 3(AH)

HAHAHA = 3(HA)

AAAHHH = 3(A)3(H)

ABYZTT = ABYZ2(T)

AHAHAB = 2(AH)AB

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

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

【问题讨论】:

  • 这能回答你的问题吗? Python string pattern recognition/compression
  • 谢谢@takendarkk 我现在正在阅读它,看看我是否可以调整其中的一些内容
  • 您需要自己想出一个压缩算法吗?否则你可以使用 zlib 模块,例如compressed = zlip.compress(yourString.encode())
  • @AlainT。输出需要是上面精确格式的另一个 .txt,带有数字、括号和字符。它将被我没有回旋余地的古老制造机器读取。 “新”机器在 Windows '95 上运行
  • 我明白了。您应该为压缩/RLE 算法提供精确的规范(或参考)。

标签: python string string-matching


【解决方案1】:

这是我为解决我的问题所做的。 我还没有彻底检查边缘情况,但它正在处理我的测试输入。 也许它会对将来的某人有所帮助。 它是运行长度编码,但适用于字符组,而不是单个字符。根据我的阅读,正常的 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

【讨论】:

    猜你喜欢
    • 2022-12-29
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 2018-03-04
    相关资源
    最近更新 更多