IndexError: list assignment index out of range python 3.4 -


in lempel-ziv decode methode indexerror code, know size 3, while len(lt) 2. converted pseudocode python code .

 def decode(self,target):         tlen = len(target)         source = ''         source += target[0]         lt = ['',target[0]]         loc = 1         size = 2         while loc < tlen:             bitlen = ceil(log2(size))             index = self.bit_to_integer(target[loc:(loc+bitlen)])              seg = lt[index]              if loc+bitlen < tlen:                 seg += target[loc+bitlen]                 size += 1                 #print(size)                  #print(lt,size)                 lt[size] = seg                 loc += 1             source += seg             loc += bitlen         return source 

this error message:

lt[size] = seg indexerror: list assignment index out of range 

if want add third item list, use append instead of assignment.

replace

lt[size] = seg 

with

lt.append(seg) 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -