我需要一些有关RLE解码的帮助。例如,我想将03a10b05 @解压缩为aaabbbbbbbbbb @@@@@@。如何在Python中解决此问题?到目前为止,我已经尝试通过选择特定的字符串来导入re
。但是,当我放入03a12b时它不起作用,它将输出aaa。
答案 0 :(得分:0)
尽管问题没有您的尝试(代码段),但我还是要发布答案。
它依赖于这样一个事实,即整个编码的字符串可立即使用。
code.py :
#!/usr/bin/env python3
import sys
import re
pat = re.compile("(\d{2}.)*?")
def decode(s):
groups = (item for item in pat.findall(s) if item)
#print(list(groups)) # @TODO - cfati: !!! Decommenting this, will result in empty string being returned !!!
return "".join((group[-1] * int(group[:-1]) for group in groups))
def main():
encoded_strings = [
"03a10b05@",
"03a12b"
]
for encoded in encoded_strings:
print("{:s}: {:s}".format(encoded, decode(encoded)))
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main()
print("Done.")
注释:
输出:
[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q056031014]> "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe" code.py Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32 03a10b05@: aaabbbbbbbbbb@@@@@ 03a12b: aaabbbbbbbbbbbb Done.