我得到一個索引超出範圍錯誤,當我運行此代碼。它適用於tmp> 3並且tmp [2] == tmp [3]。即時通訊嘗試寫入文件索引[0]中的條件滿足時的數字。又名tmp [0] == tmp [1]。無論我嘗試過什麼,爲什麼我不能調用tmp [0]。Python。試圖獲得索引[0] ==索引[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
隨着第二代碼我得到另一追蹤誤差,在如果(TMP [0] == TMP [1] 帶有打印TMP [0],從不運行的Cuz與第二代碼回溯上述錯誤的。
序列看起來像這樣
(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)
我想匹配21和21 TMP [0] == TMP [1],然後寫入文件21
你能分享確切的回溯? –
你可以在賦值後給我們一個'print(tmp)'的輸出嗎? – AetherUnbound
'sequence [i] .replace(「(」,「」)。replace(「)」,「」)。split(「,」)'看起來像個窮人的'ast.literal_eval' –