2016-05-09 17 views
0

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) 
+1

Sie sollen nicht ein bitweise AND für booleans verwenden, verwenden Sie den richtigen "und" Operator in Python. – zoubida13

+1

Können Sie uns zeigen, wie Sie 'Population.BinaryX' definieren? – tdelaney

+0

Können Sie ein Beispiel für CodedX veröffentlichen? und was ist BinaryX? – WreckeR

Antwort

1

Sie bitte sicher, dass Population.BinaryX ist eine gültige Entität und enthält mindestens 10 Elemente, da Sie die Schleife 10 Mal ausführen. Gleiches gilt für CodedX.

Wenn einer von ihnen nicht mindestens 10 Elemente haben, erhalten Sie

IndexError: list assignment index out of range

Versuchen ..

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

Dies wird preallocating wie tdelaney erwähnt.

Wenn Sie sich die einzelnen Funktionen in der Application-Klasse ansehen, versuchen sie entweder BinaryX- oder CodedX-Arrays zu verwenden, aber wenn diese Arrays keine Elemente enthalten, wie wird Python dann in sie indexieren?

Was ich meine ist vor dem Aufruf der decimal_translating() Funktion, die Population.BinrayX muss Elemente enthalten. Es kann kein leeres Array sein.

In ähnlicher Weise müssen vor dem Aufruf der binary_translating() Funktion Population.CodedX Elemente enthalten sein.

[edit # 1] Nachdem Sie Ihre Kommentare und versuchen, Ihre code.Here ist zu verstehen, was ich habe: -

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() 

Ich habe einige Debug-Code hinzugefügt einige Startwerte in CodedX haben und Anweisungen drucken, damit Sie die Ausgabe sehen können.

erzeugt die Ausgabe:

[[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] 

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

+0

Hallo @Mrunmoy Ich habe das Programm gerade überprüft und ich habe Elemente in Population.CodedX. Ich bemerkte auch interessante Gedanken, wenn ich 'BinaryX = [[0 für i in range (10)] für j in range (10)] schrieb, es funktionierte, aber wenn ich als 'BinaryX = [[0 für i in range (8)] für j im Bereich (10)] 'es hat nicht funktioniert. Also bin ich ein bisschen verwirrt darüber. Hast du eine Idee?! –

+0

Übrigens, 'Population.BinrayX' verwende ich das erste Mal in' binary_translating() 'Funktion, so dass es am Anfang sowieso leer ist. Was ist in dieser Sache zu tun? –

+0

Wie ich verstehe, muss ich Element zu "Population.BinrayX" hinzufügen, aber wie zu einem mehrdimensionalen Array in dieser Ursache verwenden ?! –

3

Betrachten Sie die [i][j] Ausdruck in Population.BinaryX.insert([i][j], dec). Dieser Ausdruck erstellt eine Liste mit 1 Eintrag, die den Wert i enthält, und versucht dann, den Eintrag j aus dieser Liste zu übernehmen.

>>> i=1 
>>> j=2 
>>> [i][j] 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
IndexError: list index out of range 

Python-Listen sind eindimensional und wenn Sie eine mehrdimensionale Liste wollen, müssen Sie eine Liste von Listen erstellen oder eine andere Struktur, wie ein numpy oder pandas Array.

Eine Option ist das Array mit einem bekannten Wert vorzubelegen, so dass Sie einfach indizieren

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

Eine weitere Option in der Unterliste kann später einzufügen ist:

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

Ich versuche deine erste Version zu verwenden und ich habe die Frage, wie wir das schreiben können' Population.BinaryX [i] [j ] = dec' Es gibt 'IndexError: Listenzuweisungsindex außerhalb des Bereichs 'Was hast du dazu? –

+0

Können Sie meinen Beitrag erneut bitte überprüfen. Ich habe es mit neuen Funktionen aktualisiert, die mit der letzten Funktion verbunden sind, und zwar an der Stelle, an der ich 'Population.BinaryX' definiert habe. –

+0

@NurzhanNogerbek - Ich war eine Weile von meinem Computer weg ... froh zu sehen, dass die andere Antwort es für Sie durchgearbeitet hat! – tdelaney

Verwandte Themen