LZW算法用于寻找输入符号之间的模式。但它能在语言中寻找模式吗?我的意思是,alfabet索引不是符号,而是文字,例如输入:
'abcd', 'abcd', 'fasf' , 'asda', 'abcd' , 'fasf' ...输出类似于:
'abcd', '1', 'fasf' , 'asda' , '1', '2' ...或者有什么压缩算法能起作用?
发布于 2014-03-12 21:23:18
keys = []
def lzw(text):
tokens = text.split()
new_keys = dict.fromkeys(tokens).keys()
keys.extend([key for key in new_keys if key not in keys])
encoded = ["%s"%keys.index(tok) for tok in tokens]
for i,key in enumerate(keys):
try:
encoded[encoded.index(str(i))] = key
except:
pass
return " ".join(encoded)
print lzw("abcd abcd fasf asda abcd fasf")
#outputs: abcd 0 fasf asda 0 2是一个很容易实现的
发布于 2014-03-12 21:25:10
您可以使用此代码搜索字符串以找到模式。你需要知道你想要搜索的是什么模式。
## Search for pattern 'iii' in string 'piiig'.
## All of the pattern must match, but it may appear anywhere.
## On success, match.group() is matched text.
match = re.search(r'iii', 'piiig') => found, match.group() == "iii"
match = re.search(r'igs', 'piiig') => not found, match == None阅读本网站:https://developers.google.com/edu/python/regular-expressions?hl=iw
https://stackoverflow.com/questions/22363942
复制相似问题