zu erhalten Ich erhalte einen Index außerhalb des Bereichs Fehler, wenn ich diesen Code ausführen. es funktioniert, wenn tmp> 3 und wenn tmp [2] == tmp [3]. Ich versuche zu schreiben die Zahlen in Index [0], wenn die Bedingungen erfüllt sind. aka tmp [0] == tmp [1]. Warum kann ich tmp [0] nicht aufrufen, egal was ich versucht habe.Python. Versucht Index [0] == Index [1]
i = 0
tmp = []
while(i < len(sequence)):
tmp = sequence[i].replace("(","").replace(")","").split(",")
if(len(tmp) > 1):
if(tmp[0] == tmp[1]):
print tmp[0]
with open(output_file1, 'a') as output:
output.write(str(tmp[0]) + '\n')
i = i+1
return True
python app.py
Traceback (most recent call last):
File "app.py", line 71, in <module>
generate(6, out_path2,out_path1, out_path)
File "app.py", line 45, in generate
random6th = random.choice(sequence2).replace("[", "").replace("]","").replac
e("'","").replace(" ","")
File "C:\Python27\lib\random.py", line 275, in choice
return seq[int(self.random() * len(seq))] # raises IndexError if seq is emp
ty
IndexError: list index out of range
i = 0
tmp = []
while(i < len(sequence)):
tmp.append(sequence[i].replace("(","").replace(")","").split(","))
if(len(tmp) > 0):
if(tmp[0] == tmp[1]):
print tmp[0]
with open(output_file1, 'a') as output:
output.write(str(tmp[0]) + '\n')
i = i+1
return True
Traceback (most recent call last):
File "app.py", line 69, in <module>
generate_2(seq_path, out_path1, out_path2);
File "app.py", line 20, in generate_2
if(tmp[0] == tmp[1]):
IndexError: list index out of range
Mit dem zweiten Code, den ich eine weitere Zurückverfolgungsfehlermeldung anzeigt, bei if (tmp [0] == tmp [1] Mit PRINT tmp [0], läuft nie mit zweitem Code Zurückverfolgungs der oben genannten Fehler Cuz.
Eine Sequenz sieht wie folgt aus
(8, 16, 5, 5, 65, 27)
(7, 15, 4, 4, 64, 26)
(21, 17, 12)
(22, 22, 11, 11, 59, 24)
(21, 21, 10, 9, 58, 23)
ich versuche, 21 und 21 tmp übereinstimmen [0] == tmp [1] und dann schreiben 21.
können Sie die genaue Rückverfolgung teilen? –
Können Sie uns nach der Zuweisung auch eine Ausgabe von 'print (tmp)' geben? – AetherUnbound
'sequence [i] .replace (" ("," "). Replace (") "," "). Split (", ")' das sieht aus wie ein armer Mann 'ast.literal_eval' –