Ich bin dran, diese C# -Funktion in Python zu schreiben, leider habe ich diese FEHLER: IndexError: list assignment index out of range
in Population.BinaryX.insert([i][j], dec)
Zeile. Kann mir jemand sagen, wie ich dieses Problem beheben kann ?! Was ist los ich habe?IndexError im Array PYTHON
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)
Sie sollen nicht ein bitweise AND für booleans verwenden, verwenden Sie den richtigen "und" Operator in Python. – zoubida13
Können Sie uns zeigen, wie Sie 'Population.BinaryX' definieren? – tdelaney
Können Sie ein Beispiel für CodedX veröffentlichen? und was ist BinaryX? – WreckeR