-2

In lempel-ziv decode methode I get indexError this is my code, I know that size will be 3, while len(LT) is just 2. But I just converted the pseudocode to the 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 is the error message:

LT[size] = seg
IndexError: list assignment index out of range
AJProg
  • 1
  • 2
  • If you know that size is 3 and the length of your list is only 2, then you also know the reason for your error. Try to explain what this code is supposed to accomplish and then someone may be able to help. – benbo Sep 21 '15 at 12:45

1 Answers1

1

If you want to add a third item to your list, use append instead of assignment.

Replace

LT[size] = seg

With

LT.append(seg)
Kevin
  • 74,910
  • 12
  • 133
  • 166