Nurzhan Nogerbek Nurzhan Nogerbek - 6 months ago 5
Python Question

IndexError in array PYTHON

I am tring to write this C# function in Python, unfortunatly I have this ERROR:

IndexError: list assignment index out of range
in
Population.BinaryX.insert([i][j], dec)
line. Can anyone tell me how can I fix this problem?! What`s wrong I did?

C# CODE:

public class Population
{
public float[] CodedX = new float[20];
public float[,] BinaryX = new float[10, 8];
}

private void BinaryTranslating()
{
int dec;
int j = 0;
for (var i = 0; i < 10; i++)
{
while (Population.CodedX[i] > 1 & j < 8)
{
dec = (int)Population.CodedX[i] % 2;
Population.BinaryX[i, j] = dec;
Population.CodedX[i] /= 2;
j++;
}
j = 0;
}
}

private void DecimalTranslating()
{
for (var i = 0; i < 10; i++)
{
Population.CodedX[i] = Population.BinaryX[i, 7] * 128 + Population.BinaryX[i, 6] * 64 +
Population.BinaryX[i, 5] * 32 + Population.BinaryX[i, 4] * 16 +
Population.BinaryX[i, 3] * 8 + Population.BinaryX[i, 2] * 4 +
Population.BinaryX[i, 1] * 2 + Population.BinaryX[i, 0];

}
}


Python CODE:

class Population:
CodedX = []
BinaryX = [[], []]

class Application:
@staticmethod
def binary_translating():
j = 0
for i in range(10):
while Population.CodedX[i] > 1 & j < 8:
dec = int(Population.CodedX[i]) % 2
Population.BinaryX.insert([i][j], dec)
Population.CodedX[i] /= 2
j += 1
j = 0

@staticmethod
def decimal_translating():
for i in range(10):
new_item = Population.BinaryX[i][7] * 128 + Population.BinaryX[i][6] * 64 + Population.BinaryX[i][5] * 32 +\
Population.BinaryX[i][4] * 16 + Population.BinaryX[i][3] * 8 + Population.BinaryX[i][2] * 4 +\
Population.BinaryX[i][1] * 2 + Population.BinaryX[i][0]
Population.CodedX.insert(i, new_item)

Answer

please make sure that Population.BinaryX is a valid entity and has at least 10 element in it because you are running the loop 10 times. Same goes for CodedX.

If either of them do not have at least 10 elements, you will get

IndexError: list assignment index out of range

Try..

class Population:
    CodedX = [0 for i in range(10)]
    BinaryX = [[0 for i in range(10)] for j in range(10)]

This is preallocating as mentioned by tdelaney.

If you look at each functions in the Application class, they try to use either BinaryX or CodedX arrays but if these arrays do not have any elements in them, then how will python index into them?

What I mean is before calling the decimal_translating() function, the Population.BinrayX must have elements in it. It cannot be empty array.

Similarly, before calling the binary_translating() function, Population.CodedX must have elements in it.

[edit #1] After your comments and trying to understand your code.Here is what I have:-

class Population(object):
    def __init__(self):
        self.CodedX = [0 for i in range(10)]  # as per your C# code
        self.BinaryX = []
        # debug code to fill CodedX array - remove it
        for i in range(10):
            self.CodedX[i] = int(761)

    def binary_translating(self):
        for i in range(10):
            j = 0
            self.BinaryX.append([0 for k in range(10)])
            while (self.CodedX[i] > 0) and (j < 10):
                dec = int(self.CodedX[i] % 2)
                self.BinaryX[i][j] = dec  # this will append at j
                self.CodedX[i] = int(self.CodedX[i] / 2)
                j += 1
        # debug code to print - remove it
        print(self.BinaryX)
        # debug code to clear CodedX - remove it
        for i in range(10):
            self.CodedX[i] = int(0)

    def decimal_translating(self):
        for i in range(10):
            value = self.BinaryX[i][7] * 128 + self.BinaryX[i][6] * 64 + self.BinaryX[i][5] * 32 + \
                             self.BinaryX[i][4] * 16 + self.BinaryX[i][3] * 8 + self.BinaryX[i][2] * 4 + \
                             self.BinaryX[i][1] * 2 + self.BinaryX[i][0]
            self.CodedX[i] = value
        print(self.CodedX)


pop = Population()

pop.binary_translating()
pop.decimal_translating()

I have added some debug code to have some starting values in CodedX and print statements for you to see the output.

generates the output:

[[1, 0, 0, 1, 1, 1, 1, 1, 0, 1], [1, 0, 0, 1, 1, 1, 1, 1, 0, 1], [1, 0, 0, 1, 1, 1, 1, 1, 0, 1], [1, 0, 0, 1, 1, 1, 1, 1, 0, 1], [1, 0, 0, 1, 1, 1, 1, 1, 0, 1], [1, 0, 0, 1, 1, 1, 1, 1, 0, 1], [1, 0, 0, 1, 1, 1, 1, 1, 0, 1], [1, 0, 0, 1, 1, 1, 1, 1, 0, 1], [1, 0, 0, 1, 1, 1, 1, 1, 0, 1], [1, 0, 0, 1, 1, 1, 1, 1, 0, 1]]

[249, 249, 249, 249, 249, 249, 249, 249, 249, 249]

[edit #2]this is what i meant by the conversion precision

Comments